Skip to content

Commit 12c4f2d

Browse files
committed
add code for database restore
close NARKOZ#33
1 parent a503d24 commit 12c4f2d

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

kumar_asshole.rb

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@
99
GMAIL_PASSWORD = ENV['GMAIL_PASSWORD']
1010

1111
gmail = Gmail.connect(GMAIL_USERNAME, GMAIL_PASSWORD)
12+
kumars_email = '[email protected]'
1213

14+
DB_NAME_REGEX = /\S+_staging/
1315
KEYWORDS_REGEX = /sorry|help|wrong/i
1416

15-
gmail.inbox.find(:unread, from: '[email protected]').each do |email|
16-
if email.body[KEYWORDS_REGEX]
17-
# Restore DB and send a reply
17+
gmail.inbox.find(:unread, from: kumars_email).each do |email|
18+
if email.body[KEYWORDS_REGEX] && (db_name = email.body[DB_NAME_REGEX])
19+
backup_file = "/home/backups/databases/#{db_name}-" + (Date.today - 1).strftime('%Y%m%d') + '.gz'
20+
abort 'ERROR: Backup file not found' unless File.exist?(backup_file)
21+
22+
# Restore DB
23+
`gunzip -c #{backup_file} | psql #{db_name}`
24+
25+
# Mark as read, add label and reply
26+
email.read!
1827
email.label('Database fixes')
1928
reply = create_reply(email.subject)
2029
gmail.deliver(reply)
@@ -23,7 +32,7 @@
2332

2433
def create_reply(subject)
2534
gmail.compose do
26-
35+
to kumars_email
2736
subject "RE: #{subject}"
2837
body "No problem. I've fixed it. \n\n Please be careful next time."
2938
end

0 commit comments

Comments
 (0)