Простой скрипт резервного копирования(backup) файлов и базы данных MySQL на perl

#!/usr/bin/perl
 
die "
=======================================================================================================================
Simple files and MySQL database backup script
Usage:
    ./backup.pl STORAGE_FOLDER BACKUP_FOLDER DAILY_BACKUPS:WEEKLY_BACKUPS:MONLY_BACKUPS DB_USER:DB_PASS@DB_HOST/DB_NAME
        - STORAGE_FORDER - folder for backubs storing
        - BACKUP_FOLDER - backuped folder
        - DAILY_BACKUPS - daily backups quantity 
        - WEEKLY_BACKUPS - weekly backups quantity 
        - MONLY_BACKUPS - mounly backups quantity 
        - DB_USER - database username
        - DB_PASS - database password
        - DB_HOST - database host
        - DB_NAME -database name
Examples:
    ./backup.pl /var/backup /home/user01 7:4:10 user01:d7jshgGj\@localhost/mydb
=======================================================================================================================
" if !$ARGV[0] || $ARGV[0] eq '-h' || $ARGV[0] eq '--help' || $ARGV[0] eq '-?';
 
$STORAGE_FOLDER = $ARGV[0];
$BACKUP_FOLDER  = $ARGV[1];
$BACK_COUNTS    = $ARGV[2];
$DATABASE       = $ARGV[3];
 
($DAILY_BACKUPS,$WEEKLY_BACKUPS,$MONLY_BACKUPS) = split(":",$BACK_COUNTS);
 
($DB_USER,$DB_PASS,$DB_HOST,$DB_NAME) = ($1,$2,$3,$4) if $DATABASE=~m/^(.*?):(.*?)\@(.*?)\/(.*?)$/i;
 
die "ERROR: Storage folder not writable or not exists\n" unless -W $STORAGE_FOLDER;
die "ERROR: Backup folder not readable or not exists\n" unless -R $BACKUP_FOLDER;
die "ERROR: Daily backups not defined\n" unless $DAILY_BACKUPS=~m/^\d+$/;
die "ERROR: Weekly backups not defined\n" unless $WEEKLY_BACKUPS=~m/^\d+$/;
die "ERROR: Mounly backups not defined\n" unless $MONLY_BACKUPS=~m/^\d+$/;
die "ERROR: Database host not defined\n" unless $DB_HOST;                                                                                                                                       
die "ERROR: Database username not defined\n" unless $DB_USER;                                                                                                                                   
die "ERROR: Database password not defined\n" unless $DB_PASS;                                                                                                                                   
die "ERROR: Database name not defined\n" unless $DB_NAME;                                                                                                                                       
$mysql_error = `echo "quit"|mysql -u $DB_USER -p$DB_PASS -h $DB_HOST $DB_NAME 2>&1`;                                                                                                            
chomp($mysql_error);                                                                                                                                                                            
die $mysql_error if $mysql_error;                                                                                                                                                               
 
$STORAGE_FOLDER=~s/^(.*)\/\s*$/$1/i;
$BACKUP_FOLDER=~s/^(.*)\/\s*$/$1/i;
$BACKUP_NAME = $BACKUP_FOLDER;
$BACKUP_NAME=~s/^.*\/(.*)$/$1/i;
 
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon += 1;
$sec = "0".$sec if $sec < 10;
$min = "0".$min if $min < 10;
$hour = "0".$hour if $hour < 10;
$mday = "0".$mday if $mday < 10;
$mon = "0".$mon if $mon < 10;
$date = "$year-$mon-$mday-$hour-$min-$sec";
 
 
@files = sort glob ("$STORAGE_FOLDER/*[0-9].tgz");
$files_count = scalar(@files);
for($i=0;$i<$files_count-$DAILY_BACKUPS+1;$i++){unlink ($files[$i]);}
 
@files = sort glob ("$STORAGE_FOLDER/*[0-9].sql.bz2");
$files_count = scalar(@files);
for($i=0;$i<$files_count-$DAILY_BACKUPS+1;$i++){unlink ($files[$i]);} `tar cfz $STORAGE_FOLDER/$BACKUP_NAME.$date.tgz $BACKUP_FOLDER 2>&1`;
`mysqldump -u $DB_USER -p$DB_PASS -h $DB_HOST --add-drop-table $DB_NAME > $STORAGE_FOLDER/$DB_NAME.$date.sql`;
`bzip2 -9 $STORAGE_FOLDER/$DB_NAME.$date.sql`;
 
$wday=0;
if($wday==0){
    @files = sort glob ("$STORAGE_FOLDER/*.weekly.tgz");
    $files_count = scalar(@files);
    for($i=0;$i< $files_count-$WEEKLY_BACKUPS+1;$i++){unlink($files[$i]);}
    `cp -f $STORAGE_FOLDER/$BACKUP_NAME.$date.tgz $STORAGE_FOLDER/$BACKUP_NAME.$date.weekly.tgz`;
 
    @files = sort glob ("$STORAGE_FOLDER/*.weekly.sql.bz2");
    $files_count = scalar(@files);
    for ($i=0;$i<($files_count-$WEEKLY_BACKUPS+1);$i++){unlink($files[$i]);}
    `cp -f $STORAGE_FOLDER/$DB_NAME.$date.sql.bz2 $STORAGE_FOLDER/$DB_NAME.$date.weekly.sql.bz2`;
}
 
if($mday==0){
    @files = sort glob ("$STORAGE_FOLDER/*.mounly.tgz");
    $files_count = scalar(@files);
    for ($i=0;$i<$files_count-$MONLY_BACKUPS+1;$i++){unlink($files[$i]);}
    `cp -f $STORAGE_FOLDER/$BACKUP_NAME.$date.tgz $STORAGE_FOLDER/$BACKUP_NAME.$date.mounly.tgz`;
 
    @files = sort glob ("$STORAGE_FOLDER/*.mounly.sql.bz2");
    $files_count = scalar(@files);
    for($i=0;$i<$files_count-$WEEKLY_BACKUPS+1;$i++){unlink($files[$i]);}
    `cp -f $STORAGE_FOLDER/$DB_NAME.$date.sql.bz2 $STORAGE_FOLDER/$DB_NAME.$date.mounly.sql.bz2`;
}

Добавить комментарий