2
0
mirror of https://github.com/narkoz/hacker-scripts synced 2025-08-26 04:17:10 +00:00

add code for database restore

close #33
This commit is contained in:
Nihad Abbasov 2015-11-25 21:57:03 +04:00
parent a503d242d8
commit 12c4f2dcfd

View File

@ -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