#!/usr/bin/perl $SnapDir="/snapshots/mysql"; $SnapDev="/dev/asylum/mysql_snapshot"; $SnapSize="1G"; $MySQLDev="/dev/asylum/mysql"; $OnOff=$ARGV[0]; chomp $OnOff; if ($ARGV[1] eq "-v" || $ARGV[1] eq "-v\n") { $Verbose="y"; $MountParams="-v -t ext4 -o ro,noatime,nodiratime,noexec,nosuid,nodev,barrier=0"; $UMountParams="-v"; } else { $Verbose="n"; $MountParams="-t ext4 -o ro,noatime,nodiratime,noexec,nosuid,nodev,barrier=0"; $UMountParams=""; } if ($OnOff eq "on") { 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 --size $SnapSize --snapshot --name $SnapDev $MySQLDev > /dev/null 2>&1") == 0 or die "Can't create snapshot: $?\n"; # unlock the tables so MySQL can continue serving clients # After this we don't have to talk to MySQL at all anymore. $DB->do("unlock tables"); $DB->disconnect; # mount up the snapshot volume system ("mount $MountParams $SnapDev $SnapDir") == 0 or die "Unable to mount snapshot: $?\n"; } elsif ($OnOff eq "off") { # Unmount the snapshot system ("umount $UMountParams $SnapDir") == 0 or die "Can't unmount snapshot: $?\n"; # Delete the snapshot volume system ("lvremove --force $SnapDev > /dev/null 2>&1") == 0 or system ("lvremove --force $SnapDev > /dev/null") == 0 or die "Can't remove the snapshot volume: $?\n"; } else { die "Invalid parameter used on command line.\n"; }