#!/usr/bin/perl # settings $SnapDir="/snapshots/mysql-data"; $SnapDev="/dev/vg/mysql-data-snapshot"; $MySQLDev="/dev/vg/mysql-data"; system ("touch /root/.backup_start.timestamp"); # determine verbosity settings if ($ARGV[0] eq "-v") { $Verbose="y"; $RsyncParams="-vaxSH --progress --delete"; $MountParams="-v -t ext3 -o ro,noatime,nodiratime,noexec,nosuid"; $UMountParams="-v"; } else { $Verbose="n"; $RsyncParams="-axSH --delete"; $MountParams="-t ext3 -o ro,noatime,nodiratime,noexec,nosuid"; $UMountParams=""; } # connect to the database use DBI; $DB=DBI->connect("DBI:mysql:mysql" . ";mysql_read_default_file=/root/.my.cnf", root, $password) or die "Could not connect to MySQL\n"; # flush and lock all tables $DB->do("flush tables with read lock;") or die "Can't flush and lock tables: ",$DB->errstr,"\n"; # create the LVM snapshot device system ("lvcreate -L10G -s -n mysql-data-snapshot $MySQLDev > /dev/null 2>&1") == 0 or die "Can't create snapshot: $?\n"; # unlock the tables so MySQL can continue serving clients $DB->do("unlock tables"); $DB->disconnect; # mount up the snapshot volume system ("mount $MountParams $SnapDev $SnapDir") == 0 or die "Unable to mount snapshot: $?\n"; # figure out where we are backing up to # output currently looks like: # rmbackup@mysql01.futurequest.net:/backup/mysql/mysql17/ # however the rmbackup user is being deprecated. $RemPath=`/usr/local/mysql-data/zbin/buddy_rempath.csh`; chomp $RemPath; $Rempath =~ s/^rmbackup@//; # make sure the RemPath is sane... if ($RemPath !~ /mysql...futurequest.net:\/backup\/mysql\/mysql..\//) { system ("umount $UMountParams $SnapDir"); system ("lvremove -f $SnapDev > /dev/null"); die "$RemPath does not look sane.\n"; } # do the actual backup system ("rsync $RsyncParams $SnapDir/ $RemPath") == 0 or die "Can't rsync the databases: $?\n"; # Unmount the snapshot system ("umount $UMountParams $SnapDir") == 0 or die "Can't unmount snapshot: $?\n"; # Delete the snapshot volume system ("lvremove -f $SnapDev > /dev/null") == 0 or die "Can't remove the snapshot volume: $?\n"; if ($Verbose eq "y") { print "Backup completed.\n"; } system ("touch /root/.backup_complete.timestamp");