Guest User

Untitled

a guest
Jun 4th, 2018
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #
  2. # Amazon EC2 API tools should be installed on the instance,
  3. # especially /usr/local/bin/ec2-create-snapshot
  4. #
  5. # MySQL username and password are stored in $HOME/.my.cnf in the
  6. # usual manner following this format:
  7. #
  8. # [client]
  9. # user=MYUSERNAME
  10. # password=MYPASSWORD
  11. #
  12. # Author:
  13. #
  14. # Eric Hammond <ehammond@thinksome.com>
  15. #
  16. use strict;
  17. use DBI;
  18.  
  19. BEGIN {
  20. $ENV{PATH} = '/bin:/usr/bin:/usr/local/bin:/root/bin/ec2-api-tools-1.3-30349/bin';
  21. }
  22.  
  23. my ($mountpoint, $volume_id) = @ARGV;
  24. die "Usage: $0 XFSMOUNTPOINT VOLUMEID" unless $mountpoint && $volume_id;
  25.  
  26. # Get the MySQL username/password from $HOME/.my.cnf and connect
  27. my ($username, $password);
  28. open(FH, "$ENV{HOME}/.my.cnf") or die "Unable to open .my.cnf: $!";
  29. while ( defined (my $line = <FH>) ) {
  30. $username = $1 if $line =~ m%^\s*user\s*=\s*(\S+)%;
  31. $password = $1 if $line =~ m%^\s*password\s*=\s*(\S+)%;
  32. }
  33. close(FH);
  34. my $dbh = DBI->connect('DBI:mysql:;host=localhost', $username, $password);
  35.  
  36. # Flush, lock, freeze
  37. $dbh->do(q{ FLUSH TABLES WITH READ LOCK });
  38. my ($logfile, $position) = $dbh->selectrow_array(q{ SHOW MASTER STATUS });
  39. system('sudo', 'xfs_freeze', '-f', $mountpoint);
  40.  
  41. # Snapshot
  42. my $snapshot_output = `/root/bin/ec2-api-tools-1.3-30349/bin/ec2-create-snapshot $volume_id`;
  43. my ($snapshot_id) = ($snapshot_output =~ m%(snap-[0-9a-f]{8})%);
  44.  
  45. # Thaw, unlock
  46. system('sudo', 'xfs_freeze', '-u', $mountpoint);
  47. $dbh->do(q{ UNLOCK TABLES });
  48.  
  49. print $snapshot_id ?
  50. "$snapshot_id (master_log_file=\"$logfile\", master_log_pos=$position)\n"
  51. :
  52. "snapshot FAILED\n";
Add Comment
Please, Sign In to add comment