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