-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (55 loc) · 2.93 KB
/
app.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
import sys
import os
import shutil
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import argparse
def backup_folder(source_path, destination_path, compress=True):
# Create a timestamp for the backup filename
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
# Get the current date and time
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# Check if the destination path already exists
if os.path.exists(destination_path):
if not os.path.exists(os.path.dirname(destination_path)):
os.makedirs(os.path.dirname(destination_path))
# Create the backup folder if it doesn't exist
if not os.path.exists(destination_path):
os.makedirs(destination_path)
# Get a list of all the files and folders in the source path
file_list = os.listdir(source_path)
# Create a list of files and folders to be backed up
backup_list = []
# Iterate through each file and folder in the source path
for file_name in file_list:
file_path = os.path.join(source_path, file_name)
# Check if the file is a folder
if os.path.isdir(file_path):
# Recursively call this function for each subfolder
backup_list += backup_folder(file_path, destination_path, compress)
else:
# Check if the file already exists in the backup folder
if file_name in os.listdir(destination_path):
# If the file already exists, compare the timestamps
if os.path.getmtime(file_path) == os.path.getmtime(os.path.join(destination_path, file_name)) and \
os.path.getatime(file_path) == os.path.getatime(os.path.join(destination_path, file_name)) and \
os.path.getctime(file_path) == os.path.getctime(os.path.join(destination_path, file_name)):
# If the timestamps are the same, compare the file sizes
if os.path.getsize(file_path) == os.path.getsize(os.path.join(destination_path, file_name)):
# If the file sizes are the same, the backups are identical, so skip the backup
continue
else:
# If the timestamps are different, or the file sizes are different, copy the file to the backup folder
shutil.copy2(file_path, destination_path)
backup_list.append(file_name)
# Compose the email message
email_message = MIMEMultipart()
email_message['Subject'] = 'Backup completed at ' + current_time
# Add the backup file list to the email message
attachment = MIMEApplication(backup_list, _subtype='txt')
attachment.add_header('Content-Disposition)