This repository has been archived by the owner on Aug 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_backup_utility.py
88 lines (78 loc) · 3.04 KB
/
my_backup_utility.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
import filecmp
import os
import re
import shutil
def filePathsList(rootDir):
"""
Returns a list of strings containing the paths to all the files under the parameter 'rootDir'.
"""
filePathsList = []
for dirpath, dirnames, filenames in os.walk(rootDir):
for file in filenames:
fullPath = os.path.join(dirpath, file)
filePathsList.append(fullPath)
return filePathsList
def getSetting(settingsFile, settingTitle):
"""
Reads 'settingsFile' and finds the last instance of 'settingTitle'. Returns the
value of 'settingTitle' that comes after te '=' sign, removing any trailing whitespaces
and any closing '/' or '\'.
If not found, returns False.
"""
setting = False
with open(settingsFile, 'r') as settings:
for line in settings:
settingRegexp = re.search(settingTitle + '\s*=\s*(.+)', line)
if settingRegexp:
setting = settingRegexp.group(1).strip().rstrip('/').rstrip('\\')
re.purge()
return setting
def printSortedLines(array, precursor=''):
"""
Sorts and prints all elements from an array. Returns the length
of the array.
Takes the optional parameter 'precursor', which it prints before
each element.
"""
lineCount = len(array)
if lineCount:
array.sort()
for line in array:
print(precursor + line)
return lineCount
def main():
print('Running backup utility...\n')
# initiation block
goodIcon = '[+] '
badIcon = '[-] '
eventsLog = []
originDir = getSetting('dirs.cfg', 'originDir')
backupDir = getSetting('dirs.cfg', 'backupDir')
print('\tOrigin directory: {}\n\tBackup directory: {}\n'.format(originDir, backupDir))
if not os.path.exists(originDir) or not os.path.exists(backupDir):
print('[-] ' + 'Specifified origin and/or backup directory do not exist.\nExiting...')
exit()
originListing = filePathsList(originDir)
backupListing = filePathsList(backupDir)
# main logic block
for filePath in originListing:
subPath = filePath[len(originDir) + 1:] # sub-dir path to file
subDir = os.path.dirname(subPath)
backupPath = os.path.join(backupDir, subPath)
if backupPath in backupListing:
sameFile = filecmp.cmp(filePath, backupPath, False)
if not sameFile:
eventsLog.append(badIcon + 'Different file with same name already exists in backup:' + subPath)
else:
if not os.path.exists(os.path.join(backupDir, subDir)):
os.mkdir(os.path.join(backupDir, subDir))
eventsLog.append(goodIcon + 'Directory created in backup: ' + subDir)
shutil.copy(filePath, backupPath)
eventsLog.append(goodIcon + 'Backed up: ' + subPath)
# termination block
print('Backup utility has finished running. Printing log:\n')
if not printSortedLines(eventsLog, '\t'):
print('\t' + goodIcon + 'All file backups up to date.')
print('\nExiting...')
if __name__ == '__main__':
main()