Update:
Thanks to podmaster for mentioning this, there is a security flaw in so much as a ps could be run showing the password.
#!/usr/bin/perl -w use strict; # Ideally should be called daily using cron my $BAKDIR = '/path/to/bak/dir'; my @DBS = qw/foo bar baz/; my $DBUSER = 'user'; my $DBPASS = 'password'; my $DAYS_TILL_UNLINK = '31'; # Error traps unless (-e $BAKDIR) { mkdir ($BAKDIR, 0600) || die "Can't create $BAKDIR $!\n"; } unless (-d $BAKDIR) { die "$BAKDIR is unavailable\n"; } my ($day, $mon, $year) = (localtime(time))[3,4,5]; $mon += 1; $year += 1900; my $date = "$year.$mon.$day"; # Iterate through Dbs to be backed up foreach my $db (@DBS) { my $cmd = "mysqldump $db -u$DBUSER -p$DBPASS > $BAKDIR/dump.$db.$d +ate"; system($cmd) == 0 || die "Can't dump $db: $!\n"; } # Remove old backups if older than $DAYS_TILL_UNLINK chdir "$BAKDIR" || warn "Can't change directory to $BAKDIR $!\n"; my $db; $db = <dump.*>; while ($db = <dump.*>) { if (-M $db > $DAYS_TILL_UNLINK) { unlink ("$db"); } }
|
|---|