| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | # ispCP ω (OMEGA) a Virtual Hosting Control Panel |
|---|
| 4 | # Copyright (c) 2001-2006 by moleSoftware GmbH |
|---|
| 5 | # http://www.molesoftware.com |
|---|
| 6 | # Copyright (c) 2006-2007 by isp Control Panel |
|---|
| 7 | # http://isp-control.net |
|---|
| 8 | # |
|---|
| 9 | # |
|---|
| 10 | # License: |
|---|
| 11 | # This program is free software; you can redistribute it and/or |
|---|
| 12 | # modify it under the terms of the MPL Mozilla Public License |
|---|
| 13 | # as published by the Free Software Foundation; either version 1.1 |
|---|
| 14 | # of the License, or (at your option) any later version. |
|---|
| 15 | # |
|---|
| 16 | # This program is distributed in the hope that it will be useful, |
|---|
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 19 | # MPL Mozilla Public License for more details. |
|---|
| 20 | # |
|---|
| 21 | # You may have received a copy of the MPL Mozilla Public License |
|---|
| 22 | # along with this program. |
|---|
| 23 | # |
|---|
| 24 | # An on-line copy of the MPL Mozilla Public License can be found |
|---|
| 25 | # http://www.mozilla.org/MPL/MPL-1.1.html |
|---|
| 26 | # |
|---|
| 27 | # |
|---|
| 28 | # The ispCP ω Home Page is at: |
|---|
| 29 | # |
|---|
| 30 | # http://isp-control.net |
|---|
| 31 | # |
|---|
| 32 | |
|---|
| 33 | use FindBin; |
|---|
| 34 | use lib "$FindBin::Bin/.."; |
|---|
| 35 | require 'ispcp_common_code.pl'; |
|---|
| 36 | |
|---|
| 37 | use strict; |
|---|
| 38 | |
|---|
| 39 | use warnings; |
|---|
| 40 | |
|---|
| 41 | sub lock_backup_ispcp_system { |
|---|
| 42 | |
|---|
| 43 | my ($lock_file) = @_; |
|---|
| 44 | |
|---|
| 45 | push_el(\@main::el, 'lock_backup_ispcp_system()', 'Starting...'); |
|---|
| 46 | |
|---|
| 47 | if ($main::cfg{'BACKUP_ISPCP'} ne 'yes') { |
|---|
| 48 | |
|---|
| 49 | push_el(\@main::el, 'lock_backup_ispcp_system()', 'NOTICE: domain backups are disabled'); |
|---|
| 50 | |
|---|
| 51 | return 2; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | if (-e $lock_file) { |
|---|
| 55 | |
|---|
| 56 | push_el(\@main::el, 'lock_backup_ispcp_system()', 'ERROR: backup request engine already locked !'); |
|---|
| 57 | |
|---|
| 58 | return 1; |
|---|
| 59 | |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | my $touch_cmd = "/bin/touch $lock_file"; |
|---|
| 63 | |
|---|
| 64 | my $rs = sys_command($touch_cmd); |
|---|
| 65 | |
|---|
| 66 | return 1 if ($rs != 0); |
|---|
| 67 | |
|---|
| 68 | push_el(\@main::el, 'lock_backup_ispcp_system()', 'Ending...'); |
|---|
| 69 | |
|---|
| 70 | return 0; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | sub unlock_backup_ispcp_system { |
|---|
| 75 | |
|---|
| 76 | my ($lock_file) = @_; |
|---|
| 77 | |
|---|
| 78 | push_el(\@main::el, 'unlock_backup_ispcp_system()', 'Starting...'); |
|---|
| 79 | |
|---|
| 80 | my $rs = del_file($lock_file); |
|---|
| 81 | |
|---|
| 82 | return $rs if ($rs != 0); |
|---|
| 83 | |
|---|
| 84 | push_el(\@main::el, 'unlock_backup_ispcp_system()', 'Ending...'); |
|---|
| 85 | |
|---|
| 86 | return 0; |
|---|
| 87 | |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | sub backup_ispcp_start_up { |
|---|
| 93 | |
|---|
| 94 | my ($lock_file) = @_; |
|---|
| 95 | |
|---|
| 96 | my ($rs, $rdata) = (undef, undef); |
|---|
| 97 | |
|---|
| 98 | push_el(\@main::el, 'backup_ispcp_start_up()', 'Starting...'); |
|---|
| 99 | |
|---|
| 100 | $rs = lock_backup_ispcp_system($lock_file); |
|---|
| 101 | |
|---|
| 102 | return $rs if ($rs != 0); |
|---|
| 103 | |
|---|
| 104 | # config check; |
|---|
| 105 | |
|---|
| 106 | $rs = get_conf(); |
|---|
| 107 | |
|---|
| 108 | return $rs if ($rs != 0); |
|---|
| 109 | |
|---|
| 110 | push_el(\@main::el, 'backup_ispcp_start_up()', 'Ending...'); |
|---|
| 111 | |
|---|
| 112 | return 0; |
|---|
| 113 | |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | sub backup_ispcp_shut_down { |
|---|
| 118 | |
|---|
| 119 | my ($lock_file) = @_; |
|---|
| 120 | |
|---|
| 121 | my $rs = undef; |
|---|
| 122 | |
|---|
| 123 | push_el(\@main::el, 'backup_ispcp_shut_down()', 'Starting...'); |
|---|
| 124 | |
|---|
| 125 | $rs = unlock_backup_ispcp_system($lock_file); |
|---|
| 126 | |
|---|
| 127 | return $rs if ($rs != 0); |
|---|
| 128 | |
|---|
| 129 | push_el(\@main::el, 'backup_ispcp_shut_down()', 'Ending...'); |
|---|
| 130 | |
|---|
| 131 | return 0; |
|---|
| 132 | |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | sub err_exit { |
|---|
| 136 | |
|---|
| 137 | my $el_data = pop_el(\@main::el); |
|---|
| 138 | |
|---|
| 139 | $main::el_sep = "\t#\t"; |
|---|
| 140 | |
|---|
| 141 | my ($sub_name, $msg) = split(/$main::el_sep/, $el_data); |
|---|
| 142 | |
|---|
| 143 | print STDERR "$msg\n"; |
|---|
| 144 | |
|---|
| 145 | exit 1; |
|---|
| 146 | |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | sub backup_ispcp_mail_task { |
|---|
| 150 | |
|---|
| 151 | my ($rs, $rdata, $sql) = (undef, undef, undef); |
|---|
| 152 | |
|---|
| 153 | push_el(\@main::el, 'backup_ispcp_mail_task()', 'Starting...'); |
|---|
| 154 | |
|---|
| 155 | my $date = get_human_date(); |
|---|
| 156 | |
|---|
| 157 | my $admin_email = $main::cfg{'DEFAULT_ADMIN_ADDRESS'}; |
|---|
| 158 | |
|---|
| 159 | my $backup_dir = $main::cfg{'BACKUP_FILE_DIR'}; |
|---|
| 160 | |
|---|
| 161 | my $backup_file_list = `ls -la $backup_dir`; |
|---|
| 162 | |
|---|
| 163 | my $server_name = $main::cfg{'SERVER_HOSTNAME'}; |
|---|
| 164 | |
|---|
| 165 | my $server_ip = $main::cfg{'BASE_SERVER_IP'}; |
|---|
| 166 | |
|---|
| 167 | my $msg_data = <<MESSAGE_TEXT; |
|---|
| 168 | Hey There, |
|---|
| 169 | |
|---|
| 170 | I'm the automatic backup system on your $server_name ($server_ip) server. |
|---|
| 171 | |
|---|
| 172 | Backup task was completed successfully! |
|---|
| 173 | |
|---|
| 174 | File(s) List In ($backup_dir): |
|---|
| 175 | |
|---|
| 176 | ======================================================================== |
|---|
| 177 | $backup_file_list |
|---|
| 178 | ======================================================================== |
|---|
| 179 | MESSAGE_TEXT |
|---|
| 180 | |
|---|
| 181 | my $out = new MIME::Entity; |
|---|
| 182 | |
|---|
| 183 | $out -> build( |
|---|
| 184 | From => "Automatic Backup Manager <".$admin_email.">", |
|---|
| 185 | To => $admin_email, |
|---|
| 186 | Subject => "[$date] Backup report.", |
|---|
| 187 | Data => $msg_data, |
|---|
| 188 | 'X-Mailer' => "ispCP $main::cfg{'VersionH'} Automatic Backup Messenger" |
|---|
| 189 | ); |
|---|
| 190 | |
|---|
| 191 | open MAIL, "| /usr/sbin/sendmail -t -oi"; |
|---|
| 192 | |
|---|
| 193 | $out -> print(\*MAIL); |
|---|
| 194 | |
|---|
| 195 | close MAIL; |
|---|
| 196 | |
|---|
| 197 | push_el(\@main::el, 'backup_ispcp_mail_task()', 'Ending...'); |
|---|
| 198 | |
|---|
| 199 | return 0; |
|---|
| 200 | |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | sub backup_ispcp_engine { |
|---|
| 205 | |
|---|
| 206 | my ($rs, $rdata, $sql) = (undef, undef, undef); |
|---|
| 207 | |
|---|
| 208 | my ($backup_filename, $backup_cmd) = (undef, undef); |
|---|
| 209 | |
|---|
| 210 | push_el(\@main::el, 'backup_ispcp_engine()', 'Starting...'); |
|---|
| 211 | |
|---|
| 212 | my $cmd_tar = $main::cfg{'CMD_TAR'}; |
|---|
| 213 | |
|---|
| 214 | my $cmd_rm = $main::cfg{'CMD_RM'}; |
|---|
| 215 | |
|---|
| 216 | my $cmd_mv = $main::cfg{'CMD_MV'}; |
|---|
| 217 | |
|---|
| 218 | my $zip = $main::cfg{ZIP}; |
|---|
| 219 | |
|---|
| 220 | my $date = get_human_date(); |
|---|
| 221 | |
|---|
| 222 | my $backup_dir = $main::cfg{'BACKUP_FILE_DIR'}; |
|---|
| 223 | |
|---|
| 224 | if ($zip eq "bzip2") { |
|---|
| 225 | $backup_filename = "config-backup-$date.tar.bz2"; |
|---|
| 226 | |
|---|
| 227 | $backup_cmd = "$cmd_tar --create --directory=/etc/ispcp --bzip2 --file=$backup_dir/$backup_filename . 2> $main::cfg{'LOG_DIR'}/$backup_filename.log"; |
|---|
| 228 | } |
|---|
| 229 | elsif ($zip eq "gzip") { |
|---|
| 230 | $backup_filename = "config-backup-$date.tar.gz"; |
|---|
| 231 | |
|---|
| 232 | $backup_cmd = "$cmd_tar --create --directory=/etc/ispcp --gzip --file=$backup_dir/$backup_filename . 2> $main::cfg{'LOG_DIR'}/$backup_filename.log"; |
|---|
| 233 | } |
|---|
| 234 | else { |
|---|
| 235 | push_el(\@main::el, 'backup_ispcp_engine()', "Backup algorithm not supported: $zip"); |
|---|
| 236 | |
|---|
| 237 | return 1; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | if(! -d $backup_dir) { |
|---|
| 242 | $rs = make_dir($backup_dir, 0, 0, 0750); |
|---|
| 243 | |
|---|
| 244 | return $rs if ($rs != 0); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | my $dbuser = $main::db_user; |
|---|
| 249 | my $dbpass = $main::db_pwd; |
|---|
| 250 | my $db_name = $main::cfg{'DATABASE_NAME'}; |
|---|
| 251 | |
|---|
| 252 | if ($dbuser && $dbpass) { |
|---|
| 253 | my $db_backup_file = "$backup_dir/$db_name.sql"; |
|---|
| 254 | |
|---|
| 255 | my $db_backupcmd = "$main::cfg{'CMD_MYSQLDUMP'} --add-drop-table --allow-keywords --quote-names -u\'$dbuser\' -p\'$dbpass\' \'$db_name\' >\'$db_backup_file\'"; |
|---|
| 256 | |
|---|
| 257 | my ($db_filename, $db_compresscmd) = (undef, undef); |
|---|
| 258 | |
|---|
| 259 | if ($zip eq "bzip2") { |
|---|
| 260 | $db_filename = "$db_backup_file.bz2"; |
|---|
| 261 | $db_compresscmd = "$main::cfg{'CMD_BZIP'} --force \'$db_backup_file\'"; |
|---|
| 262 | } |
|---|
| 263 | elsif ($zip eq "gzip") { |
|---|
| 264 | $db_filename = "$db_backup_file.gz"; |
|---|
| 265 | $db_compresscmd = "$main::cfg{'CMD_GZIP'} --force \'$db_backup_file\'"; |
|---|
| 266 | } |
|---|
| 267 | else { |
|---|
| 268 | push_el(\@main::el, 'backup_all_engine()', "Backup algorithm not supported: $zip"); |
|---|
| 269 | |
|---|
| 270 | return 1; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | $rs = sys_command($db_backupcmd); |
|---|
| 274 | |
|---|
| 275 | if ($rs == 0) { |
|---|
| 276 | $rs = setfmode("$db_backup_file", 0, 0, 0640); |
|---|
| 277 | |
|---|
| 278 | if ($rs != 0) { |
|---|
| 279 | unlink($db_backup_file); |
|---|
| 280 | } else { |
|---|
| 281 | $rs = sys_command($db_compresscmd); |
|---|
| 282 | return $rs if ($rs != 0); |
|---|
| 283 | } |
|---|
| 284 | return $rs if ($rs != 0); |
|---|
| 285 | } else { |
|---|
| 286 | push_el(\@main::el, 'backup_ispcp_engine()', "ERROR: Failed to backup database $db_name"); |
|---|
| 287 | unlink($db_backup_file); |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | $rs = sys_command($backup_cmd); |
|---|
| 292 | |
|---|
| 293 | # do not return if backup throws an error, otherwise other domains will not be backuped |
|---|
| 294 | if ($rs == 0) { |
|---|
| 295 | |
|---|
| 296 | $rs = sys_command("$cmd_rm -rf $main::cfg{'LOG_DIR'}/$backup_filename.log"); |
|---|
| 297 | |
|---|
| 298 | return $rs if ($rs != 0); |
|---|
| 299 | } elsif ( -e "$backup_dir/$backup_filename" ) { |
|---|
| 300 | |
|---|
| 301 | $rs = del_file("$backup_dir/$backup_filename"); |
|---|
| 302 | return $rs if ($rs != 0); |
|---|
| 303 | |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | sys_command("find $backup_dir/* -maxdepth 0 -type f -mtime +14 -print | xargs -r /bin/rm"); |
|---|
| 307 | |
|---|
| 308 | $rs = setfmode("$backup_dir/$backup_filename", 0, 0, 0640); |
|---|
| 309 | |
|---|
| 310 | return $rs if ($rs != 0); |
|---|
| 311 | |
|---|
| 312 | push_el(\@main::el, 'backup_ispcp_engine()', 'Ending...'); |
|---|
| 313 | |
|---|
| 314 | return 0; |
|---|
| 315 | |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | my $rs = undef; |
|---|
| 320 | |
|---|
| 321 | my $proceed = $ARGV[0]; |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | if (!defined($proceed) || $proceed eq '') { |
|---|
| 325 | |
|---|
| 326 | push_el(\@main::el, 'main()', "ERROR: Missing Input Data! Please provide appropriate command line parameter(s) (report|noreport)!"); |
|---|
| 327 | |
|---|
| 328 | err_exit(); |
|---|
| 329 | |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | if ($proceed ne 'report' && $proceed ne 'noreport' ) { |
|---|
| 333 | |
|---|
| 334 | push_el(\@main::el, 'main()', "NOTE: If you want to be notified when the backup is done, please run this script with 'report' as command line parameter, otherwise with 'noreport'"); |
|---|
| 335 | |
|---|
| 336 | err_exit(); |
|---|
| 337 | |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | my $backup_lock_file = "/tmp/ispcp-backup-ispcp.lock"; |
|---|
| 341 | |
|---|
| 342 | $rs = backup_ispcp_start_up($backup_lock_file); |
|---|
| 343 | |
|---|
| 344 | err_exit() if ($rs != 0 && $rs != 2); |
|---|
| 345 | |
|---|
| 346 | exit 0 if ($rs == 2); # $rs == 2 when backups are disabled |
|---|
| 347 | |
|---|
| 348 | $rs = backup_ispcp_engine(); |
|---|
| 349 | |
|---|
| 350 | err_exit() if ($rs != 0); |
|---|
| 351 | |
|---|
| 352 | if ($proceed eq 'report') { |
|---|
| 353 | |
|---|
| 354 | $rs = backup_ispcp_mail_task(); |
|---|
| 355 | |
|---|
| 356 | err_exit() if ($rs != 0); |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | $rs = backup_ispcp_shut_down($backup_lock_file); |
|---|
| 360 | |
|---|
| 361 | err_exit() if ($rs != 0); |
|---|
| 362 | |
|---|
| 363 | exit 0; |
|---|