2015-11-21 23:04:28 +04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'dotenv'
|
|
|
|
require 'gmail'
|
|
|
|
|
|
|
|
Dotenv.load
|
|
|
|
|
|
|
|
GMAIL_USERNAME = ENV['GMAIL_USERNAME']
|
|
|
|
GMAIL_PASSWORD = ENV['GMAIL_PASSWORD']
|
|
|
|
|
2015-11-28 11:26:22 +05:30
|
|
|
GMAIL = Gmail.connect(GMAIL_USERNAME, GMAIL_PASSWORD)
|
|
|
|
KUMARS_EMAIL = 'kumar.a@example.com'
|
2015-11-21 23:04:28 +04:00
|
|
|
|
2015-11-25 21:57:03 +04:00
|
|
|
DB_NAME_REGEX = /\S+_staging/
|
2015-11-21 23:04:28 +04:00
|
|
|
KEYWORDS_REGEX = /sorry|help|wrong/i
|
|
|
|
|
2015-11-27 22:01:08 +04:00
|
|
|
def create_reply(subject)
|
2015-11-28 11:26:22 +05:30
|
|
|
GMAIL.compose do
|
|
|
|
to KUMARS_EMAIL
|
2015-11-27 22:01:08 +04:00
|
|
|
subject "RE: #{subject}"
|
|
|
|
body "No problem. I've fixed it. \n\n Please be careful next time."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-28 11:26:22 +05:30
|
|
|
GMAIL.inbox.find(:unread, from: KUMARS_EMAIL).each do |email|
|
2015-11-27 22:01:08 +04:00
|
|
|
if email.body.raw_source[KEYWORDS_REGEX] && (db_name = email.body.raw_source[DB_NAME_REGEX])
|
2015-11-25 21:57:03 +04:00
|
|
|
backup_file = "/home/backups/databases/#{db_name}-" + (Date.today - 1).strftime('%Y%m%d') + '.gz'
|
|
|
|
abort 'ERROR: Backup file not found' unless File.exist?(backup_file)
|
|
|
|
|
|
|
|
# Restore DB
|
|
|
|
`gunzip -c #{backup_file} | psql #{db_name}`
|
|
|
|
|
|
|
|
# Mark as read, add label and reply
|
|
|
|
email.read!
|
2015-11-21 23:04:28 +04:00
|
|
|
email.label('Database fixes')
|
2015-11-24 15:17:00 +04:00
|
|
|
reply = create_reply(email.subject)
|
2015-11-28 11:26:22 +05:30
|
|
|
GMAIL.deliver(reply)
|
2015-11-21 23:04:28 +04:00
|
|
|
end
|
|
|
|
end
|