-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemail_backup_tool.py
91 lines (68 loc) · 2.79 KB
/
email_backup_tool.py
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
88
89
90
91
#!/usr/bin/env python
import argparse
import signal
import sys
from EmailBackup import EmailBackup, get_accounts_csv, get_accounts, build_account_dict
def signal_handler(sig, frame):
"""
Handle the SIGINT signal (Ctrl+C) gracefully by printing a message and exiting the program.
Args:
sig (int): Signal number.
frame (_frame): Current stack frame.
Returns:
None
"""
print("Ctrl+C detected. Exiting gracefully...")
sys.exit(0)
def parse_arguments():
"""
Parse command-line arguments.
Returns:
argparse.Namespace: Parsed arguments.
"""
parser = argparse.ArgumentParser(description="Email Backup Tool")
parser.set_defaults(func=parser.print_help)
parser.add_argument("--zip", type=int, help="Maximum size in MB for each archive")
parser.add_argument("--backup", default="backups", help="Path of the backup folder (default: backups)")
parser.add_argument("--account", help="Path of the CSV file containing account information")
parser.add_argument("--email", help="Email address of the account to be backed up")
parser.add_argument("--password", help="Password of the email account")
parser.add_argument("--server", help="IMAP server address for accessing emails")
parser.add_argument("--port", type=int, help="Port number of the IMAP server")
return parser.parse_args()
def main():
"""
Main function to execute the email backup process.
Returns:
None
"""
account_csv = "accounts.csv"
is_single_account = False
args = parse_arguments()
if args.email is not None and args.password is not None:
is_single_account = True
if args.account is not None:
account_csv = args.account
if get_accounts_csv(filename=account_csv) is None:
# @todo: file not found, show error
args.func()
email_backup = EmailBackup(max_zip_size=args.zip, backup_folder=args.backup)
try:
if is_single_account:
account = build_account_dict(args)
print(f"[single] Extracting emails from {account['email']}")
email_backup.backup_account(account['email'], account['password'], account['server'], account['port'])
email_backup.zip_backup(account['email'])
return
for account in get_accounts(filename=account_csv):
print(f"[multi] Extracting emails from {account['email']}")
email_backup.backup_account(account['email'], account['password'], account['server'], account['port'])
email_backup.zip_backup(account['email'])
except KeyboardInterrupt:
pass
print("Exiting...")
if __name__ == '__main__':
# Set up a signal handler for SIGINT (Ctrl+C)
signal.signal(signal.SIGINT, signal_handler)
# Execute the main function
main()