From 12c4f2dcfd3266e4a1e2687d2d50007b15324caa Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 25 Nov 2015 21:57:03 +0400 Subject: [PATCH] add code for database restore close #33 --- kumar_asshole.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/kumar_asshole.rb b/kumar_asshole.rb index e50c6cc..ca5b9f6 100755 --- a/kumar_asshole.rb +++ b/kumar_asshole.rb @@ -9,12 +9,21 @@ GMAIL_USERNAME = ENV['GMAIL_USERNAME'] GMAIL_PASSWORD = ENV['GMAIL_PASSWORD'] gmail = Gmail.connect(GMAIL_USERNAME, GMAIL_PASSWORD) +kumars_email = 'kumar.a@example.com' +DB_NAME_REGEX = /\S+_staging/ KEYWORDS_REGEX = /sorry|help|wrong/i -gmail.inbox.find(:unread, from: 'kumar.a@example.com').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) @@ -23,7 +32,7 @@ end def create_reply(subject) gmail.compose do - to "kumar.a@example.com" + to kumars_email subject "RE: #{subject}" body "No problem. I've fixed it. \n\n Please be careful next time." end