6 * This file is part of FusionForge. FusionForge is free software;
7 * you can redistribute it and/or modify it under the terms of the
8 * GNU General Public License as published by the Free Software
9 * Foundation; either version 2 of the Licence, or (at your option)
12 * FusionForge is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 require dirname(__FILE__).'/../www/env.inc.php';
23 require_once $gfcommon.'include/pre.php';
24 require $gfcommon.'include/cron_utils.php';
27 $database=forge_get_config('database_name');
28 $username=forge_get_config('database_user');
29 $password=forge_get_config('database_password');
30 $host=forge_get_config('database_host');
31 $port=forge_get_config('database_port');
33 $datetime=date('Y-m-d'); //we will use this to concatenate it with the tar filename
35 if(!(isset($sys_path_to_backup)) || (strcmp($sys_path_to_backup,"/") == 0)){
36 // cron_entry(23,'Variable $sys_path_to_backup was not set or it was equal to /.');
39 $sys_path_to_backup = '/gforge-backups/';
42 if(util_is_root_dir($sys_path_to_backup)){
43 $sys_path_to_backup=$sys_path_to_backup.'/';
46 if (!is_dir($sys_path_to_backup)) {
47 // try to recursively create it
48 $subdirs = explode('/', $sys_path_to_backup);
50 foreach ($subdirs as $subdir) {
51 $subdir = trim($subdir);
52 if (empty($subdir)) continue;
54 if (!file_exists($path)) {
56 cron_entry(23,'Couldn\'t create directory '.$path.' for backups');
64 if (!preg_match('/\\/$/',$sys_path_to_backup)) {
65 $sys_path_to_backup .= '/';
70 $dump_cmd = 'pg_dump -U ' . $username;
72 $dump_cmd .= ' -h ' . $host;
75 $dump_cmd .= ' -p ' . $port;
78 $tmpfname = tempnam(sys_get_temp_dir(), "tmp");
80 $handle = fopen($tmpfname, "w");
82 $line .= $host ? "$host:" : "localhost:";
83 $line .= $port ? "$port:" : "5432:";
84 $line .= "$database:$username:$password";
85 fwrite($handle, "$line");
88 $dump_cmd .= ' -v -Ft -b '.$database;
89 @exec('PGPASSFILE='.$tmpfname.' '.$dump_cmd.' 2>&1 > '.$sys_path_to_backup.'db-'.$database.'-tmp-'.$datetime.'.tar ',$output,$retval); //proceed with db dump
93 $err.= implode("\n", $output);
96 /**************************************
98 **************************************/
100 if (file_exists(forge_get_config('upload_dir'))) {
101 @exec('tar -hjcvf '.$sys_path_to_backup.'uploads-tmp-'.$datetime.'.tar.bz2 '.forge_get_config('upload_dir').' 2>&1' ,$output,$retval); //proceed upload dir tar file creation
103 $err.= implode("\n", $output);
106 $err.= 'Unable to find upload dir. Configured value is:'.forge_get_config('upload_dir');
109 /**************************************
110 * Backup mailing lists files
111 **************************************/
113 // Most probable mailman data dir
114 $mailman_data_dir = '/var/lib/mailman';
115 if (file_exists($mailman_data_dir)) {
116 @exec('tar -jcvf '.$sys_path_to_backup.'mailinglist-tmp-'.$datetime.'.tar.bz2 '.$mailman_data_dir.'/ 2>&1', $output,$retval); //proceed mailman dir tar file creation
118 $err.= implode("\n", $output);
121 $err.= 'Unable to find Mailman data dir. Please edit backup_site.php cronjob';
124 /**************************************
125 * Backup CVS repositories
126 **************************************/
128 if (file_exists(forge_get_config('repos_path', 'scmcvs'))) {
129 @exec('tar -hjcvf '.$sys_path_to_backup.'cvsroot-tmp-'.$datetime.'.tar.bz2 '.forge_get_config('repos_path', 'scmcvs').'/ 2>&1' ,$output,$retval); //proceed cvsroot dir tar file creation
131 $err.= implode("\n", $output);
134 $err.= 'Unable to find CVSROOT dir. Configured value is:'.forge_get_config('repos_path', 'scmcvs');
137 /**************************************
138 * Backup SVN repositories (using the hot-backup.py script)
139 **************************************/
140 if (file_exists($svndir_prefix)) {
141 $hot_backup = dirname(__FILE__).'/hot-backup.py';
142 $svn_path = dirname(`which svn`);
145 if (!file_exists($hot_backup) || !$svn_path) {
146 // hot-backup.py script not available, try regular backup (doesn't check for inconsistencies)
147 @exec('tar -hjcvf '.$sys_path_to_backup.'svnroot-tmp-'.$datetime.'.tar.bz2 '.$svndir_prefix.'/ 2>&1' ,$output,$retval); //proceed svnroot dir tar file creation
149 $err.= implode("\n", $output);
152 // backup the files using hot-backup script
153 $repos_backup_dir = $sys_path_to_backup.'/svn-repositories-'.$datetime.'/';
154 mkdir($repos_backup_dir);
155 $dh = opendir($svndir_prefix);
156 while ($file = readdir($dh)) {
157 if (preg_match('/^\\./', $file)) continue; // skip files that start with a dot
158 $path = $svndir_prefix.'/'.$file;
159 if (!is_dir($path)) continue; // not a repository
160 $cmd = 'SVN_PATH="'.$svn_path.'" '.$hot_backup.' '.$path.' '.$repos_backup_dir;
161 @exec($cmd, $output, $retval);
163 $err.= implode("\n", $output);
168 @exec('tar -hjcvf '.$sys_path_to_backup.'svnroot-tmp-'.$datetime.'.tar.bz2 '.$repos_backup_dir.' 2>&1' ,$output,$retval);
170 $err.= implode("\n", $output);
173 @exec("rm -rf ".$repos_backup_dir);
176 $err.= 'Unable to find SVNROOT dir. Configured value is:'.$svndir_prefix;
179 /**************************************
180 * Backup config files
181 **************************************/
183 if (file_exists(forge_get_config('config_path'))) {
184 @exec('tar -jcvf '.$sys_path_to_backup.'etc-tmp-'.$datetime.'.tar.bz2 '.forge_get_config('config_path').' 2>&1' ,$output,$retval); //proceed svnroot dir tar file creation
186 $err.= implode("\n", $output);
189 $err.= 'Unable to find '.forge_get_config('config_path').' dir.';
193 /**************************************
195 **************************************/
197 @exec('tar -jcvf '.$sys_path_to_backup.'backup'.$datetime.'.tar.bz2 '.$sys_path_to_backup.'*-tmp-'.$datetime.'* 2>&1',$output,$retval);
199 $err.= implode("\n", $output);
202 //If execution of tar command was successfull ($retval equals zero) remove individual files
205 @exec('rm '.$sys_path_to_backup.'*tmp-'.$datetime.'* 2>&1',$output,$retval);
207 $err.= implode("\n", $output);