File tree Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change 9
9
GMAIL_PASSWORD = ENV [ 'GMAIL_PASSWORD' ]
10
10
11
11
gmail = Gmail . connect ( GMAIL_USERNAME , GMAIL_PASSWORD )
12
+ kumars_email = 'kumar.a@example.com'
12
13
14
+ DB_NAME_REGEX = /\S +_staging/
13
15
KEYWORDS_REGEX = /sorry|help|wrong/i
14
16
15
- gmail . inbox . find ( :unread , from : 'kumar.a@example.com' ) . 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!
18
27
email . label ( 'Database fixes' )
19
28
reply = create_reply ( email . subject )
20
29
gmail . deliver ( reply )
23
32
24
33
def create_reply ( subject )
25
34
gmail . compose do
26
- to "kumar.a@example.com"
35
+ to kumars_email
27
36
subject "RE: #{ subject } "
28
37
body "No problem. I've fixed it. \n \n Please be careful next time."
29
38
end
You can’t perform that action at this time.
0 commit comments