fallout 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58b93407bb2b48ce39c73c4dbcc3bc770260b32e
4
- data.tar.gz: d3bf1ff41fe648c677518404783c13bb1dd30bfe
3
+ metadata.gz: 4173f8e75da22b283ef91722f045e2a3dff4e20c
4
+ data.tar.gz: a390595d7d6db59a458e6a59c1e89d260a70e9f7
5
5
  SHA512:
6
- metadata.gz: 57ac6f64c67dc9db392c8ea2922eee7025ac906cd024c86c654072fe888cf002e4ea97efc28b4404ff07f5ce5947db2e687903a957d0f6dda31047bd5c0f8015
7
- data.tar.gz: 23c065ae1393c703e1e6fd9e37a84f0d77ddd41d5bd758ef5ac80672925c824f186f31e93e8cc83cc45652cc9cd999465aeb63364e1fdde7782ccd22a464d5c4
6
+ metadata.gz: 5fe8aab0a871b68b8abb6a2447c011f6bc666cea784c2af6fe28de3156569b9accdd758ca7027bf6238b0f7b117ad3537cc52c7b256f22f024cbb46f134fdd5d
7
+ data.tar.gz: 9240b4ee9b2951ad3a79ea6ffd435cd59337499ef8582cf90e0d86416e759f1d74a31aa63f27699ec181d0f72058ee07cfb983024bba49d7c6d884530eed3651
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fallout (0.0.1)
4
+ fallout (0.0.2)
5
5
  aws-sdk
6
6
  trollop
7
7
 
data/README.md CHANGED
@@ -44,7 +44,63 @@ running every day, it will keep N most fresh snapshots automatically.
44
44
 
45
45
  ### restore
46
46
 
47
- Work in progress
47
+ Restoring is a one-liner too.
48
+
49
+ Syntax:
50
+
51
+ `fallout restore -i <instance_id> -v <volume_id>`
52
+
53
+ Example:
54
+
55
+ ```
56
+ $ fallout restore -i i-f7850bda -v vol-72436837
57
+
58
+ Shutting down your instance.
59
+ Shut down instance successfully: i-f7850bda, status: stopped
60
+ Detached volume vol-72436837 from instance i-f7850bda successfully
61
+ Created new volume from the latest snapshot (vol-3942697c)
62
+ Attached new volume to instance
63
+ Successfully restored and started the instance with the new volume
64
+ Instance public hostname: ec2-54-165-20-157.compute-1.amazonaws.com
65
+ You man want to delete the old, detached volume vol-72436837 and old volume snapshots
66
+ IMPORTANT: You must update your backup command to use new volume_id: vol-3942697c
67
+ ```
68
+
69
+ #### How does restoring work?
70
+
71
+ Restoring with fallout requires you to have at least 1 snapshot for the
72
+ volume. The process will shutdown the instance, detach the root volume,
73
+ create new volume from the latest snapshot and attach it as `/dev/sda1`
74
+ device. Then the process will boot the instance with the new volume and
75
+ display its public hostname.
76
+
77
+ ##### IMPORTANT:
78
+
79
+ Currently the volume will be attached as `/dev/sda1` device and created
80
+ in `us-east-1a` zone.
81
+ If you need this stuff to be configurable, let me know, I'll implement
82
+ it.
83
+
84
+ #### General considerations:
85
+
86
+ I created this gem purely for my own purposes, we only have a handful of
87
+ EC2 instances and the gem is working fine for us.
88
+
89
+ I run `fallout backup` daily with cron.
90
+
91
+ Contributions, tips/advices and pull requests are welcome.
92
+
93
+ ### Licence
94
+
95
+ This code is MIT licenced:
96
+
97
+ Copyright (c) 2014 Valentin Vasilyev
98
+
99
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
100
+
101
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
102
+
103
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48
104
 
49
105
  ## Contributing
50
106
 
data/bin/fallout CHANGED
@@ -66,6 +66,27 @@ when 'restore'
66
66
  if(opts[:instance].nil? || opts[:volume].nil?)
67
67
  puts '-i and -v are required for restore'
68
68
  else
69
- Fallout::Restore.new(opts).run
69
+ begin
70
+ r = Fallout::Restore.new(opts)
71
+ snapshot = r.get_latest_snapshot_for_volume(r.get_volume)
72
+ puts "Initiating omega protocol, retina scan required."
73
+ sleep 3
74
+ puts "Ok, let's do it without retina, shutting down your instance."
75
+ instance = r.shutdown_instance
76
+ puts "Shut down instance successfully: #{instance.id}, status: #{instance.status}"
77
+ attachment = r.detach_volume(instance)
78
+ puts "Detached volume #{attachment.volume.id} from instance #{instance.id} successfully"
79
+ new_volume = r.create_volume(snapshot)
80
+ puts "Created new volume from the latest snapshot (#{new_volume.id})"
81
+ r.attach_volume(new_volume, instance)
82
+ puts "Attached new volume to instance"
83
+ instance = r.start_instance(instance)
84
+ puts "Successfully restored and started the instance with the new volume"
85
+ puts "Instance public hostname: #{instance.dns_name}"
86
+ puts "You man want to delete the old, detached volume #{attachment.volume.id} and old volume snapshots"
87
+ puts "IMPORTANT: You must update your backup command to use new volume_id: #{new_volume.id}"
88
+ rescue Exception => e
89
+ puts e.message
90
+ end
70
91
  end
71
92
  end
@@ -1,10 +1,77 @@
1
1
  module Fallout
2
2
  class Restore
3
3
  def initialize(options)
4
+ @instance_id = options[:instance]
5
+ @volume_id = options[:volume]
6
+ @ec2 = AWS::EC2.new
7
+ @ec2_client = AWS::EC2::Client.new
4
8
  end
5
9
 
6
- def run
7
- puts 'running restore with options: ' + @options.inspect
10
+ def get_volume
11
+ @ec2.volumes[@volume_id]
12
+ end
13
+
14
+ def shutdown_instance
15
+ instance = @ec2.instances[@instance_id]
16
+ raise "Instance does not exist: #{@instance_id}" if instance.nil? || !instance.exists?
17
+ instance.stop
18
+ while instance.status != :stopped
19
+ sleep 1
20
+ end
21
+ instance
22
+ end
23
+
24
+ def start_instance(instance)
25
+ instance.start
26
+ while instance.status != :running
27
+ sleep 1
28
+ end
29
+ instance
30
+ end
31
+
32
+ def detach_volume(instance, device = '/dev/sda1')
33
+ volume = @ec2.volumes[@volume_id]
34
+ raise "Volume does not exist: #{volume_id}" if volume.nil? || !volume.exists?
35
+ attachment = volume.detach_from(instance, device)
36
+ while volume.status != :available
37
+ sleep 1
38
+ end
39
+ attachment
40
+ end
41
+
42
+ def attach_volume(volume, instance, device = '/dev/sda1')
43
+ volume.attach_to(instance, device)
44
+ while volume.status != :in_use
45
+ sleep 1
46
+ end
47
+ volume
48
+ end
49
+
50
+ def delete_volume(volume)
51
+ volume.delete
52
+ while volume.status != :deleted
53
+ sleep 1
54
+ end
55
+ volume
56
+ end
57
+
58
+ def get_latest_snapshot_for_volume(volume)
59
+ snapshots = @ec2.snapshots.filter('volume-id', @volume_id)
60
+ raise "No snapshots for volume #{volume.id} found, aborting restore process.\n
61
+ Hint: have you created a snapshot for this volume at least once?" unless snapshots.any?
62
+ snapshots.max_by{|ss| Date.parse(ss.tags.to_h[EXPIRES_AFTER_KEY]) rescue Date.new}
63
+ end
64
+
65
+ def create_volume(snapshot, availability_zone = 'us-east-1a', volume_type: 'gp2')
66
+ resp = @ec2_client.create_volume(
67
+ snapshot_id: snapshot.id,
68
+ availability_zone: availability_zone,
69
+ volume_type: volume_type)
70
+ volume = @ec2.volumes[resp[:volume_id]]
71
+ while volume.status != :available
72
+ sleep 1
73
+ end
74
+ volume
8
75
  end
9
76
  end
10
- end
77
+ end
@@ -1,3 +1,3 @@
1
1
  module Fallout
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fallout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valentin Vasilyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-12 00:00:00.000000000 Z
11
+ date: 2014-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk