-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.py
77 lines (60 loc) · 2.42 KB
/
verify.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
import hashlib
import tarfile
import os
import time
import shutil
import sys
source_dir = sys.argv[1]
backup_file = sys.argv[2]
current_time = time.strftime("%Y-%m-%d-%H%M", time.gmtime())
extract_dir = "/tmp/" + current_time
if len(sys.argv) != 3:
print("Error: incorrect number of parameters. Usage: verify.py source_dir backup_file")
sys.exit(1)
if not os.path.exists(backup_file):
print("Error: backup file", backup_file, "does not exist")
sys.exit(1)
if not tarfile.is_tarfile(backup_file):
print("Error:", backup_file, "is not a tar archive")
sys.exit(1)
if not os.path.exists(source_dir):
print("Error: source directory", source_dir, "does not exist")
sys.exit(1)
tar = tarfile.open(backup_file)
with tarfile.open(tar, "r:gz") as tarfilecount
members = tar.getmembers()
num_files = len(members) - 2
if len(tar.getnames()) != num_files:
print("Error: the number of files in the tar archive and the source directory do not match")
sys.exit(1)
tar.close()
# Create the extract_dir directory if it does not exist
if not os.path.exists(extract_dir):
os.makedirs(extract_dir)
# Open the tar archive for reading
with tarfile.open(backup_file, 'r') as tar:
# Keep track of whether there are any checksum errors
error = False
# Iterate through all the files in the tar archive
for member in tar.getmembers():
# Extract the file from the tar archive to the extract_dir directory
tar.extract(member, extract_dir)
# Calculate the MD5 checksum of the file in the tar archive
with open(os.path.join(extract_dir, member.name), 'rb') as f:
backup_checksum = hashlib.md5(f.read()).hexdigest()
# Calculate the MD5 checksum of the corresponding source file
with open(os.path.join(source_dir, member.name), 'rb') as f:
source_checksum = hashlib.md5(f.read()).hexdigest()
# Compare the checksums
if backup_checksum != source_checksum:
print(f'ERROR: Checksum mismatch for file {member.name}')
else:
# Delete the extracted file after the comparison is done
os.remove(os.path.join(extract_dir, member.name))
# Return a message if there are no checksum errors
if not error:
print('All files match their source counterparts.')
# Remove the extract_dir directory after all files have been processed
shutil.rmtree(extract_dir)
# close the tar archive
tar.close()