|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use YAML; |
| 7 | +use DateTime; |
| 8 | +use Mail::Webmail::Gmail; |
| 9 | + |
| 10 | +# Config |
| 11 | +my $conf = Load( <<'...' ); |
| 12 | +--- |
| 13 | + |
| 14 | +database_regex: \S+_staging |
| 15 | +keywords_regex: sorry|help|wrong |
| 16 | +backup_path: /home/backups/databases/ |
| 17 | +... |
| 18 | +$conf->{'database_regex'} = qr/ ( $conf->{'database_regex'} ) /x; |
| 19 | +$conf->{'keywords_regex'} = qr/ ( $conf->{'keywords_regex'} ) /x; |
| 20 | + |
| 21 | +my $date = DateTime->now->subtract( |
| 22 | + 'days' => 1 |
| 23 | +); |
| 24 | + |
| 25 | +# Load GMail API config |
| 26 | +open( my $env, '<', '../.env' ) || die "Cannot find .env file in project root."; |
| 27 | +LINE: while ( my $line = <$env> ) { |
| 28 | + next LINE unless ( $line =~ m/^(GMAIL[^=]+)=(.*)(?:[\n\r]*)/ ); |
| 29 | + $conf->{'env'}->{ $1 } = $2; |
| 30 | +} |
| 31 | + |
| 32 | +close $env; |
| 33 | + |
| 34 | +my $gmail = Mail::Webmail::Gmail->new( |
| 35 | + username => $conf->{'env'}->{'GMAIL_USERNAME'}, |
| 36 | + password => $conf->{'env'}->{'GMAIL_PASSWORD'}, |
| 37 | + encrypt_session => 1, |
| 38 | +); |
| 39 | + |
| 40 | +my $messages = $gmail->get_messages( label => $Mail::Webmail::Gmail::FOLDERS{ 'INBOX' } ); |
| 41 | +die "Cannot fetch emails: ". $gmail->error_msg(); |
| 42 | + |
| 43 | +MESSAGE: foreach my $message ( @{ $messages } ) { |
| 44 | + unless ( |
| 45 | + ( $message->{ 'new' } ) |
| 46 | + && ( $message->{'sender_email'} eq $conf->{'kumars_email'} ) |
| 47 | + && ( $message->{'body'} =~ m/$conf->{'keywords_regex'}/ ) |
| 48 | + && ( $message->{'body'} =~ m/$conf->{'database_regex'}/ ) |
| 49 | + ) { |
| 50 | + print "Skipping mail from=[". $message->{'sender_email'}."] subject=[". $message->{'subject'} ."]\n"; |
| 51 | + next MESSAGE; |
| 52 | + } |
| 53 | + exit 1; |
| 54 | + |
| 55 | + my $database = $1; |
| 56 | + my $backup_file = $conf->{'backup_path'} . $database .'-'. $date->ymd() .'.gz'; |
| 57 | + |
| 58 | + unless ( -f $backup_file ) { |
| 59 | + die 'Cannot find backup file=['. $backup_file ."]\n"; |
| 60 | + } |
| 61 | + |
| 62 | + print 'Restoring database=['. $database .'] from day=['. $date->ymd() .'] from file=['. $backup_file ."]\n"; |
| 63 | + |
| 64 | + # Restore DB |
| 65 | + system( 'gunzip -c '. $backup_file .' | psql '. $database ); |
| 66 | + die "Error while restoring the database=[". $database ."] from file=[". $backup_file ."]" if ( $? >> 8 ); |
| 67 | + |
| 68 | + # Mark as read, add label, reply |
| 69 | + $gmail->edit_labels( |
| 70 | + 'label' => 'Database fixes', |
| 71 | + 'action' => 'add', |
| 72 | + 'msgid' => $message->{'id'} |
| 73 | + ); |
| 74 | + |
| 75 | + $gmail->send_message( |
| 76 | + 'to' => $conf->{'kumars_email'}, |
| 77 | + 'subject' => 'RE: '. $message->{'subject'}, |
| 78 | + 'msgbody' => "No problem. I've fixed it. \n\n Please be careful next time.", |
| 79 | + ); |
| 80 | + |
| 81 | + $gmail->edit_labels( |
| 82 | + 'label' => 'unread', |
| 83 | + 'action' => 'remove', |
| 84 | + 'msgid' => $message->{'id'} |
| 85 | + ); |
| 86 | + |
| 87 | +} |
| 88 | + |
0 commit comments