-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolder_backup.py
More file actions
266 lines (209 loc) · 7.77 KB
/
Folder_backup.py
File metadata and controls
266 lines (209 loc) · 7.77 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'''
• Control Panel:
================================================='''
on=True; off=False; exe=(); pause_code=(); debug_mode=()
# --------------------------------------------------------
exe=True # Execu/te code (copy, remove) [#...] [X]
pause_code=True # Pause code
# Print code mode:
if exe:pass
else:print("\n >>> {exe} mode is disabled!!!\n\n")
'''
• Define Variables:
================================================='''
FolderToBackup = 'C:\Sample_folder'
DestDrive = 'E:\\'
tag_copy = '-copy'
tag_old = '-old'
folder_copy = "E:\Sample_folder"+tag_copy
folder_copy_old = folder_copy+tag_old
'''
• Code timer:
================================================='''
import time
'''
• Check directories to backup if exists:
================================================='''
import sys, os
if os.path.exists(FolderToBackup):pass
else:
print('\n !Error, no folder to backup:\n',FolderToBackup,'\n')
if pause_code:
os.system("pause") # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
sys.exit() # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
if os.path.exists(DestDrive):pass
else:
print('\n !Error - no destination drive:\n',DestDrive,'\n')
if pause_code:
os.system("pause") # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
sys.exit() # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'''
• Timestamp:
================================================='''
from datetime import datetime
timestamp = datetime.now().strftime("%a, %d %b %Y, %H:%M:%S")
print(''+timestamp)
'''
• Folder size:
================================================='''
from pathlib import Path
root_directory = Path(FolderToBackup)
# root_directory = Path('D:\data')
x= sum(f.stat().st_size for f in root_directory.glob('**/*') if f.is_file())
if x <= 1024: # < 1 KB
unit = 'Bytes'
x= x
elif x <= 1024**2: # < 1 MB
unit = 'KB'
x= x/1024
elif x <= 1024**3: # < 1 GB
unit = 'MB'
x= x/1024**2
else: # >= 1 GB
unit = 'GB'
x= x/1024**3
x= round(x,2)
x= '| Size: '+str(x)+' '+unit
# print('\n',x)
'''
• Number of items:
================================================='''
folder_list=0
# List all files in the directory tree: FolderToBackup
def getListOfFiles(dirName):
# create a list of file and sub directories
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
allFiles.append(fullPath)
return allFiles
dirName = FolderToBackup
listOfFiles = getListOfFiles(dirName) # Get the list of all files in directory tree at given path
folder_list= listOfFiles
folder_list= len(folder_list)
x= x+' | Items: '+str(folder_list)
print('\nBackup Folder: ',FolderToBackup,'',x)
print()
# print('------------------------------------------------------------------------')
if pause_code: os.system("pause") # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
print()
# Start timer:
t_code_start = time.perf_counter()
'''
• Backup folder:
================================================='''
# Remove copy of old one:
# -----------------------------------------------
# Check if exists E:/DEV-old/
# Remove E:/DEV-old/
# Copy E:/DEV/ to E:/DEV-old/
# Remove E:/DEV/
# Copy D:/DEV/ to E:/DEV/
# from distutils.dir_util import copy_tree
# import shutil
'''
• Copy/Remove:
================================================='''
if on:
from distutils.dir_util import copy_tree
# Check if folder-copy exists:
if os.path.exists(folder_copy):
import shutil
# Check if folder-copy-old exists:
# print('\nRemoving folder...',folder_copy_old)
if os.path.exists(folder_copy_old):
print(' [#...] removing...',folder_copy_old)
# Remove old folder:
try:
if exe: shutil.rmtree(folder_copy_old)
except OSError as e:
print("\n Error: %s - %s." % (e.filename, e.strerror))
# COPY folder-copy to folder-copy-old:
if on:
# Check if folder-copy exists:
if os.path.exists(folder_copy):
print(' [##..] copying... ',folder_copy,'->',folder_copy_old)
if exe: copy_tree(folder_copy, folder_copy_old) # Copy!
# Remove folder-copy:
print(' [###.] removing...',folder_copy)
try:
if exe: shutil.rmtree(folder_copy)
except OSError as e:
print("\n Error: %s - %s." % (e.filename, e.strerror))
# COPY new one:
if on:
if os.path.exists(FolderToBackup):
print(' [####] copying... ',FolderToBackup,'->',folder_copy)
if exe: copy_tree(FolderToBackup, folder_copy) # Copy!
else:print(' !No Folder...', FolderToBackup)
'''
• Folder backup completed:
================================================='''
print('\n\n Folder backup completed.')
print('------------------------------------------')
if os.path.exists(folder_copy):print(' '+folder_copy,' - created!')
else:print(' '+folder_copy, '- doesn\'t exist!')
if os.path.exists(folder_copy_old):print(' '+folder_copy_old,' - created!')
else:print(' '+folder_copy_old, '- doesn\'t exist!')
'''
• Code execution time:
================================================='''
t_code_sec = round(time.perf_counter() - t_code_start, 3) ; print('\n Time:',t_code_sec,'[s]\n')
# if pause_code: os.system("pause") # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'''
• List files and folders:
================================================='''
if on:
import keyboard
print('\nPress [L] to list all items... or any key to EXIT:')
while True:
x= keyboard.read_key()
if x == "l":break
sys.exit()
# Start time
t_code_start = time.perf_counter()
# List folders and files in dir tree: FolderToBackup
if off:
print()
for i in listOfFiles:
print(i)
# x= len(listOfFiles)
# print('\n',x)
# List folders and files in dir: folder_copy
if off:
folder_list= os.listdir(path=folder_copy)
for i in folder_list:
print(i)
x= len(folder_list)
print('\n',x)
# List files only:
if on:
print()
path = 'c:\\projects\\hc2\\'
path = folder_copy
files = []
# r= root, d= directories, f= files
for r, d, f in os.walk(path):
for file in f:
# print(file)
# files.append(os.path.join(file)) # all items
# if '.txt' in file: files.append(os.path.join(r, file)) # .txt files only
# if '.py' in file: files.append(os.path.join('', file)) # .py files only
# if '.lnk' in file: files.append(os.path.join(r, file)) # .lnk files only
if '.lnk' in file:pass # .lnk files excluded
else: files.append(os.path.join('', file))
files.sort(reverse=False) # sort list ascending
for f in files:
print(f)
print('\n',len(files),'files')
# End time:
t_code_sec = round(time.perf_counter() - t_code_start, 3) ; print('\n Time:',t_code_sec,'[s]\n')
# print('\n')
if pause_code: os.system("pause") # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>