repcsi has asked for the wisdom of the Perl Monks concerning the following question:
I'm a newbie to perl, and I'm trying to create a script to backup a mysql database every hour from 6:00 AM to 24:00, the script would run from crontab hourly. My worst enemy is the two paragraphs where I try to unlink files from the daily dir that are older than 31 days, and from the weekly that are older than 365.
Also Usually I can't transform my scripts to use strict... :(
What the script does alright (without the last two unlink part) is that it will do only one mysql dump in one hour no matter how many times do you start it, if its the first day in the week and ran in the 6:00 - 6:59 time period the backup will be put in the ./backup/weekly dir, if it's not monday but in the same period it will be created in the ./backup/daily dir. Every other case the dump will placed in ./backup/
I also would like to clean out files from ./backup/daily after 31 days , ./backup/weekly after 365days and from ./backup/ that are older than one day. But this is not working...
My error message (apparently I can't use Dirhandles as well): If the dump file does not exist for this hour(I get most of it because perl warns me about the vars, but I don't get why do I receive the error about "if (-e file)":
~/perl/sandbox]$ ./newtime.pl Name "main::backup_weekly_subdir" used only once: possible typo at ./n +ewtime.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 ./ne +wtime.pl line 7. Name "main::yday" used only once: possible typo at ./newtime.pl line 1 +7. 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.</ +p>
My error message: If the file does exist for this hour:
Name "main::backup_weekly_subdir" used only once: possible typo at ./n +ewtime.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 ./ne +wtime.pl line 7. Name "main::yday" used only once: possible typo at ./newtime.pl line 1 +6. 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.
What am I missing? :|
#!/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_' . g +et_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(t +ime); $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 a +ccount --password=$backup_password > $unique_daily_filename"); } elsif ($hour == 06){ system("mysqldump --user=$backup_user --add-drop-table --databases a +ccount --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 a +ccount --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);
My updated code:
#!/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_' . g +et_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(t +ime); $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 a +ccount --password=$backup_password > $unique_daily_filename"); } elsif ($hour == 06){ system("mysqldump --user=$backup_user --add-drop-table --databases a +ccount --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 a +ccount --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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Need help with mysql dump script
by ww (Archbishop) on Aug 17, 2011 at 21:09 UTC | |
|
Re: Need help with mysql dump script
by jethro (Monsignor) on Aug 17, 2011 at 21:24 UTC | |
|
Re: Need help with mysql dump script
by repcsi (Initiate) on Aug 17, 2011 at 22:58 UTC | |
by toolic (Bishop) on Aug 18, 2011 at 00:56 UTC | |
by repcsi (Initiate) on Aug 18, 2011 at 14:57 UTC | |
by jethro (Monsignor) on Aug 19, 2011 at 12:06 UTC |