-
Notifications
You must be signed in to change notification settings - Fork 1
/
restore.rb
99 lines (79 loc) · 2.57 KB
/
restore.rb
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require File.dirname(__FILE__) + '/backs3'
require 'fileutils'
module Backs3
class Restore
include Backs3
include AWS::S3
def self.commands
%w(ls available restore cat info)
end
def initialize(options = {})
@options = options
@options['prefix'] ||= ''
establish_connection
@backups = load_backup_info.sort{|a,b| a.date <=> b.date }
end
def available(backup_key = nil)
if backup_key.nil?
puts "Backups available: #{@backups.map{|b| b.date}.join(", ")}"
else
unless backup = @backups.detect{|b| b.date.to_s == backup_key.to_s }
raise "No backup #{backup_key} available"
end
files = backup.all_files
puts "Backup information for #{backup.date}"
files.each do |file|
puts "\tFile: #{file.path}, backed up #{Time.at(file.backup_info.date).to_s}"
end
end
end
def info(file)
files = @backups.collect{|b| b.files}.flatten.select{|f| f.path == file}
if files.empty?
puts "No information found for file #{file}"
else
puts "Backup information for file #{file}"
files.each do |f|
puts "\tBacked up #{Time.at(f.backup_info.date).to_s} in #{f.backup_info.date} with md5sum #{f.md5sum}"
end
end
end
def ls(backup)
Bucket.objects(@options['bucket'], :prefix => @options['prefix'] + backup.to_s).each do |object|
puts object.key
end
end
def cat(backup, file)
backup_key = @options['prefix'] + backup.to_s
object = S3Object.find(File.join(backup_key, file), @options['bucket']) rescue nil
if object.nil?
puts "Cannot find file #{file}"
else
puts object.value(:reload)
end
end
def restore(date, file = nil)
backup = @backups.detect{|b| b.date.to_s == key.to_s}
backup_key = @options['prefix'] + backup.to_s
objects = []
if file.nil?
objects = Bucket.objects(@options['bucket'], :prefix => @options['prefix'] + backup.to_s)
else
objects << S3Object.find(File.join(backup_key, file), @options['bucket']) rescue nil
end
objects.compact!
objects.each do |object|
$stdout.write "Restoring file #{object.key} to /tmp/#{object.key}"
filename = "/tmp/#{object.key}"
FileUtils.mkdir_p File.dirname(filename)
File.open(filename, 'w') do |f|
object.value do |segment|
$stdout.write "."
f.write segment
end
end
$stdout.write "\n"
end
end
end
end