mongo-ec2-backup 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/Rakefile +1 -1
  2. data/bin/ec2_snapshot_restorer +60 -20
  3. metadata +2 -2
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ task :default => :package
8
8
 
9
9
  spec = Gem::Specification.new do |s|
10
10
  s.name = 'mongo-ec2-backup'
11
- s.version = '0.0.7'
11
+ s.version = '0.0.8'
12
12
  s.summary = 'Snapshot your mongodb in the EC2 cloud via XFS Freeze'
13
13
 
14
14
  s.author = 'Pierre Baillet'
@@ -1,12 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'rubygems'
4
- require 'bundler/setup'
5
4
  require 'trollop'
6
5
  require 'fog'
7
6
  require 'open-uri'
8
-
7
+ require 'trollop'
8
+ require 'pp'
9
9
  $: << File.join(File.dirname(__FILE__), "../lib")
10
+ require 'ec2_instance_identifier'
11
+
10
12
 
11
13
  DEBUG=false
12
14
 
@@ -69,26 +71,55 @@ class SnapshotRestorer
69
71
 
70
72
  @volumes << volume
71
73
  end
72
- def rattach_volumes(base_device = nil)
73
- dest = base_device
74
- if !dest
75
- dest = @volumes.map{ |v| v.tags['sdevice']}.min
76
- end
77
- dest = dest.dup
74
+ end
75
+ def rattach_volumes(base_device = nil)
76
+ dest = base_device
77
+ if !dest
78
+ dest = @volumes.map{ |v| v.tags['sdevice']}.min
79
+ end
80
+ dest = dest.dup
78
81
 
79
- @volumes.each do |vol|
80
- vol.reload
81
- puts "Attaching #{vol.id} to #{dest} on #{vol.tags['dinstance']}"
82
- @compute.attach_volume(vol.tags['dinstance'], vol.id, dest)
83
- dest.next!
82
+ @volumes.each do |vol|
83
+ vol.reload
84
+ puts "Attaching #{vol.id} to #{dest} on #{vol.tags['dinstance']}"
85
+ @compute.attach_volume(vol.tags['dinstance'], vol.id, dest)
86
+ dest.next!
87
+ end
88
+ end
89
+ def find_volume_for_instance_and_snapshots(dest_instance)
90
+ vols = []
91
+ @snaps.each do | resource_id |
92
+ @compute.volumes.all().each do |volume|
93
+ if volume.server_id == dest_instance &&
94
+ volume.snapshot_id == resource_id
95
+ vols << volume
96
+ end
84
97
  end
85
98
  end
99
+ return vols
100
+ end
101
+ def detach_snapshots(dest_instance)
102
+ vols = find_volume_for_instance_and_snapshots(dest_instance)
103
+ if vols.length == 0
104
+ puts "Nothing to detach!"
105
+ return
106
+ end
107
+ vols.each { |v|
108
+ v.force_detach()
109
+ }
110
+ puts "Waiting for volumes to be detached"
111
+ vols.each do |v|
112
+ begin
113
+ sleep(3)
114
+ v.reload
115
+ end while v.state == 'in-use'
116
+ puts "#{v.id} is detached"
117
+ v.destroy()
118
+ end
86
119
  end
87
120
  end
88
121
 
89
- require 'trollop'
90
- require 'ec2_instance_identifier'
91
- require 'pp'
122
+
92
123
  opts = Trollop::options do
93
124
  opt :hostname, "Hostname tag to use to find the instance", :type => :string, :required => true
94
125
  opt :access_key_id, "Access Key Id for AWS", :type => :string, :required => true
@@ -97,6 +128,7 @@ opts = Trollop::options do
97
128
  opt :type, "Snapshot type to restore, defaults to snapshot", :type => :string, :default => 'snapshot'
98
129
  opt :target, "Creates volume ready for mounting on instance id. Use special value SELF to restore here", :type => :string
99
130
  opt :first_device, "First device to attach to (default is to use source first device) /dev/sdx", :type => :string
131
+ opt :action, 'Action', :default => 'attach'
100
132
  end
101
133
 
102
134
  finder = EC2InstanceIdentifier.new(opts[:access_key_id], opts[:secret_access_key])
@@ -120,10 +152,18 @@ else
120
152
  s.snaps = snaps[opts[:date]].map{ |s| s.id }
121
153
  target = opts[:target]
122
154
  target = open("http://169.254.169.254/latest/meta-data/instance-id").read if target == "SELF"
123
- puts "Preparing volumes for instance #{target}"
124
- s.prepare_volumes(target)
125
- # Need to clone, because trollop freeze the variable
126
- s.rattach_volumes(opts[:first_device])
155
+ if opts[:action] == 'attach'
156
+ if !opts[:first_device]
157
+ raise "First device has to be indicated"
158
+ end
159
+ puts "Preparing volumes for instance #{target}"
160
+ s.prepare_volumes(target)
161
+ # Need to clone, because trollop freeze the variable
162
+
163
+ s.rattach_volumes(opts[:first_device])
164
+ elsif opts[:action] == 'destroy'
165
+ s.detach_snapshots(target)
166
+ end
127
167
  end
128
168
  end
129
169
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo-ec2-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-20 00:00:00.000000000 Z
12
+ date: 2012-09-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fog