This is just a short script to utilise the mysqldump utility. The reason I am posting this is to double check with you Monks that I haven't inadvertantly made any boo-boos. My last posting resulted in many good points being raised that I hadn't even considered, and I always get nervous when I use the system() call {shudder}. So any feedback (•s etc) most welcome. I remain all ears and again wish to thank you all for all that I have learned to date.

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"); } }

In reply to *nix mySQL backup script using mysqldump by barrd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.