6 * This file is part of GForge.
8 * GForge is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * GForge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GForge; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
23 require dirname(__FILE__).'/../www/env.inc.php';
24 require_once $gfwww.'include/pre.php';
25 require $gfcommon.'include/cron_utils.php';
28 $database=forge_get_config('database_name');
29 $username=forge_get_config('database_user');
30 $password=forge_get_config('database_password');
32 $datetime=date('Y-m-d'); //we will use this to concatenate it with the tar filename
34 if(!(isset($sys_path_to_backup)) || (strcmp($sys_path_to_backup,"/") == 0)){
35 // cron_entry(23,'Variable $sys_path_to_backup was not set or it was equal to /.');
38 $sys_path_to_backup = '/gforge-backups/';
41 if(util_is_root_dir($sys_path_to_backup)){
42 $sys_path_to_backup=$sys_path_to_backup.'/';
45 if (!is_dir($sys_path_to_backup)) {
46 // try to recursively create it
47 $subdirs = explode('/', $sys_path_to_backup);
49 foreach ($subdirs as $subdir) {
50 $subdir = trim($subdir);
51 if (empty($subdir)) continue;
53 if (!file_exists($path)) {
55 cron_entry(23,'Couldn\'t create directory '.$path.' for backups');
63 if (!preg_match('/\\/$/',$sys_path_to_backup)) {
64 $sys_path_to_backup .= '/';
69 @exec('echo -n -e "'.$password.'\n" | pg_dump -U '.$username.' -v -Ft -b 2>&1 '.$database.' > '.$sys_path_to_backup.'db-'.$database.'-tmp-'.$datetime.'.tar ',$output,$retval); //proceed with db dump
71 $err.= implode("\n", $output);
74 /**************************************
76 **************************************/
78 if (file_exists(forge_get_config('upload_dir'))) {
79 @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
81 $err.= implode("\n", $output);
84 $err.= 'Unable to find upload dir. Configured value is:'.forge_get_config('upload_dir');
87 /**************************************
88 * Backup mailing lists files
89 **************************************/
91 // Most probable mailman data dir
92 $mailman_data_dir = '/var/lib/mailman';
93 if (file_exists($mailman_data_dir)) {
94 @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
96 $err.= implode("\n", $output);
99 $err.= 'Unable to find Mailman data dir. Please edit backup_site.php cronjob';
102 /**************************************
103 * Backup CVS repositories
104 **************************************/
106 if (file_exists($cvsdir_prefix)) {
107 @exec('tar -hjcvf '.$sys_path_to_backup.'cvsroot-tmp-'.$datetime.'.tar.bz2 '.$cvsdir_prefix.'/ 2>&1' ,$output,$retval); //proceed cvsroot dir tar file creation
109 $err.= implode("\n", $output);
112 $err.= 'Unable to find CVSROOT dir. Configured value is:'.$cvsdir_prefix;
115 /**************************************
116 * Backup SVN repositories (using the hot-backup.py script)
117 **************************************/
118 if (file_exists($svndir_prefix)) {
119 $hot_backup = dirname(__FILE__).'/hot-backup.py';
120 $svn_path = dirname(`which svn`);
123 if (!file_exists($hot_backup) || !$svn_path) {
124 // hot-backup.py script not available, try regular backup (doesn't check for inconsistencies)
125 @exec('tar -hjcvf '.$sys_path_to_backup.'svnroot-tmp-'.$datetime.'.tar.bz2 '.$svndir_prefix.'/ 2>&1' ,$output,$retval); //proceed svnroot dir tar file creation
127 $err.= implode("\n", $output);
130 // backup the files using hot-backup script
131 $repos_backup_dir = $sys_path_to_backup.'/svn-repositories-'.$datetime.'/';
132 mkdir($repos_backup_dir);
133 $dh = opendir($svndir_prefix);
134 while ($file = readdir($dh)) {
135 if (preg_match('/^\\./', $file)) continue; // skip files that start with a dot
136 $path = $svndir_prefix.'/'.$file;
137 if (!is_dir($path)) continue; // not a repository
138 $cmd = 'SVN_PATH="'.$svn_path.'" '.$hot_backup.' '.$path.' '.$repos_backup_dir;
139 @exec($cmd, $output, $retval);
141 $err.= implode("\n", $output);
146 @exec('tar -hjcvf '.$sys_path_to_backup.'svnroot-tmp-'.$datetime.'.tar.bz2 '.$repos_backup_dir.' 2>&1' ,$output,$retval);
148 $err.= implode("\n", $output);
151 @exec("rm -rf ".$repos_backup_dir);
154 $err.= 'Unable to find SVNROOT dir. Configured value is:'.$svndir_prefix;
157 /**************************************
158 * Backup config files
159 **************************************/
161 if (file_exists(forge_get_config('config_path'))) {
162 @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
164 $err.= implode("\n", $output);
167 $err.= 'Unable to find '.forge_get_config('config_path').' dir.';
171 /**************************************
173 **************************************/
175 @exec('tar -jcvf '.$sys_path_to_backup.'backup'.$datetime.'.tar.bz2 '.$sys_path_to_backup.'*-tmp-'.$datetime.'* 2>&1',$output,$retval);
177 $err.= implode("\n", $output);
180 //If execution of tar command was successfull ($retval equals zero) remove individual files
183 @exec('rm '.$sys_path_to_backup.'*tmp-'.$datetime.'* 2>&1',$output,$retval);
185 $err.= implode("\n", $output);