#!/usr/bin/perl -w
use DBI;

BEGIN {
  $ENV{PATH} = '/bin:/usr/bin:/usr/local/bin:/opt/ec2-api-tools/bin';                                                                                                                                                                                                          
  $ENV{JAVA_HOME} = '/opt/java';                                                                                                                                                                                                                                               
  $ENV{EC2_HOME} = '/opt/ec2-api-tools';                                                                                                                                                                                                                                       
  $ENV{EC2_PRIVATE_KEY} = '/path/to/pk-file.pem';                                                                                                                                                                                                     
  $ENV{EC2_CERT} = '/path/to/cert-file.pem';
}                                                                                                                                                                                                                                                                              

my $volume_id = 'vol-12345678';                                                                                                                                                                                                                                                
my $mountpoint = '/path/to/mysqldatavol/mnt/';

# Get the MySQL username/password from $HOME/.my.cnf and connect                                                                                                                                                                                                               
my ($username, $password);                                                                                                                                                                                                                                                     
open(FH, "/etc/mysql-$replica_id/debian.cnf") or die "Unable to open debian.cnf: $!";                                                                                                                                                                                          
while ( defined (my $line = <FH>) ) {                                                                                                                                                                                                                                          
  $username = $1 if $line =~ m%^\s*user\s*=\s*(\S+)%;                                                                                                                                                                                                                          
  $password = $1 if $line =~ m%^\s*password\s*=\s*(\S+)%;                                                                                                                                                                                                                      
  $socket = $1 if $line =~ m%^\s*socket\s*=\s*(\S+)%;                                                                                                                                                                                                                          
}                                                                                                                                                                                                                                                                              
close(FH);                                                                                                                                                                                                                                                                     
my $dbh = DBI->connect("DBI:mysql:;host=localhost;mysql_socket=$socket", $username, $password);                                                                                                                                                                                
                                                                                                                                                                                                                                                                               
# shutdown,remount                                                                                                                                                                                                                                                             
$dbh->do(q{  STOP SLAVE });                                                                                                                                                                                                                                                    
my @slavestatus = $dbh->selectrow_array(q{  SHOW SLAVE STATUS  });                                                                                                                                                                                                             
my $slavelog = $slavestatus[5];                                                                                                                                                                                                                                                
my $slavepos = $slavestatus[6];                                                                                                                                                                                                                                                
open(STATUSFILE, ">> $mountpoint/master_status.snapshot.txt");                                                                                                                                                                                                                 
print STATUSFILE "master_log_file=\"$slavelog\", master_log_pos=$slavepos\n";                                                                                                                                                                                                  
close (STATUSFILE);                                                                                                                                                                                                                                                            
system("/etc/init.d/mysql stop");
system("/bin/mount -o remount,ro $mountpoint");

# Snapshot
my $snapshot_output = `ec2-create-snapshot $volume_id`;

# Unlock
system("/bin/mount -o remount,rw $mountpoint");
system("/etc/init.d/mysql start");

