-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbackup.py
116 lines (98 loc) · 4.11 KB
/
backup.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Backup all files in the ./data directory
__author__ = "Julien Mousqueton"
__copyright__ = "Copyright 2024, Ransomwarelive NG Project"
__version__ = "1.0.1"
# Import necessary modules
import os
import gzip
import shutil
import datetime
import difflib
import logging
#from pprint import pprint
# Backup destination
base_backup_dir = "/root/backup/" # Directory to store backups
# Log File (future use)
log_file = "/var/log/backup.log" # Specify the path to your log file
# Enable or disable gzip compression for .diff files
use_gzip = True # Set to True to enable gzip compression
# Source directory containing files to back up
source_dir = "./data/"
# Configure logging
logging.basicConfig(
format='%(asctime)s,%(msecs)d %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d:%H:%M:%S',
level=logging.INFO
)
# Define custom logging functions
def stdlog(msg):
'''Standard info logging'''
logging.info(msg)
def dbglog(msg):
'''Debug logging'''
logging.debug(msg)
def errlog(msg):
'''Error logging'''
logging.error(msg)
# Function to log messages with timestamps
def writelog(message):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(log_file, "a") as f:
f.write(f"{timestamp} {message}\n")
# Calculate the week number
week_number = datetime.datetime.now().strftime("%Y-%U")
# Log the start of the backup process
stdlog("Backup started for week " + week_number)
# Create the week-specific backup directory
backup_dir = os.path.join(base_backup_dir, week_number) + "/data"
try:
os.makedirs(backup_dir, exist_ok=True)
stdlog("Backup directory created: " + backup_dir)
except Exception as e:
errlog("Failed to create backup directory " + backup_dir)
exit(1)
# Get the list of all files in the source directory
files = [os.path.join(source_dir, file) for file in os.listdir(source_dir) if os.path.isfile(os.path.join(source_dir, file))]
# Loop through the list of files to back up
for file in files:
# Check if the source file exists (this should always be true as we filtered for files)
if not os.path.exists(file):
errlog("Source file does not exist: " + file)
continue # Skip this file and proceed to the next one
backup_file = os.path.join(backup_dir, f"{os.path.basename(file)}.{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.diff")
# Compare the current file with the backup (if it exists)
if os.path.isfile(os.path.join(backup_dir, os.path.basename(file))):
with open(os.path.join(backup_dir, os.path.basename(file)), "r") as f1:
l1 = f1.readlines()
with open(file, "r") as f2:
l2 = f2.readlines()
differ = difflib.unified_diff(l1, l2, lineterm='')
# Write the differences to the diff file
with open(backup_file, "w") as backup_output:
backup_output.writelines(differ)
stdlog("Check for incremental for " + file)
# Check if the diff file size is 0 and delete it if so
if os.path.getsize(backup_file) == 0:
os.remove(backup_file)
stdlog("No incremental for " + file)
else:
stdlog("Creating " + backup_file)
# If use_gzip is True, gzip the .diff file
if use_gzip:
try:
with open(backup_file, "rb") as f_in, gzip.open(backup_file + ".gz", "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
os.remove(backup_file) # Remove the original .diff file
stdlog("Gzipped incremental file: " + backup_file + ".gz")
except Exception as e:
errlog("Failed to gzip incremental file: " + backup_file)
else:
try:
shutil.copyfile(file, os.path.join(backup_dir, os.path.basename(file)))
stdlog("Created new backup for week " + week_number + ": " + file)
except Exception as e:
errlog("Failed to create new backup for week " + week_number + ": " + file)
# Log the completion of the backup process for the current week
stdlog("Backup completed for week " + week_number)