A. Täffner
2016-01-23 afee192d2a77afa1c11d25156d0a44df94e19aed
server/lib/classes/cron.d/500-backup.inc.php
@@ -52,7 +52,7 @@
      $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
      $global_config = $app->getconf->get_global_config('sites');
      $backup_dir = $server_config['backup_dir'];
      $backup_dir = trim($server_config['backup_dir']);
      $backup_mode = $server_config['backup_mode'];
      if($backup_mode == '') $backup_mode = 'userzip';
@@ -117,12 +117,12 @@
                     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\*'.$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);
                        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\*'.$backup_excludes.' --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)){
@@ -192,11 +192,16 @@
                     $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.'/*'));
                        $sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ?";
                        $app->db->query($sql, $conf['server_id'], $web_id);
                        if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $web_id);
                        $dir_handle = opendir($web_backup_dir.'/');
                        while ($file = readdir($dir_handle)) {
                           if(!is_dir($file)) {
                              unlink ("$web_backup_dir/"."$file");
                           }
                        }
                     }
                     $sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ?";
                     $app->db->query($sql, $conf['server_id'], $web_id);
                     if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $web_id);
                  }
               }
            }
@@ -353,15 +358,7 @@
            }
            //* end run_backups
            if( $server_config['backup_dir_is_mount'] == 'y' ) $app->system->umount_backup_dir($backup_dir);
         } 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);
            }
         }
         }
      }
      
      // delete files from backup download dir (/var/www/example.com/backup)