Skip to content

Commit

Permalink
add code for database restore
Browse files Browse the repository at this point in the history
close NARKOZ#33
  • Loading branch information
NARKOZ committed Nov 25, 2015
1 parent a503d24 commit 12c4f2d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions kumar_asshole.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@
GMAIL_PASSWORD = ENV['GMAIL_PASSWORD']

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

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

gmail.inbox.find(:unread, from: '[email protected]').each do |email|
if email.body[KEYWORDS_REGEX]
# Restore DB and send a reply
gmail.inbox.find(:unread, from: kumars_email).each do |email|
if email.body[KEYWORDS_REGEX] && (db_name = email.body[DB_NAME_REGEX])
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!
email.label('Database fixes')
reply = create_reply(email.subject)
gmail.deliver(reply)
Expand All @@ -23,7 +32,7 @@

def create_reply(subject)
gmail.compose do
to "[email protected]"
to kumars_email
subject "RE: #{subject}"
body "No problem. I've fixed it. \n\n Please be careful next time."
end
Expand Down

0 comments on commit 12c4f2d

Please sign in to comment.