#! /usr/bin/perl # mysql-backup.pl # Dump and gzip each MySQL database. use warnings; use strict; my $dbhost = "localhost"; my $dbuser = "root"; my $dbpass = "easy1"; my $date = "`date +%Y-%m-%d`"; my $path = "/path/to/dbbpdir"; my $ext = "gz"; &clean_up($path,$ext); my($dbh,$sth,$query); $dbh = (&connect($dbhost,$dbuser,$dbpass)); $query = qq^SHOW DATABASES^; $sth = $dbh->prepare($query); $sth->execute(); while(my $ref = $sth->fetchrow_array()) { my @bp = `mysqldump --user=$dbuser --password=$dbpass --add-drop-t +able $ref | gzip > $path/$ref.$date.$ext`; } $sth->finish(); $dbh->disconnect(); # - - - - - - - - - - - - - - - - - sub connect() { use DBI; my $hostname = shift; my $dbuser = shift; my $dbpass = shift; my $dsn = "DBI:mysql:host=$hostname"; DBI->connect($dsn,$dbuser,$dbpass,{PrintError => 0, RaiseError => 1 +}); } sub clean_up() { $path = shift; $ext = shift; opendir(BP_DIR,"$path") or die "Cannot open $path: $!\n"; my @old_backups = grep { /\.$ext$/ } readdir BP_DIR; closedir(BP_DIR); foreach (@old_backups) { my @args = ("rm","-f","$path/$_"); system(@args); } }