RSS

Perl MySQL backup script.

Mon, Sep 11, 2006

System Administration

I’m in the middle of migrating a lot of websites from one server to another. Here’s a small script I knocked up to take a dump of all the MySQL databases on that machine:

[perl]#!/usr/bin/perl -w
use strict;

my $dumpDir = “/tmp/foo”;
my $dumpUser= “root”;
my $mysqlBin = “/usr/local/mysql/bin”;
my $mysqlPassword = “bar”;

foreach (`ls -F /usr/local/mysql/var`)
{
if (/\//)
{
chomp;
s/\/$//; # remove the trailing / now
my $dumpFile = “$dumpDir/$_.sql”;
my $dumpCommand = “$mysqlBin/mysqldump -celqF –password=$mysqlPassword $_ > $dumpFile”;
system $dumpCommand; # or warn “mysqlDump of $_ failed”;
}
}[/perl]

Works for me.

This post was written by:

Bealers - who has written 352 posts on Darren Beale.


Contact the author

Leave a Reply