Repository navigation
Expand file tree
/
Copy pathEasyBackup.py
More file actions
135 lines (98 loc) · 5.11 KB
/
Copy pathEasyBackup.py
File metadata and controls
135 lines (98 loc) · 5.11 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
import os, json, math
# In lieu of settings menu, change this variable
filename = 'devices.json'
hostname = str(os.getenv('COMPUTERNAME'))
# Stat tracker variables
coCount = 0
dirCount = 0
fileCount = 0
# Function that actually does the copying. Takes remote and local directories in that order
def moveThings(push,pull):
print('Copying ' + pull)
pullStats = os.stat(pull) # Gets file statistics
pullFile = open(pull, 'rb') # Open the file to copy
pushFile = open(push, 'wb') # and the place to put it
content = pullFile.read() # Reads copied file
pushFile.write(content) # Writes read data
pullFile.close() # Closes files
pushFile.close()
os.utime(push, (pullStats.st_atime, pullStats.st_mtime)) # Write read/modified time to new file, does not do create time
print('Device identified: ' + hostname)
# JSON dict spaghetti to read from the backup data file
rules = dict()
temp = dict()
file = open(filename)
temp = json.loads(file.read())
file.close()
rules = temp[hostname]
del temp
for rule in rules :
# Holds where to move things to/from
deviceDir = rules[rule][0]
driveDir = rules[rule][1]
# List of every directory to check, appended to the pull directory above. Added to as the program runs
dirsToCheck = ['']
for directory in dirsToCheck : # Iterate through the directory list
dirCount += 1
itemDir = deviceDir + directory
itemDirDrive = driveDir + directory
if not(os.path.exists(itemDirDrive)) : # Make the remote dir if it doesn't already exist
os.makedirs(itemDirDrive)
if not(os.path.exists(itemDir)) : # If the local dir doesn't exist don't worry about it
continue
for files in os.listdir(deviceDir + directory): # Scan for files
if os.path.isdir(itemDir + '/' + files) : # If directory, add to The List
dirsToCheck.append(directory + '/' + files)
if not(os.path.isdir(itemDir + '/' + files)) : # If file...
devicePath = itemDir + '/' + files
drivePath = itemDirDrive + '/' + files
deviceStats = os.stat(devicePath)
fileCount += 1
if os.path.exists(drivePath) :
driveStats = os.stat(drivePath) # ...check it's stats...
if (math.ceil(driveStats.st_mtime) >= math.ceil(deviceStats.st_mtime)) :
continue # ...skip this file if the remote location already has the latest version as determined by filename and the "last modified" stat...
coCount += 1
moveThings(drivePath,devicePath) # ...and if it doesn't, then copy it over.
# Switch the source and destination locations so we can do it again
temp = deviceDir
deviceDir = driveDir
driveDir = temp
del temp
# The part where we do it again. Spot the differences!
dirsToCheck = ['']
for directory in dirsToCheck : # Iterate through the directory list
dirCount += 1
itemDir = deviceDir + directory
itemDirDrive = driveDir + directory
if not(os.path.exists(itemDirDrive)) : # Make the dir if it doesn't already exist
os.makedirs(itemDirDrive)
for files in os.listdir(deviceDir + directory): # Scan for files
if files == 'desktop.ini' : # I have no idea why not having this breaks things but it does
continue
if os.path.isdir(itemDir + '/' + files) : # If directory, add to The List
dirsToCheck.append(directory + '/' + files)
if not(os.path.isdir(itemDir + '/' + files)) : # If file...
devicePath = itemDir + '/' + files
drivePath = itemDirDrive + '/' + files
deviceStats = os.stat(devicePath)
fileCount += 1
if os.path.exists(drivePath) :
driveStats = os.stat(drivePath) # ...check it's stats...
if (math.ceil(driveStats.st_mtime) >= math.ceil(deviceStats.st_mtime)) :
continue # ...skip this file if the remote location already has the latest version as determined by filename and the "last modified" stat...
coCount += 1
moveThings(drivePath,devicePath) # ...and if it doesn't, then copy it over.
if (coCount == 0) : # Basic endscreen, largely cosmetic. Displays some stats but that's about it. This part runs if nothing was copied.
print()
print('No new or updated files were found.')
print(str(fileCount) + ' file(s) in ' + str(dirCount) + ' folder(s) scanned.')
print()
os.system('pause')
else: # This part runs if at least one file was copied.
print()
print('Sync finished.')
print(str(fileCount) + ' file(s) in ' + str(dirCount) + ' folder(s) scanned.')
print(str(coCount) + ' file(s) copied.')
print()
os.system('pause')