public
Description: A set of extra command line tools for Amazon EC2
Homepage: http://blog.carlmercier.com
Clone URL: git://github.com/cmer/ec2-extra-tools.git
ehaselwanter (author)
Thu Mar 05 10:13:45 -0800 2009
cmer (committer)
Thu Mar 05 15:51:21 -0800 2009
ec2-extra-tools / ec2-xfs-snapshot
100755 50 lines (40 sloc) 1.202 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/ruby -w
 
# Creates an EBS snapshot of an XFS volume by properly freezing the volume
# first to ensure data consistency.
 
class NilClass; def empty?; true; end; end
 
if ARGV[0].empty? or ARGV[0].count("/") < 2 or ARGV[0] == "--help" or ARGV[0] == "-h"
  puts "ec2-xfs-snapshot <devicename>\nexample: ec2-xfs-snapshot /dev/sdj"
  exit 1
end
 
def exec(cmd)
  out = `#{cmd}`
  raise "'#{cmd}' did not exit gracefully." if $? != 0
  return out
end
 
def get_mountpoint(device)
  out = `mount`
  out.each_line do |l|
    return l.split(" ")[2] if l.index(device) == 0
  end
  raise "Mount point not found for device #{device}"
end
 
begin
  device = ARGV[0]
  puts "device: #{device}"
  
  mountpoint = get_mountpoint(device)
  puts "mount point: #{mountpoint}"
  
  ec2_volume_id = exec("ec2-identify-volume #{device}")
  puts "ec2-volume-id: #{ec2_volume_id}"
  
  puts ""
  puts "Freezing XFS volume..."
  puts exec("xfs_freeze -f #{mountpoint}")
  puts "Creating snapshot..."
  puts exec("ec2-create-snapshot #{ec2_volume_id}")
rescue Exception=>ex
  puts "EXCEPTION: #{ex.message}"
  exit 1
ensure
  puts "Unfreezing XFS volume..."
  puts exec("xfs_freeze -u #{mountpoint}")
  puts "Done!"
end