-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackupUtils.py
127 lines (104 loc) · 3.68 KB
/
BackupUtils.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
117
118
119
120
121
122
123
124
125
126
127
import sys
import os
import configparser
import re
import hashlib
import getpass
from BackupTools import *
backupTools = BackupTools()
def _canSkip(folderName, folderIgnorePatterns):
print('*********starting logging _canSkip************')
print(folderName)
print(folderIgnorePatterns)
print('*********finished logging _canSkip************')
#'''
for ignorePat in folderIgnorePatterns:
sr = re.search(ignorePat, folderName)
# Ensure full match of pattern
if (sr and len(sr[0]) == len(folderName)):
print(folderName+' VS '+ignorePat+'=pass')
print('group match='+sr[0])
print('ignoring '+folderName)
return True
#'''
return False
def _generatePassHash():
temp = getpass.getpass().encode('utf-8')
return hashlib.sha224(temp).hexdigest()
'''
_createPassword
parameters:
passwordExists - Can be: not_set, none, null, set
config - configParser object reference not copy
'''
def createPassword(passwordExists, config):
password = ''
cwd = os.getcwd()
files = os.listdir(cwd)
if ('key' not in files):
print("ERROR - 'key' file does not exist")
open('key', 'w+')
if (
passwordExists == 'not_set' or
passwordExists == 'none' or
passwordExists == 'null' or
passwordExists == 'false'): # Initial case
password = _generatePassHash()
print('STORING PASSWORD')
config['DEFAULT']['encryptionKey'] = 'SET'
f = open('key', 'w')
f.write(password)
with open('backup.ini', 'w') as configfile:
config.write(configfile)
elif passwordExists.lower() == 'set': # Case where password was set before
f = open('key', 'r')
password = f.read()
return password
'''
_backupSourceToDestination
parameters:
source - Source folder path
destination - destination folder path
password - used to encrypt
'''
def backupSourceToDestination(
source,
destination,
password=None,
standardCopyOptions=None,
encryptedCopyOptions=None,
folderIgnorePatterns=None
):
# Build folder name automaticalyl with source name if no folder exists in destination for use later on
logging = ''
# Encryption backup
folderName = backupTools.getFolderName(source)
if (re.search('.*encrypted.*', destination) and password is not None): # IF encryption is needed
# extract drive letter of destination
match = re.search('([A-Z]:)', destination)
# IF extraction successful AND IF the drive letter exists then build archive
if match and os.path.exists(match.groups()[0]):
configDecrypt = configparser.ConfigParser()
configDecrypt.read('decrypt.ini')
print(folderIgnorePatterns)
if folderIgnorePatterns:
if _canSkip(folderName, folderIgnorePatterns):
return ('Ignoring' +source+ ' to ' +destination)
fileName = folderName
configDecrypt['DEFAULT']['fileName'] = '"'+fileName+'"'
with open('decrypt.ini', 'w') as configfile:
configDecrypt.write(configfile)
logging = backupTools.doEncryptedBackup(
source, destination, password)
# Standard backup
else:
#print('ignorePatterns='+ignorePatterns)
#'''
if folderIgnorePatterns:
if _canSkip(folderName, folderIgnorePatterns):
return 'skipping'
#return ('Ignoring' +source+ ' to ' +destination)
#'''
logging = backupTools.doStandardBackup(
source, destination, standardCopyOptions)
return logging