Newer
Older
$app->uses('system');
$app->system->web_folder_protection($web_path,false);
tbrehm
committed
if(is_link($web_path.'/backup')) {
unlink($web_path.'/backup');
}
if(!is_dir($web_path.'/backup')) {
mkdir($web_path.'/backup');
tbrehm
committed
chgrp($web_path.'/backup', $rec['system_group']);
}
$app->system->web_folder_protection($web_path,true);
/* If backup_interval is set to none and we have a
backup directory for the website, then remove the backups */
if($rec['backup_interval'] == 'none') {
$web_id = $rec['domain_id'];
$web_user = $rec['system_user'];
$web_backup_dir = realpath($backup_dir.'/web'.$web_id);
if(is_dir($web_backup_dir)) {
exec('sudo -u '.escapeshellarg($web_user).' rm -f '.escapeshellarg($web_backup_dir.'/*'));
}
}
}
}
tbrehm
committed
$sql = "SELECT * FROM web_database WHERE server_id = '".$conf['server_id']."' AND backup_interval != 'none'";
$records = $app->db->queryAllRecords($sql);
if(is_array($records)) {
tbrehm
committed
include('lib/mysql_clientdb.conf');
tbrehm
committed
foreach($records as $rec) {
tbrehm
committed
//* Do the database backup
if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
tbrehm
committed
$web_id = $rec['parent_domain_id'];
$db_backup_dir = $backup_dir.'/web'.$web_id;
if(!is_dir($db_backup_dir)) mkdir($db_backup_dir, 0750);
chmod($db_backup_dir, 0750);
chown($db_backup_dir, 'root');
chgrp($db_backup_dir, 'root');
tbrehm
committed
//* Do the mysql database backup with mysqldump
$db_id = $rec['database_id'];
$db_name = $rec['database_name'];
$db_backup_file = 'db_'.$db_name.'_'.date('Y-m-d_H-i').'.sql';
$command = "mysqldump -h '".escapeshellcmd($clientdb_host)."' -u '".escapeshellcmd($clientdb_user)."' -p'".escapeshellcmd($clientdb_password)."' -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
exec($command, $tmp_output, $retval);
tbrehm
committed
//* Compress the backup with gzip
if($retval == 0) exec("gzip -c '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file)."' > '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file).".gz'", $tmp_output, $retval);
if($retval == 0){
chmod($db_backup_dir.'/'.$db_backup_file.'.gz', 0750);
chown($db_backup_dir.'/'.$db_backup_file.'.gz', fileowner($db_backup_dir));
chgrp($db_backup_dir.'/'.$db_backup_file.'.gz', filegroup($db_backup_dir));
//* Insert web backup record in database
//$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
//$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
$sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
$app->db->query($sql);
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
} else {
if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')) unlink($db_backup_dir.'/'.$db_backup_file.'.gz');
}
tbrehm
committed
//* Remove the uncompressed file
if(is_file($db_backup_dir.'/'.$db_backup_file)) unlink($db_backup_dir.'/'.$db_backup_file);
tbrehm
committed
//* Remove old backups
$backup_copies = intval($rec['backup_copies']);
tbrehm
committed
$dir_handle = dir($db_backup_dir);
$files = array();
while (false !== ($entry = $dir_handle->read())) {
if($entry != '.' && $entry != '..' && preg_match('/^db_(.*?)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.sql.gz$/', $entry, $matches) && is_file($db_backup_dir.'/'.$entry)) {
if(array_key_exists($matches[1], $files) == false) $files[$matches[1]] = array();
$files[$matches[1]][] = $entry;
tbrehm
committed
}
}
$dir_handle->close();
reset($files);
foreach($files as $db_name => $filelist) {
rsort($filelist);
for ($n = $backup_copies; $n <= 10; $n++) {
if(isset($filelist[$n]) && is_file($db_backup_dir.'/'.$filelist[$n])) {
unlink($db_backup_dir.'/'.$filelist[$n]);
//$sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
//$tmp = $app->dbmaster->queryOneRecord($sql);
//$sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
$app->db->query($sql);
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
}
}
}
tbrehm
committed
unset($files);
unset($dir_handle);
}
}
tbrehm
committed
unset($clientdb_host);
unset($clientdb_user);
unset($clientdb_password);
tbrehm
committed
}
tbrehm
committed
die("finished.\n");