forked from jaredbeck/gocongress-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.rb
86 lines (69 loc) · 1.62 KB
/
publish.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
#!/usr/bin/env ruby
# Publish the latest backup
# =========================
#
# find the latest backup
# delete records from previous years
# encrypt the dump with DES3
# openssl des3 -pass derp < herp.dump > herp.dump.des3
# upload to S3 (using fog?)
require 'date'
module BackupPublisher
module Die
def die msg
$stderr.puts(msg)
exit(1)
end
end
module Config
extend Die
BACKUP_DIR = '/Users/jared/git/usgo/gocongress/backup'.freeze
def self.assert_dir_exists d
die("Not directory: #{d}") unless Dir.exists?(d)
end
def self.check
[BACKUP_DIR, month_dir].each do |d|
assert_dir_exists(d)
end
end
def self.month_dir
File.join(BACKUP_DIR, Date.today.strftime("%Y-%m"))
end
end
module LatestBackup
def self.in path
File.join(path, Dir.new(path).sort.last)
end
end
module Db
extend Die
def self.cleanup path
create
restore path
# delete records from previous years
# see `delete_from_prev_years.sql`
# dump
# drop
end
def self.create
die('Cannot create db') unless system("createdb -T template0 #{name}")
end
def self.drop
die('Cannot drop db') unless system("dropdb #{name}")
end
def self.name
yyyymmdd = Date.today.strftime("%Y%m%d")
"usgc_cleanup_#{yyyymmdd}"
end
def self.restore path
unless system("pg_restore --no-acl --no-owner -d #{name} #{path}")
die('Cannot restore db')
end
end
end
def self.main
Config.check
Db.cleanup LatestBackup.in Config.month_dir
end
end
BackupPublisher.main