~/perl/sandbox]$ ./newtime.pl Name "main::backup_weekly_subdir" used only once: possible typo at ./newtime.pl line 8. Name "main::isdst" used only once: possible typo at ./newtime.pl line 17. Name "main::backup_daily_subdir" used only once: possible typo at ./newtime.pl line 7. Name "main::yday" used only once: possible typo at ./newtime.pl line 17. Use of uninitialized value in -e at ./newtime.pl line 36. Use of uninitialized value $age in numeric gt (>) at ./newtime.pl line 53. readdir() attempted on invalid dirhandle DH at ./newtime.pl line 60.
####
Name "main::backup_weekly_subdir" used only once: possible typo at ./newtime.pl line 8.
Name "main::isdst" used only once: possible typo at ./newtime.pl line 16.
Name "main::backup_daily_subdir" used only once: possible typo at ./newtime.pl line 7.
Name "main::yday" used only once: possible typo at ./newtime.pl line 16.
The file for this hour already exists!
Use of uninitialized value $age in numeric gt (>) at ./newtime.pl line 52.
readdir() attempted on invalid dirhandle DH at ./newtime.pl line 59.
####
#!/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);
####
#!/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 < 20)){
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 (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
####
die unless opendir DIR, "$backup_daily_subdir";
foreach $file (grep {-f && (31 < -M)} readdir DIR) {
unlink $file;
}
closedir DIR;
die unless opendir DR, "$backup_weekly_subdir";
foreach $files (grep {-f && (365 < -M)} readdir DR) {
unlink $files;
}
closedir DR;