in reply to Need help with mysql dump script

Now I updated my code and it works! :) Thanks! I found another way to delete files that are older than x days, however, something strange came up... I don't know why it happened, but if I use the second delete section like this:

#### #### Check for expired backups #### print $backup_daily_subdir."\n"; die unless chdir "$backup_daily_subdir"; die unless opendir DIR, "."; foreach $file (grep {-f && (31 < -M)} readdir DIR) { unlink $file; } closedir DIR; print $backup_weekly_subdir."\n"; die unless chdir "$backup_weekly_subdir"; die unless opendir DR, "."; foreach $files (grep {-f && (365 < -M)} readdir DR) { unlink $files; } closedir DR;

I get this error:

~/perl/sandbox]$ ./newtime.pl Name "main::files" used only once: possible typo at ./newtime.pl line +59. Name "main::isdst" used only once: possible typo at ./newtime.pl line +16. Name "main::yday" used only once: possible typo at ./newtime.pl line 1 +6. The file for this hour already exists! Died at ./newtime.pl line 57.

Line 57 is -> die unless chdir "$backup_weekly_subdir";

I got rid of the chdir part to make it work and used only opendir, but why was this happening with chdir(i mean the die on line 57)? "./backup/weekly" was missing so you were right jethro, but I created it, and it was there, I even printed the variables and tried an ls command on it and it was working... I'm clueless, is this safe what I'm doing, or I can wipe something out that I don't want?

Replies are listed 'Best First'.
Re^2: Need help with mysql dump script
by toolic (Bishop) on Aug 18, 2011 at 00:56 UTC
      Hello, thanks, but I don't figure it out from this one:
      $ perldoc -f chdir chdir EXPR chdir FILEHANDLE chdir DIRHANDLE chdir Changes the working directory to EXPR, if possible. If +EXPR is omitted, changes to the directory specified by $ENV{HOM +E}, if set; if not, changes to the directory specified by $ENV{LOGDIR}. (Under VMS, the variable $ENV{SYS$LOGIN} +is also checked, and used if it is set.) If neither is set, "ch +dir" does nothing. It returns true on success, false otherwi +se. See the example under "die". On systems that support fchdir(2), you may pass a fileh +andle or directory handle as argument. On systems that don't su +pport fchdir(2), passing handles raises an exception. $

      Also modifing that line, is not working, also I figured, that the file removal is not working, only I just don't get any error messages:(.

        What toolic (probably) meant was the "example under "die"" mentioned in the docu of chdir. If you do "perldoc -f die" you will see these two examples:

        die "Can't cd to spool: $!\n" unless chdir '/usr/spool/news'; chdir '/usr/spool/news' or die "Can't cd to spool: $!\n"

        The two lines are equivalent (to each other and to the line toolic posted) and show how to use chdir, specifically the use of $! to print the error message that the operating system returned.

        The error message will tell you why it couldn't chdir. But to further explore the situation add

        use Cwd; #at the start of your script ... print "We are here: ",cwd(),"\n"; #test where you are in the directory + structure #alternatively this works too, but is not as portable: print "We are here:"; system('pwd');