Skip to content

Commit 8079c42

Browse files
author
florian030
committed
use pigz if available for tar-backups and add definable tmp-dir for zip-backups
1 parent 3d245ca commit 8079c42

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

install/tpl/server.ini.master

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ firewall=bastille
1616
loglevel=2
1717
admin_notify_events=1
1818
backup_dir=/var/backup
19+
backup_tmp=/tmp
1920
backup_dir_is_mount=n
2021
backup_mode=rootgz
2122
backup_time=0:00

interface/web/admin/form/server_config.tform.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@
183183
'width' => '40',
184184
'maxlength' => '255'
185185
),
186+
'backup_tmp' => array(
187+
'datatype' => 'VARCHAR',
188+
'formtype' => 'TEXT',
189+
'default' => '/tmp/',
190+
'validators' => array( 0 => array('type' => 'NOTEMPTY',
191+
'errmsg' => 'tmpdir_path_error_empty'),
192+
1 => array ( 'type' => 'REGEX',
193+
'regex' => '/^\/[a-zA-Z0-9\.\-\_\/]{5,128}$/',
194+
'errmsg'=> 'tmpdir_path_error_regex'),
195+
),
196+
'value' => '',
197+
'width' => '40',
198+
'maxlength' => '255'
199+
),
186200
'backup_dir_is_mount' => array(
187201
'datatype' => 'VARCHAR',
188202
'formtype' => 'CHECKBOX',

interface/web/admin/lib/lang/de_server_config.lng

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ $wb['awstats_data_dir_txt'] = 'AWStats Datenverzeichnis';
8181
$wb['awstats_pl_txt'] = 'AWStats awstats.pl Script';
8282
$wb['awstats_buildstaticpages_pl_txt'] = 'AWStats awstats_buildstaticpages.pl Script';
8383
$wb['backup_dir_txt'] = 'Backupverzeichnis';
84+
$wb['backup_tmp_txt'] = 'Backup tmp-Dir (zip)';
8485
$wb['named_conf_local_path_txt'] = 'BIND named.conf.local Pfad';
8586
$wb['php_ini_path_cgi_txt'] = 'CGI php.ini Pfad';
8687
$wb['php_ini_path_apache_txt'] = 'Apache php.ini Pfad';
@@ -103,6 +104,8 @@ $wb['nginx_user_txt'] = 'Nginx Benutzer';
103104
$wb['nginx_group_txt'] = 'Nginx Gruppe';
104105
$wb['nginx_cgi_socket_txt'] = 'Nginx CGI Socket';
105106
$wb['backup_dir_error_empty'] = 'Backup Verzeichnis ist leer.';
107+
$wb['tmpdir_path_error_empty'] = 'Tmp-Dir Pfad ist leer.';
108+
$wb['tmpdir_path_error_regex'] = 'Invalid Tmp-Dir Pfad.';
106109
$wb['maildir_path_error_empty'] = 'Maildir Pfad ist leer.';
107110
$wb['homedir_path_error_empty'] = 'Homedir Pfad ist leer.';
108111
$wb['mailuser_uid_error_empty'] = 'Mail Benutzer UID ist leer.';

interface/web/admin/lib/lang/en_server_config.lng

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ $wb["awstats_data_dir_txt"] = 'awstats data folder';
9090
$wb["awstats_pl_txt"] = 'awstats.pl script';
9191
$wb["awstats_buildstaticpages_pl_txt"] = 'awstats_buildstaticpages.pl script';
9292
$wb["backup_dir_txt"] = 'Backup directory';
93+
$wb["backup_tmp_txt"] = 'Backup tmp directory for zip';
9394
$wb["named_conf_local_path_txt"] = 'BIND named.conf.local path';
9495
$wb["php_ini_path_cgi_txt"] = 'CGI php.ini path';
9596
$wb["php_ini_path_apache_txt"] = 'Apache php.ini path';
@@ -101,6 +102,8 @@ $wb["fastcgi_config_syntax_txt"] = 'FastCGI config syntax';
101102
$wb["backup_mode_txt"] = 'Backup mode';
102103
$wb["backup_mode_userzip"] = 'Backup web files owned by web user as zip';
103104
$wb["backup_mode_rootgz"] = 'Backup all files in web directory as root user';
105+
$wb['tmpdir_path_error_empty'] = 'tmp-dir Path is empty.';
106+
$wb['tmpdir_path_error_regex'] = 'Invalid tmp-dir path.';
104107
$wb["backup_time_txt"] = 'Backup time';
105108
$wb["server_type_txt"] = 'Server Type';
106109
$wb["nginx_vhost_conf_dir_txt"] = 'Nginx Vhost config dir';

interface/web/admin/templates/server_config_server_edit.htm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ <h1><tmpl_var name="list_head_txt"></h1>
6363
{tmpl_var name='backup_mode'}
6464
</select></div>
6565
</div>
66+
<div class="form-group">
67+
<label for="backup_tmp" class="col-sm-3 control-label">{tmpl_var name='backup_tmp_txt'}</label>
68+
<div class="col-sm-9"><input type="text" name="backup_tmp" id="backup_tmp" value="{tmpl_var name='backup_tmp'}" class="form-control" /></div>
69+
</div>
6670
<div class="form-group">
6771
<label for="backup_time" class="col-sm-3 control-label">{tmpl_var name='backup_time_txt'}</label>
6872
<div class="col-sm-3"><select name="backup_time" id="backup_time" class="form-control">

server/lib/classes/cron.d/500-backup.inc.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function onRunJob() {
5454
$global_config = $app->getconf->get_global_config('sites');
5555
$backup_dir = trim($server_config['backup_dir']);
5656
$backup_mode = $server_config['backup_mode'];
57+
$backup_tmp = trim($server_config['backup_tmp']);
5758
if($backup_mode == '') $backup_mode = 'userzip';
5859

5960
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
@@ -77,6 +78,15 @@ public function onRunJob() {
7778
if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $run_backups = false;
7879
if($run_backups){
7980
$web_array = array();
81+
82+
system('which pigz > /dev/null', $ret);
83+
if($ret === 0) {
84+
$use_pigz = true;
85+
$zip_cmd = 'pigz'; // db-backups
86+
} else {
87+
$use_pigz = false;
88+
$zip_cmd = 'gzip'; // db-backups
89+
}
8090

8191
//* backup only active domains
8292
$sql = "SELECT * FROM web_domain WHERE server_id = ? AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias') AND active = 'y'";
@@ -117,12 +127,16 @@ public function onRunJob() {
117127
if($backup_mode == 'userzip') {
118128
//* Create a .zip backup as web user and include also files owned by apache / nginx user
119129
$web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.zip';
120-
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);
121-
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);
130+
exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print 2> /dev/null | zip -b '.escapeshellarg($backup_tmp).' --exclude=./backup\*'.$backup_excludes.' --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
131+
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 '.escapeshellarg($backup_tmp).' --exclude=./backup\*'.$backup_excludes.' --update --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
122132
} else {
123133
//* Create a tar.gz backup as root user
124134
$web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.tar.gz';
125-
exec('tar pczf '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' --exclude=./backup\*'.$backup_excludes.' --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
135+
if ($use_pigz) {
136+
exec('tar pcf - --directory '.escapeshellarg($web_path).' . --exclude=./backup\*'.$backup_excludes.' | pigz > '.escapeshellarg($web_backup_dir.'/'.$web_backup_file), $tmp_output, $retval);
137+
} else {
138+
exec('tar pczf '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' --exclude=./backup\*'.$backup_excludes.' --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
139+
}
126140
}
127141
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
128142
if(is_file($web_backup_dir.'/'.$web_backup_file)){
@@ -241,8 +255,8 @@ public function onRunJob() {
241255
$command = "mysqldump -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." -c --add-drop-table --create-options --quick --max_allowed_packet=512M --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
242256
exec($command, $tmp_output, $retval);
243257

244-
//* Compress the backup with gzip
245-
if($retval == 0) exec("gzip -c '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file)."' > '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file).".gz'", $tmp_output, $retval);
258+
//* Compress the backup with gzip / pigz
259+
if($retval == 0) exec("$zip_cmd -c '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file)."' > '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file).".gz'", $tmp_output, $retval);
246260

247261
if($retval == 0){
248262
if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')){

server/lib/classes/cron.d/500-backup_mail.inc.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function onRunJob() {
5959

6060
$backup_mode = $server_config['backup_mode'];
6161
if($backup_mode == '') $backup_mode = 'userzip';
62+
$backup_tmp = trim($server_config['backup_tmp']);
6263

6364
if($backup_dir != '') {
6465
$run_backups = true;
@@ -72,7 +73,12 @@ public function onRunJob() {
7273
} else {
7374
chmod(escapeshellcmd($backup_dir), $backup_dir_permissions);
7475
}
75-
76+
system('which pigz > /dev/null', $ret);
77+
if($ret === 0) {
78+
$use_pigz = true;
79+
} else {
80+
$use_pigz = false;
81+
}
7682
foreach($records as $rec) {
7783
//* Do the mailbox backup
7884
$email = $rec['email'];
@@ -120,11 +126,15 @@ public function onRunJob() {
120126

121127
if($backup_mode == 'userzip') {
122128
$mail_backup_file.='.zip';
123-
exec('cd '.$this->tmp_backup_dir.' && zip '.$mail_backup_dir.'/'.$mail_backup_file.' -b /tmp -r backup > /dev/null && rm -rf backup', $tmp_output, $retval);
129+
exec('cd '.$this->tmp_backup_dir.' && zip '.$mail_backup_dir.'/'.$mail_backup_file.' -b '.escapeshellarg($backup_tmp).' -r backup > /dev/null && rm -rf backup', $tmp_output, $retval);
124130
}
125131
else {
126132
$mail_backup_file.='.tar.gz';
127-
exec(escapeshellcmd('tar pczf '.$mail_backup_dir.'/'.$mail_backup_file.' --directory '.$this->tmp_backup_dir.' backup && rm -rf '.$this->tmp_backup_dir.'/backup'), $tmp_output, $retval);
133+
if ($use_pigz) {
134+
exec('tar pcf - --directory '.escapeshellarg($this->tmp_backup_dir).' backup | pigz > '.$mail_backup_dir.'/'.$mail_backup_file.' && rm -rf '.$this->tmp_backup_dir.'/backup', $tmp_output, $retval);
135+
} else {
136+
exec(escapeshellcmd('tar pczf '.$mail_backup_dir.'/'.$mail_backup_file.' --directory '.$this->tmp_backup_dir.' backup && rm -rf '.$this->tmp_backup_dir.'/backup'), $tmp_output, $retval);
137+
}
128138
}
129139

130140
if ($retval != 0) {
@@ -144,11 +154,15 @@ public function onRunJob() {
144154
//* create archives
145155
if($backup_mode == 'userzip') {
146156
$mail_backup_file.='.zip';
147-
exec('cd '.$domain_dir.' && zip '.$mail_backup_dir.'/'.$mail_backup_file.' -b /tmp -r '.$source_dir.' > /dev/null', $tmp_output, $retval);
157+
exec('cd '.$domain_dir.' && zip '.$mail_backup_dir.'/'.$mail_backup_file.' -b '.escapeshellarg($backup_tmp).' -r '.$source_dir.' > /dev/null', $tmp_output, $retval);
148158
} else {
149159
/* Create a tar.gz backup */
150160
$mail_backup_file.='.tar.gz';
151-
exec(escapeshellcmd('tar pczf '.$mail_backup_dir.'/'.$mail_backup_file.' --directory '.$domain_dir.' '.$source_dir), $tmp_output, $retval);
161+
if ($use_pigz) {
162+
exec('tar pcf - --directory '.escapeshellarg($domain_dir).' '.escapeshellarg($source_dir).' | pigz > '.$mail_backup_dir.'/'.$mail_backup_file, $tmp_output, $retval);
163+
} else {
164+
exec(escapeshellcmd('tar pczf '.$mail_backup_dir.'/'.$mail_backup_file.' --directory '.$domain_dir.' '.$source_dir), $tmp_output, $retval);
165+
}
152166
}
153167
}
154168

0 commit comments

Comments
 (0)