-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_encrypter.py
More file actions
162 lines (128 loc) · 4.22 KB
/
backup_encrypter.py
File metadata and controls
162 lines (128 loc) · 4.22 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import os.path
import shutil
import subprocess
HARD_CODED_PASSWORD = ""
MAIN_MENU = """
-------------------------
Archivator Tabajara
-------------------------
1. Validate file
2. Validate directory
3. Archive a directory
0. Exit\n"""
BASE_PATH = os.path.dirname(os.path.realpath(__file__))
def validate_file():
os.system("clear")
file = input("File>> ")
file_exists = os.path.isfile(file)
if file_exists:
dirname = os.path.dirname(file)
filename = os.path.basename(file)
os.chdir(dirname)
command = "shasum -c '%s.sha1'" % filename
os.system(command)
else:
print("\nFile not found")
input("\npress any enter")
os.chdir(BASE_PATH)
main_menu()
def validate_directory():
os.system("clear")
dir = input("Dir>> ")
dir_exists = os.path.isdir(dir)
if dir_exists:
os.chdir(dir)
for file in os.listdir(dir):
if file.endswith(".tar.gz") or file.endswith(".enc"):
command = "shasum -c '%s.sha1'" % file
print("\n"+command)
os.system(command)
else:
print("\nDir not found")
input("\npress any key")
os.chdir(BASE_PATH)
main_menu()
def archive_directory():
# os.system("clear")
# password = input("Password>> ")
#if password == HARD_CODED_PASSWORD:
dir_path = input("Dir>> ")
dir_exists = os.path.isdir(dir_path)
if dir_exists:
if not dir_path.endswith(os.sep):
dir_path = dir_path + os.sep # need a slash at the end to extract dir_name properly
# Declaration of variables needed later
dirname = os.path.dirname(dir_path)
dir_temp = dirname+"-temp"
tar = dirname+".tar.gz"
tar_encrypted = tar+".enc"
tar_decrypted = dirname+"-temp.tar.gz"
print("\nCOMPRESSING ")
# tar -czf "$TAR" "$DIR"
command = "tar -czf '%s' -C '%s' ." % (tar, dirname)
print(command)
os.system(command)
print("\nGENERATING CHECKSUM")
# shasum "$TAR" > "$TAR.sha1"
command = "shasum '%s' > '%s.sha1' " % (tar, tar)
print(command)
os.system(command)
print("\nENCRYPTING")
# openssl enc -aes-256-cbc -a -salt -in "$TAR" -out "$TAR_ENCRYPTED" -pass pass:"$PASSWORD"
command = "openssl enc -aes-256-cbc -a -salt -in '%s' -out '%s' -pass pass:'%s'" % (tar, tar_encrypted, HARD_CODED_PASSWORD)
print(command)
os.system(command)
print("\nGENERATING CHECKSUM")
command = "shasum '%s' > '%s.sha1' " % (tar_encrypted, tar_encrypted)
print(command)
os.system(command)
# REVERSING OPERATIONS FOR VALIDATION
print("\nVALIDATING ENCRYPTION")
# openssl enc -d -aes-256-cbc -a -in "$TAR_ENCRYPTED" -out "$TAR_DECRYPTED" -pass pass:"$PASSWORD"
command = "openssl enc -d -aes-256-cbc -a -in '%s' -out '%s' -pass pass:'%s'" % (tar_encrypted, tar_decrypted, HARD_CODED_PASSWORD)
print(command)
os.system(command)
print("\nVALIDATING COMPRESSION")
os.makedirs(dir_temp)
# tar xzf "$TAR_DECRYPTED" --directory "$TMP_DIR"
command = "tar xzf '%s' --directory '%s'" % (tar_decrypted, dir_temp)
print(command)
os.system(command)
print("\nVALIDATING CONTENT")
command = "diff -rq '%s' '%s'" % (dirname, dir_temp)
print(command)
result = os.system(command)
print("\nDELETING TEMPORARY FILES")
command = tar_decrypted
print(command)
os.remove(tar_decrypted)
command = dir_temp
print(command)
shutil.rmtree(dir_temp)
if result == 0:
print("\nSuccessful archiving")
else:
print("\nFailure at archiving")
else:
print("\nDir not found")
#else:
# print("\nPasswords do not match")
input("\npress any key")
main_menu()
def main_menu():
os.system("clear")
print(MAIN_MENU)
choice = input(">> ")
try:
MENU_ACTIONS[choice]()
except KeyError:
main_menu()
MENU_ACTIONS = {
'1': validate_file,
'2': validate_directory,
'3': archive_directory,
'0': exit
}
if __name__ == '__main__':
main_menu()