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';
26 $database=forge_get_config('database_name');
27 $username=forge_get_config('database_user');
28 $password=forge_get_config('database_password');
29 $host=forge_get_config('database_host');
30 $port=forge_get_config('database_port');
32 $num_of_days = forge_get_config('backup_days'); // Number of days to keep if set, unlimited if not set
34 $datetime=date('Y-m-d'); //we will use this to concatenate it with the tar filename
36 if(!(isset($sys_path_to_backup)) || (strcmp($sys_path_to_backup,"/") == 0)){
37 // cron_entry(23,'Variable $sys_path_to_backup was not set or it was equal to /.');
40 $sys_path_to_backup = '/gforge-backups/';
43 if(util_is_root_dir($sys_path_to_backup)){
44 $sys_path_to_backup=$sys_path_to_backup.'/';
47 if (!is_dir($sys_path_to_backup)) {
48 if (!mkdir($sys_path_to_backup, 0700, true)) {
49 cron_entry(23,'Couldn\'t create directory '.$sys_path_to_backup.' for backups');
55 if (!preg_match('/\\/$/',$sys_path_to_backup)) {
56 $sys_path_to_backup .= '/';
59 function delete_files( $files )
64 @exec('rm -rf '.$files.' 2>&1', $output, $retval);
66 $err .= implode("\n", $output);
75 /* Delete all old backups with in 30 days old */
76 for( $day = $num_of_days; $day <= $num_of_days + 30; $day = $day + 1)
79 $trash = date('Y-m-d', mktime(0,0,0, date("m"), date("d")-$day, date("Y")));
82 delete_files($sys_path_to_backup.'/backup'.$trash.'.tar.bz2');
83 delete_files($sys_path_to_backup.'/*'.$trash.'.tar.bz2');
84 delete_files($sys_path_to_backup.'/*'.$trash.'.tar');
85 delete_files($sys_path_to_backup.'/*'.$trash);
89 $dump_cmd = 'pg_dump -U ' . $username;
91 $dump_cmd .= ' -h ' . $host;
94 $dump_cmd .= ' -p ' . $port;
97 $tmpfname = tempnam(sys_get_temp_dir(), "tmp");
99 $handle = fopen($tmpfname, "w");
101 $line .= $host ? "$host:" : "localhost:";
102 $line .= $port ? "$port:" : "5432:";
103 $line .= "$database:$username:$password";
104 fwrite($handle, "$line");
107 $dump_cmd .= ' -v -Ft -b '.$database;
108 @exec('PGPASSFILE='.$tmpfname.' '.$dump_cmd.' 2>&1 > '.$sys_path_to_backup.'db-'.$database.'-tmp-'.$datetime.'.tar ',$output,$retval); //proceed with db dump
112 $err.= implode("\n", $output);
115 /**************************************
117 **************************************/
119 if (file_exists(forge_get_config('upload_dir'))) {
120 @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
122 $err.= implode("\n", $output);
125 $err.= 'Unable to find upload dir. Configured value is:'.forge_get_config('upload_dir');
128 /**************************************
129 * Backup mailing lists files
130 **************************************/
132 // Most probable mailman data dir
133 $mailman_data_dir = '/var/lib/mailman';
134 if (file_exists($mailman_data_dir)) {
135 @exec('tar -hjcvf '.$sys_path_to_backup.'mailinglist-tmp-'.$datetime.'.tar.bz2 '.$mailman_data_dir.'/ 2>&1', $output,$retval); //proceed mailman dir tar file creation
137 $err.= implode("\n", $output);
140 $err.= 'Unable to find Mailman data dir. Please edit backup_site.php cronjob';
143 /**************************************
144 * Backup CVS repositories
145 **************************************/
147 if (file_exists(forge_get_config('repos_path', 'scmcvs'))) {
148 @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
150 $err.= implode("\n", $output);
153 $err.= 'Unable to find CVSROOT dir. Configured value is:'.forge_get_config('repos_path', 'scmcvs');
156 /**************************************
157 * Backup SVN repositories (using the hot-backup.py script)
158 **************************************/
159 $svndir_prefix = forge_get_config ('repos_path', 'scmsvn');
160 if (file_exists($svndir_prefix)) {
161 $hot_backup = dirname(__FILE__).'/hot-backup.py';
162 $svn_path = dirname(`which svn`);
165 if (!file_exists($hot_backup) || !$svn_path) {
166 // hot-backup.py script not available, try regular backup (doesn't check for inconsistencies)
167 @exec('tar -hjcvf '.$sys_path_to_backup.'svnroot-tmp-'.$datetime.'.tar.bz2 '.$svndir_prefix.'/ 2>&1' ,$output,$retval); //proceed svnroot dir tar file creation
169 $err.= implode("\n", $output);
172 // backup the files using hot-backup script
173 $repos_backup_dir = $sys_path_to_backup.'/svn-repositories-'.$datetime.'/';
174 mkdir($repos_backup_dir);
175 $dh = opendir($svndir_prefix);
176 while ($file = readdir($dh)) {
177 if (preg_match('/^\\./', $file)) continue; // skip files that start with a dot
178 $path = $svndir_prefix.'/'.$file;
179 if (!is_dir($path)) continue; // not a repository
180 $cmd = 'SVN_PATH="'.$svn_path.'" '.$hot_backup.' '.$path.' '.$repos_backup_dir;
181 @exec($cmd, $output, $retval);
183 $err.= implode("\n", $output);
188 @exec('tar -hjcvf '.$sys_path_to_backup.'svnroot-tmp-'.$datetime.'.tar.bz2 '.$repos_backup_dir.' 2>&1' ,$output,$retval);
190 $err.= implode("\n", $output);
193 @exec("rm -rf ".$repos_backup_dir);
196 $err.= 'Unable to find SVNROOT dir. Configured value is:'.$svndir_prefix;
199 /**************************************
200 * Backup config files
201 **************************************/
203 if (file_exists(forge_get_config('config_path'))) {
204 @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
206 $err.= implode("\n", $output);
209 $err.= 'Unable to find '.forge_get_config('config_path').' dir.';
213 /**************************************
215 **************************************/
217 @exec('tar -jcvf '.$sys_path_to_backup.'backup'.$datetime.'.tar.bz2 '.$sys_path_to_backup.'*-tmp-'.$datetime.'* 2>&1',$output,$retval);
219 $err.= implode("\n", $output);
222 //If execution of tar command was successful ($retval equals zero) remove individual files
225 @exec('rm '.$sys_path_to_backup.'*tmp-'.$datetime.'* 2>&1',$output,$retval);
227 $err.= implode("\n", $output);