-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_backup_files.rb
69 lines (57 loc) · 1.81 KB
/
check_backup_files.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
require 'rubygems'
require 'fog'
require 'pit'
require 'yaml'
require 'active_support/all'
require 'colorize'
class AwsConnect
@@pit = Pit.get('s3', :require => { 'access_key' => '', 'secret_key' => ''})
def self.run(opt = {})
self.new(opt).run
end
def initialize(opt = {})
@access_key = opt[:access_key] || @@pit['access_key']
@secret_key = opt[:secret_key] || @@pit['secret_key']
@region = opt[:region] || 'ap-northeast-1'
end
def connect
Fog::Storage.new(:provider => 'AWS',
:aws_access_key_id => @access_key,
:aws_secret_access_key => @secret_key,
:region => @region )
end
end
class CheckBackupFiles
def self.run
self.new.run
end
def run
yaml = load_yaml
yaml["target_apps"].each do |app_name, config|
puts "Application : #{app_name}".on_yellow
aws = AwsConnect.new(:region => config["region"])
connection = aws.connect
config["backups"].keys.each do |path|
files = connection.directories.get(config["bucket"]).files.select{ |file| file.key.include?(config["backups"]["#{path}"]) }
if file = select_latest_file(files)
created_at = Time.parse(file.last_modified.to_s)
puts "\tFile : #{file.key}"
puts "\tCreated : #{created_at.strftime('%Y/%m/%d %H:%M:%S')}".colorize(:color => risk_color(created_at))
puts "\tCount : #{files.count - 1}"
else
puts "\tFile Not Found".colorize(:red)
end
end
end
end
def load_yaml
YAML.load_file("./config.yml")
end
def select_latest_file files
files.sort{|x,y| y.last_modified <=> x.last_modified}.first
end
def risk_color created_at
created_at + 1.day > Time.now ? :green : :red
end
end
CheckBackupFiles.run