I wrote a script to back up the individual MySQL databases on my server.
Basicly it looks for any directories in /var/lib/mysql that are not "." or "..". It assumes the direcory names will match the DB names (which works so far).
It then does a while loop through the names (I have also tried a foreach loop). And for each directory it runs mysqldump. When I did test runs with just printing what it should do, it loops through perfectly. But if I either do and "exec" or use "open" to run mysqldump, it acts weird. Basicly it does it dumps the first db successfully and then exits the loop like if there was a "return(0)" being issued, but there isn't. The mysqldump is ending prperly, or it would trigger the die statement, but if I put a print after the dump, it never prints.
Any idea what is going wrong?
#!/usr/bin/perl # # A script that looks in the mysql data dir, finds all the database names, # and then does a dump for each individual db. # # # Config options: # The mysqldump executable $mysqldump = "/usr/bin/mysqldump"; # The mysql back-up account $user = "backup"; # Directory with all the mysql datafiles $mysqldatadir = "/var/lib/mysql"; # Where the dump files should be kept. $dumpdir = "/web/systems/mysqlbackups"; # The date. @time = localtime(time); $day = $time[3]; $month = $time[4]; $year = 1900 + $time[5]; # # The program... Of DOOM!!!! opendir(DIR,$mysqldatadir) || die "Can not open ".$mysqldatadir.".\n"; while ( $name = readdir(DIR) ) { if ( -d $mysqldatadir."/".$name && $name ne "." && $name ne ".." ) { &dumper($name); } } closedir(DIR); sub dumper(){ my $db = $_[0]; # print STDOUT $mysqldump." -u ".$user." ".$db." > ".$dumpdir."/".$year."-".$month."-".$day."-".$db.".sql\n"; # exec $mysqldump." -u ".$user." ".$db." > ".$dumpdir."/".$year."-".$month."-".$day."-".$db.".sql" or die "Back-up of ".$db." failed.\n"; # print STDOUT "WTF?!?!?!\n"; open(MYSQLDUMP,$mysqldump." -u ".$user." ".$db."|"); open(BACKUP,">".$dumpdir."/".$year."-".$month."-".$day."-".$db.".sql"); while(){ print BACKUP $_; } close(BACKUP); close(MYSQLDUMP); }