This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.pl
84 lines (63 loc) · 1.53 KB
/
backup.pl
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
# Always be safe
use strict;
use warnings;
# Use the module
use Mail::IMAPClient;
use Sys::Hostname;
use Getopt::Std;
sub usage()
{
print STDERR << "EOF";
-u : IMAP Username
-p : IMAP Password
-F : IMAP Folders to backup
Example: perl $0 -u foo.bar\@gmail.com -p secret -F "INBOX SPAM"
EOF
exit;
}
my %options=();
getopts("u:p:F:", \%options) or usage();
my $mail_password = $options{p} || usage();
my $mail_user = $options{u} || usage();
my $folders = $options{F} || usage();
my @boxes = split(' ', $folders);
my $imap = Mail::IMAPClient->new(
Server => 'imap.gmail.com',
User => $mail_user,
Password => $mail_password,
Port => 993,
Ssl => 1,
)
or die "IMAP Failure: $@";
$imap->State(Mail::IMAPClient::Connected());
if ( !-d 'mail' ) {
mkdir('mail',0777);
}
foreach my $box (@boxes) {
if ( !-d "mail/$box" ) {
mkdir("mail/$box",0777);
}
if ( !-d "mail/$box/cur" ) {
mkdir("mail/$box/cur",0777);
}
if ( !-d "mail/$box/new" ) {
mkdir("mail/$box/new",0777);
}
if ( !-d "mail/$box/tmp" ) {
mkdir("mail/$box/tmp",0777);
}
$imap->select($box)
or die "IMAP Select Error: $!";
my @msgs = $imap->search('ALL')
or die "Couldn't get all messages\n";
foreach my $msg (@msgs) {
my $filename = time . "." . int(rand(10000)) . "." . hostname();
open my $fh, ">>mail/$box/cur/$filename"
or die("Open File Error: $!");
$imap->message_to_file($fh, $msg);
close $fh
or die("Formail Close Pipe Error: $!");
}
$imap->close($box);
}
$imap->logout();