Marius Cramer
2014-01-28 746e52a6f3cfdd6438f9c62b39c119925a9405f9
Included changes from stable that could not be merged
1 files modified
114 ■■■■ changed files
server/lib/classes/cron.d/500-backup.inc.php 114 ●●●● patch | view | raw | blame | history
server/lib/classes/cron.d/500-backup.inc.php
@@ -72,7 +72,19 @@
                chmod(escapeshellcmd($backup_dir), $backup_dir_permissions);
            }
            $sql = "SELECT * FROM web_domain WHERE server_id = '".$conf['server_id']."' AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias') AND active = 'y'";
            //* mount backup directory, if necessary
            $run_backups = true;
            $server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
            if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
                if(!$app->system->is_mounted($backup_dir)){
                    exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
                    sleep(1);
                    if(!$app->system->is_mounted($backup_dir)) $run_backups = false;
                }
            }
            if($run_backups){
                $sql = "SELECT * FROM web_domain WHERE server_id = ".$conf['server_id']." AND (type = 'vhost' OR type = 'vhostsubdomain')";
            $records = $app->db->queryAllRecords($sql);
            if(is_array($records)) {
                foreach($records as $rec) {
@@ -94,17 +106,30 @@
                            chown($web_backup_dir, 'root');
                            chgrp($web_backup_dir, 'root');
                        }*/
                            $backup_excludes = '';
                            $b_excludes = explode(',', trim($rec['backup_excludes']));
                            if(is_array($b_excludes) && !empty($b_excludes)){
                                foreach($b_excludes as $b_exclude){
                                    $b_exclude = trim($b_exclude);
                                    if($b_exclude != ''){
                                        $backup_excludes .= ' --exclude='.escapeshellarg($b_exclude);
                                    }
                                }
                            }
                        if($backup_mode == 'userzip') {
                            //* Create a .zip backup as web user and include also files owned by apache / nginx user
                            $web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.zip';
                            exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print 2> /dev/null | zip -b /tmp --exclude=backup\* --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
                            if($retval == 0 || $retval == 12) exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -user '.escapeshellarg($http_server_user).' -print 2> /dev/null | zip -b /tmp --exclude=backup\* --update --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
                                exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print 2> /dev/null | zip -b /tmp --exclude=backup\*'.$backup_excludes.' --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
                                if($retval == 0 || $retval == 12) exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -user '.escapeshellarg($http_server_user).' -print 2> /dev/null | zip -b /tmp --exclude=backup\*'.$backup_excludes.' --update --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
                        } else {
                            //* Create a tar.gz backup as root user
                            $web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.tar.gz';
                            exec('tar pczf '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' --exclude=backup\* --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
                                exec('tar pczf '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' --exclude=backup\*'.$backup_excludes.' --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
                        }
                        if($retval == 0 || ($backup_mode != 'userzip' && $retval == 1) || ($backup_mode == 'userzip' && $retval == 12)) { // tar can return 1, zip can return 12(due to harmless warings) and still create valid backups
                                if(is_file($web_backup_dir.'/'.$web_backup_file)){
                            chown($web_backup_dir.'/'.$web_backup_file, 'root');
                            chgrp($web_backup_dir.'/'.$web_backup_file, 'root');
                            chmod($web_backup_dir.'/'.$web_backup_file, 0750);
@@ -112,9 +137,10 @@
                            //* 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.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
                            //$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,filesize) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."','".$app->functions->formatBytes(filesize($web_backup_dir.'/'.$web_backup_file))."')";
                                    $sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
                            $app->db->query($sql);
                            if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
                                }
                        } else {
                            if(is_file($web_backup_dir.'/'.$web_backup_file)) unlink($web_backup_dir.'/'.$web_backup_file);
                        }
@@ -150,7 +176,6 @@
                        unset($dir_handle);
                        //* Remove backupdir symlink and create as directory instead
                        $app->uses('system');
                        $app->system->web_folder_protection($web_path, false);
                        if(is_link($web_path.'/backup')) {
@@ -163,12 +188,11 @@
                        }
                        $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') {
                        if($rec['backup_interval'] == 'none' || $rec['backup_interval'] == '') {
                        $web_id = $rec['domain_id'];
                        $web_user = $rec['system_user'];
                        $web_backup_dir = realpath($backup_dir.'/web'.$web_id);
@@ -179,7 +203,7 @@
                }
            }
            $sql = "SELECT * FROM web_database WHERE server_id = '".$conf['server_id']."' AND backup_interval != 'none'";
                $sql = "SELECT * FROM web_database WHERE server_id = ".$conf['server_id']." AND backup_interval != 'none' AND backup_interval != ''";
            $records = $app->db->queryAllRecords($sql);
            if(is_array($records)) {
@@ -197,20 +221,19 @@
                        chown($db_backup_dir, 'root');
                        chgrp($db_backup_dir, 'root');
                        //* Do the mysql database backup with mysqldump or mongodump
                            //* Do the mysql database backup with mysqldump
                        $db_id = $rec['database_id'];
                        $db_name = $rec['database_name'];
                        if ($rec['type'] == 'mysql') {
                            $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."'";
                            $command = "mysqldump -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." -c --add-drop-table --quote-names --routines --events --triggers --hex-blob --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
                            $command = "mysqldump -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
                            exec($command, $tmp_output, $retval);
                            //* 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){
                                if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')){
                                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));
@@ -218,52 +241,15 @@
                                //* 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,filesize) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz','".$app->functions->formatBytes(filesize($db_backup_dir.'/'.$db_backup_file))."')";
                                    $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');
                            }
                            //* Remove the uncompressed file
                            if(is_file($db_backup_dir.'/'.$db_backup_file)) unlink($db_backup_dir.'/'.$db_backup_file);
                        } else if ($rec['type'] == 'mongo') {
                                $db_backup_file = 'db_'.$db_name.'_'.date('Y-m-d_H-i');
                                try {
                                    $connection = new MongoClient("mongodb://root:123456@127.0.0.1:27017/admin");
                                    $db = $connection->selectDB($db_name);
                                    // exclude not supported by mongodump, only get user collections
                                    $collections = $db->getCollectionNames(false);
                                    foreach ($collections as $collection) {
                                        // mongodump -h 127.0.0.1 --port 27017 -u root -p 123456 --authenticationDatabase admin -d <db> -c <table> -o /tmp/test
                                        $command = "mongodump -h 127.0.0.1 --port 27017 -u root -p 123456 --authenticationDatabase admin -d ".escapeshellcmd($db_name)." -c ".escapeshellcmd($collection)." -o ".escapeshellcmd($db_backup_dir.'/'.$db_backup_file);
                                        exec($command);
                                    }
                                    if (is_dir(escapeshellcmd($db_backup_dir.'/'.$db_backup_file))) {
                                        //* Compress the backup with gzip
                                        exec("cd ".escapeshellcmd($db_backup_dir)." && tar -pczf ".escapeshellcmd($db_backup_dir.'/'.$db_backup_file).".tar.gz ".escapeshellcmd($db_backup_file));
                                        chmod($db_backup_dir.'/'.$db_backup_file.'.tar.gz', 0750);
                                        chown($db_backup_dir.'/'.$db_backup_file.'.tar.gz', fileowner($db_backup_dir));
                                        chgrp($db_backup_dir.'/'.$db_backup_file.'.tar.gz', filegroup($db_backup_dir));
                                        //* Insert web backup record in database
                                        $sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mongodb','rootgz',".time().",'".$app->db->quote($db_backup_file).".tar.gz')";
                                        $app->db->query($sql);
                                        if ($app->db->dbHost != $app->dbmaster->dbHost) {
                                            $app->dbmaster->query($sql);
                                        }
                                        //* Remove the uncompressed file
                                        exec("rm -rf ".escapeshellcmd($db_backup_dir.'/'.$db_backup_file));
                                    }
                                } catch (MongoConnnectionException $e) {
                                    // connecting to MongoDB failed - cannot create backup
                                }
                            }
                        //* Remove old backups
                        $backup_copies = intval($rec['backup_copies']);
@@ -271,7 +257,7 @@
                        $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) || preg_match('/^db_(.*?)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.tar.gz$/', $entry, $matches)) && is_file($db_backup_dir.'/'.$entry)) {
                                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;
                            }
@@ -304,6 +290,28 @@
                unset($clientdb_password);
            }
                // remove non-existing backups from database
                $backups = $app->db->queryAllRecords("SELECT * FROM web_backup WHERE server_id = ".$conf['server_id']);
                if(is_array($backups) && !empty($backups)){
                    foreach($backups as $backup){
                        $backup_file = $backup_dir.'/web'.$backup['parent_domain_id'].'/'.$backup['filename'];
                        if(!is_file($backup_file)){
                            $sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$backup['parent_domain_id']." AND filename = '".$backup['filename']."'";
                            $app->db->query($sql);
                            if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
                        }
                    }
                }
            } else {
                //* send email to admin that backup directory could not be mounted
                $global_config = $app->getconf->get_global_config('mail');
                if($global_config['admin_mail'] != ''){
                    $subject = 'Backup directory '.$backup_dir.' could not be mounted';
                    $message = "Backup directory ".$backup_dir." could not be mounted.\n\nThe command\n\n".$server_config['backup_dir_mount_cmd']."\n\nfailed.";
                    mail($global_config['admin_mail'], $subject, $message);
                }
            }
        }
        parent::onRunJob();