#!/usr/local/bin/perl use warnings; $backup_user = "backupuser"; $backup_password = "backuppassword"; $backup_subdir = "./backup"; $backup_daily_subdir = './'."$backup_subdir".'/daily'; $backup_weekly_subdir = './'."$backup_subdir".'/weekly'; $unique_filename = "$backup_subdir".'/account_backup_' . get_timestamp().'.sql'; $unique_daily_filename = "$backup_subdir".'/daily/account_backup_' . get_timestamp().'.sql'; $unique_weekly_filename = "$backup_subdir".'/weekly/account_backup_' . get_timestamp().'.sql'; sub get_timestamp { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $mon += 1; if ($mon < 10) { $mon = "0$mon"; } if ($hour < 10) { $hour = "0$hour"; } if ($min < 10) { $min = "0$min"; } if ($sec < 10) { $sec = "0$sec"; } $year=$year+1900; return $year . '_' . $mon . '_' . $mday . '-' . $hour . '_' . $min . '_' . $sec; } if (($hour > 5)||($hour < 24)){ my $oldfile = "./backup/account_backup\_$year\_$mon\_".($mday - 1)."\-$hour\_*"; my $testfile = "./backup/account_backup\_$year\_$mon\_$mday\-$hour\_*"; if (($hour == 06)&&($wday == 1)){ system("mysqldump --user=$backup_user --add-drop-table --databases account --password=$backup_password > $unique_daily_filename"); } elsif ($hour == 06){ system("mysqldump --user=$backup_user --add-drop-table --databases account --password=$backup_password > $unique_weekly_filename"); } if (-e glob($testfile)){ print "The file for this hour already exists!\n"; } else { system("mysqldump --user=$backup_user --add-drop-table --databases account --password=$backup_password > $unique_filename"); } } else { print "It's not configured to create a backup at this hour!\n"; } #### #### Check for expired backups #### opendir (DH, "./backup/daily"); foreach (readdir(DH)) { next if (/^\./); $age = -M ; if ($age > 31) { push(@daily_goners, $_); } } unlink(@daily_goners); opendir (DH,"./backup/weekly"); foreach (readdir(DH)) { next if (/^\./); $age = -M ; if ($age > 365) { push(@weekly_goners, $_); } } unlink(@weekly_goners);