G'day Voytek,

In a spirit of defensive programming, I have an important question for you.
What happens if the mysqldump fails?  Perhaps the database goes offline part way
through, perhaps the disk fills up... since you've just deleted your old backup,
what are you going to do?

I suggest the following changes to your code:
        * Take the backup first
        * Check that it didn't fail
        * Then do compression
        * Check that it didn't fail (find out the possible failure modes,
          can it corrupt your file?  What happens if all the disk space vanishes
          half way through?
        * Then, when you're fairly certain that things will be okay, delete the
          old version.


while( my ($ignored, $database) = $sth->fetchrow() ) {
        print "$database\n";

        # Make the backup
        system("mysqldump --opt $database -u $DB_User --password=$DB_Password ".
               " > $backuppath/$database-$year$month$day.sql");

        # Handle any errors
        if($?) {
                # something went wrong... try to guess what.
                die "some error as appropriate";
        }

        # Do the compression
        system("gzip", "$backuppath/$database-$year$month$day.sql");
        
        # Handle any errors
        if($?) {
                # something went wrong... try to guess what.
                die "some error as appropriate";
        }

        # Now that you're fairly certain that the new backup has worked...
        unless( unlink("$backuppath/$database-$oldyear$oldmonth$oldday.sql.gz");
                # something went wrong... try to guess what.
                # This could just be that the file doesn't exist.
                die "if you think it's appropriate";
        }
}



It might also be worth using chdir to change into $backuppath so that you don't
need to keep prepending that:

        chdir $backuppath or die "Failed to chdir to $backuppath $!";

You probably also want to wonder why you're pulling out a column from the
database and then ignoring it.

Whether or not it's better to use system or a module to do your compression
depends on a few things, like how easy it is to get modules installed, how easy
it is to use the module you select.  If you do use a module you're more likely
to have intuitive error handling rather than having to check one of Perl's very
super extra special variables.

All the best,

        Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | [EMAIL PROTECTED] |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to