-
Notifications
You must be signed in to change notification settings - Fork 2
/
RCloneBackup.py
535 lines (444 loc) · 20.3 KB
/
RCloneBackup.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
from argparse import ArgumentParser
from pathlib import Path
import sqlite3
import re
import datetime
import configparser
import subprocess
import json
import platform
import os
import smtplib
from smtplib import SMTPException
import io
import ctypes, sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def SetTaskInfo(database,taskID,lastUpdate):
conn = CreateConnection(database)
dataValue = (taskID, lastUpdate)
UpdateTaskInfo(conn, dataValue)
def GetDriveLetter(path):
rgx = re.match('^[a-zA-Z]{1}:\\\\', str(path))
return rgx.group().replace("\\", "")
def GetMappedDrivers():
currentDir = os.path.dirname(os.path.realpath(__file__))
return str(RunVBScript(str(Path(currentDir) / 'MapedDrivers.vbs'),""), 'utf-8').replace("}\r\n","}")
def Is64Windows():
return 'PROGRAMFILES(X86)' in os.environ
def VSSCreate(path):
rvalue = VSSEasy('CreateShadowCopy "' + path + '"')
return rvalue
def VSSDelete(ShadowID):
rvalue = VSSEasy("DeleteShadowCopyByID " + ShadowID)
return rvalue
def VSSMount(ShadowID, PathToMount):
rvalue = VSSEasy('MountShadowCopy ' + ShadowID + ' "' + PathToMount + '"')
return rvalue
def VSSUnmount(path):
rvalue = VSSEasy('UnmountShadowCopy "' + path + '"')
return rvalue
def RunVBScript(script, parameters):
Command = 'cscript /nologo "' + script + '" ' + parameters
process = subprocess.Popen(Command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
return out + err
def VSSEasy(parameters):
os.path.abspath(__file__)
exe = 'VSSEasy32.exe'
if Is64Windows:
exe = 'VSSEasy64.exe'
path = str(Path(Path().absolute() / exe))
Command = path + ' ' + parameters
process = subprocess.Popen(Command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = process.communicate()
return out[0].decode("utf-8")
def AvoidRCloneBug(path):
#if path is root letter rclone dont work correct
#fix rclone bug when path is c:\ or other root windows path
if re.match('^[a-zA-Z]{1}:\\\\$', str(path)) is None:
rvalue = '"' + path + '"'
else:
rvalue = path
return rvalue
def ListFolderContents(path):
RCloneReturn = RCloneRunCommand('lsjson ' + AvoidRCloneBug(path) + ' -L')
rvalue = ''.join(RCloneReturn[0].decode("utf-8"))
return rvalue
def GetTaskInfo(database, taskID, StarTime):
TaskInfo=''
if not os.path.exists(database):
conn = CreateConnection(database)
if conn is not None:
sql_crate_table = """ CREATE TABLE IF NOT EXISTS TASK_INFO (
ID text NOT NULL UNIQUE,
LAST_UPDATE DATETIME NOT NULL
); """
CreateTable(conn, sql_crate_table)
else:
print("Error! cannot create the database connection.")
else:
conn = CreateConnection(database)
dataValue = (taskID, StarTime)
queryResult = SelectTaskInfo(conn, taskID)
if not queryResult:
InsertTaskInfo(conn, dataValue)
queryResult = SelectTaskInfo(conn, taskID)
return queryResult
def SelectTaskInfo(conn, taskID):
cur = conn.cursor()
cur.execute('SELECT * FROM TASK_INFO WHERE ID = "' + str(taskID) + '";')
rows = cur.fetchall()
return rows
def CreateConnection(db_file):
try:
conn = sqlite3.connect(db_file)
return conn
except OSError as e:
print(e)
return None
def CreateTable(conn, create_table_sql):
try:
c = conn.cursor()
c.execute(create_table_sql)
except OSError as e:
print(e)
def InsertTaskInfo(conn, value):
sql = 'INSERT INTO TASK_INFO(ID, LAST_UPDATE) VALUES("' + str(value[0]) + '","' + str(value[1]) + '");'
cur = conn.cursor()
try:
cur.execute(sql)
except sqlite3.IntegrityError as e:
print('sqlite error: ', e.args[0]) # column name is not unique
conn.commit()
return cur.lastrowid
def UpdateTaskInfo(conn, value):
sql = 'UPDATE TASK_INFO SET LAST_UPDATE = "' + str(value[1]) + '" WHERE ID = "' + str(value[0])+ '";'
cur = conn.cursor()
try:
cur.execute(sql)
except sqlite3.IntegrityError as e:
print('sqlite error: ', e.args[0]) # column name is not unique
conn.commit()
return cur.lastrowid
def ReturnFolderName(path):
rvalue = ''
folderName = os.path.basename(path)
if folderName == '':
rvalue = 'DISK-' + str(path).split(":")[0]
else:
rvalue = folderName
return rvalue
def MakeFolder(path):
RCloneRunCommand('mkdir "' + str(path) + '"')
def DeleteFolder(path):
RCloneRunCommand('purge "' + str(path) + '"')
def GetFolderList(path):
rvalue=[]
vjson = json.loads(ListFolderContents(path))
for element in vjson:
if element['MimeType'] == 'inode/directory':
rvalue.append(element['Name'])
return rvalue
def RCloneRunCommand(command):
CurrentDir = Path().absolute()
rcloneCommand = str(Path(CurrentDir / 'rclone.exe ')) + command
process = subprocess.Popen(rcloneCommand, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
return [out + err]
def RCloneSync(source, destination, backup, logFile, excludedFolders):
excludeds = excludedFolders.split('|')
excludeString = ''
for excluded in excludeds:
excludeString = excludeString + '--exclude "' + excluded.strip() + '" '
excludeString = excludeString.strip()
CurrentDir = Path().absolute()
params = 'sync ' + AvoidRCloneBug(source) + ' ' + AvoidRCloneBug(destination) + ' --backup-dir ' + AvoidRCloneBug(backup) + ' -v --log-file ' + logFile + ' ' + excludeString
rcloneCommand = str(Path(CurrentDir / 'rclone.exe ')) + params
p = subprocess.Popen( rcloneCommand, shell=True)
p.wait()
return p.returncode
def DoesTheFolderExist(localToCheck, folderName):
folderList = GetFolderList(localToCheck)
for folder in folderList:
if str(folderName) == folder:
return True
return False
def CheckFileStructDB(path,fileName):
vjson = json.loads(ListFolderContents(path))
for element in vjson:
if (element['MimeType'] == 'application/octet-stream') and (element['Name'] == fileName ):
return True
return False
def ClearOldBackups(diffsToRetain, path, folderName):
rvalue=[]
vjson = json.loads(ListFolderContents(path))
for element in vjson:
if (element['MimeType'] == 'inode/directory') and ( str(element['Name']).startswith(folderName + '-[')):
rvalue.append(element['Name'])
if rvalue.__len__() > (diffsToRetain + 1):
toDelete = (rvalue.__len__() - (diffsToRetain + 1))
count = 1
while count <= toDelete:
teste = (sorted(rvalue)[count - 1])
RCloneReturn = RCloneRunCommand('purge ' + str(Path(path) / (sorted(rvalue)[count-1])))
count += 1
def IsTheFolderEmpty(path):
rvalue = False
vjson = json.loads(ListFolderContents(path))
if len(vjson) == 0:
rvalue = True
if (len(vjson)==1) and (vjson[0].get('Name')).endswith('.str') :
rvalue = True
return rvalue
def SendEmail(email_server,email_port,from_addr,password,to_addr,subject,content):
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = subject
body = MIMEText(content, 'html')
msg.attach(body)
try:
server = smtplib.SMTP(email_server, email_port)
server.login(from_addr, password)
server.send_message(msg, from_addr=from_addr, to_addrs=[to_addr])
return("The e-mail was sent sucesfuly.")
except smtplib.SMTPAuthenticationError:
return("The username or password is incorrect.")
def WriteLog(path,data):
with io.open(Path(path), 'a', encoding='utf8') as f:
f.write(data + '\n')
f.close()
def SaveFolderStruct(database, directory):
if not os.path.exists(database):
conn = CreateConnection(database)
if conn is not None:
sql_crate_table = """ CREATE TABLE IF NOT EXISTS STRUCT (
IS_FOLDER boolean NOT NULL,
PATH text NOT NULL UNIQUE
); """
CreateTable(conn, sql_crate_table)
else:
print("Error! cannot create the database connection.")
else:
conn = CreateConnection(database)
rootFolder = str(directory).split(os.sep)
rootFolder = rootFolder[rootFolder.__len__() - 1]
for root, dirs, files in os.walk(directory):
currFolder = root.split(str(directory))
dataValue = (os.path.isdir(root), rootFolder + currFolder[1])
InsertFolderStruct(conn,dataValue)
for file in files:
dataValue = (os.path.isdir(Path(Path(root) / file)), os.path.join(rootFolder + currFolder[1], file))
InsertFolderStruct(conn,dataValue)
def InsertFolderStruct(conn, value):
sql = 'INSERT INTO STRUCT(IS_FOLDER, PATH) VALUES (' + str(int(value[0])) + ',"' + str(value[1]) + '");'
cur = conn.cursor()
try:
cur.execute(sql)
except sqlite3.IntegrityError as e:
print('sqlite error: ', e.args[0]) # column name is not unique
conn.commit()
return cur.lastrowid
def RunTask(taskFile):
currentDir = os.path.dirname(os.path.realpath(__file__))
css = '<style>#data{font-family: Verdana, sans-serif;border-collapse: collapse;width: 100%; font-size: 12px;}#data td, #data th {border: 1px solid #000;padding: 8px;}#data tr {background-color: #f1f1f1;}#data th {padding-top: 12px;padding-bottom: 12px;text-align: left;background-color: #2a579a;color: white;}</style>'
VssTable = ""
VSSPath = ''
thtml = ''
error = 0
Messages = []
tableHTML=[]
Messages.append("Resume of the job:" + '<br>')
now = datetime.datetime.now()
startTime= now
Messages.append("Started on: " + startTime.strftime('%d/%m/%Y %H:%M:%S') + '<br>')
today = now.strftime('%Y-%m-%d-%H-%M-%S')
taskSource = ""
taskDestination = ""
configParser = configparser.RawConfigParser()
configParser.read(taskFile, encoding='utf8')
taskID = int(configParser.get('TASK', 'id'))
taskName = str(configParser.get('TASK', 'name'))
taskFolders = configParser.get('TASK', 'folders')
taskDiff = int(configParser.get('TASK', 'diff'))
taskEmailServer = str(configParser.get('TASK', 'email-server'))
taskEmailPort = str(configParser.get('TASK', 'email-port'))
taskEmailUser = str(configParser.get('TASK', 'email-user'))
taskEmailPassword = str(configParser.get('TASK', 'email-password'))
taskEmailTo = str(configParser.get('TASK', 'email-to'))
taskExcludedFolders = str(configParser.get('TASK', 'excluded-folders'))
taskInfoDB = Path(Path().absolute() / 'tasks.sqlite3')
logName = re.sub('[ \\\\/:"*?<>|]', '', str(taskName + '-' + str(taskID) + '-' + today))
backupStatus = ''
#check if log folder exist. if not create folder
if not DoesTheFolderExist(str(Path(Path().absolute())), 'Logs'):
MakeFolder(str(Path(Path().absolute() / 'Logs' )))
#def log file
logFile= str(Path(Path().absolute() / 'Logs' / str(logName +'.log')))
LastDataSent = GetTaskInfo(taskInfoDB, taskID, startTime)
WriteLog(logFile, "Started on: " + startTime.strftime('%d/%m/%Y %H:%M:%S') + '<br>')
#Initialize Shadow copy id table
if str(platform.system()).upper() == "WINDOWS":
VssTable = GetMappedDrivers()
if VssTable == "{}":
VssTable = dict()
else:
VssTable = eval(VssTable)
#check if all source and destination folders exists
folders = taskFolders.split('|')
for folder in folders:
FolderPair = folder.split('->')
firstTime = 0
taskSource = str(FolderPair[0]).strip()
taskDestination = str(FolderPair[1]).strip()
#create Shadow copy
if str(platform.system()).upper() == "WINDOWS":
DiskLetter = GetDriveLetter(str(FolderPair[0]).strip())
VSSid = VssTable.get(DiskLetter.upper())
if VSSid is None:
VssTable[DiskLetter.upper()] = VSSCreate(taskSource)
#check if source folder exists
if not os.path.exists(taskSource):
error=1
Messages.append('<strong>The source folder "' + taskSource + '" does not exist.</strong>')
# check if destination folder exists
if not DoesTheFolderExist(str(Path(taskDestination).parent), str(ReturnFolderName(taskDestination))):
error=1
Messages.append('<strong>The destination folder "' + taskDestination + '" does not exist.</strong>')
#if no error start send data processs
if error == 0:
folders = taskFolders.split('|')
Messages.append('<br><table id="data"><tr><th>Folder</th><th>Status</th></tr>')
for folder in folders:
WriteLog(logFile, "=============================================================================")
WriteLog(logFile, now.strftime('%Y/%m/%d %H:%M:%S') + " Synchronizing " + str(folder))
FolderPair = str(folder).split('->')
taskSource = Path(FolderPair[0].strip())
taskDestination = Path(FolderPair[1].strip())
# mount VSS as source if possible
if str(platform.system()).upper() == "WINDOWS":
DiskLetter = GetDriveLetter(FolderPair[0].strip())
VSSid = VssTable.get(DiskLetter.upper())
if VSSid is not None:
VSSPath = VSSMount(VSSid, DiskLetter)
taskSource = Path(str(taskSource).replace(VSSPath[0:2],VSSPath))
else:
Messages.append('<strong>Error on mount Volume Shadow Copy.</strong>')
folderName = ReturnFolderName(taskSource)
label = datetime.datetime.strptime(LastDataSent[0][1], '%Y-%m-%d %H:%M:%S.%f')
label = label.strftime('%Y-%m-%d-%H-%M-%S')
lastFolderStructDBName = folderName + '-' + str(taskID) + '-' + label + '.str'
folderStructDBName = re.sub('[ \\\\/:"*?<>|]', '', folderName + '-' + str(taskID) + '-' + today + '.str')
folderStructDB = Path().absolute() / folderStructDBName
destFull = str(taskDestination / (folderName + '-[FULL]'))
destDiff = str(taskDestination / (folderName + '-[' + label + ']'))
#save folder struct to be possible restore files
SaveFolderStruct(folderStructDB, taskSource)
#create full folder on dest if not exists
if not DoesTheFolderExist(str(taskDestination), ReturnFolderName(destFull)):
firstTime=1
MakeFolder(destFull)
# create diff folder on dest if not exists
if not DoesTheFolderExist(str(taskDestination), ReturnFolderName(destDiff)):
MakeFolder(destDiff)
#send data to destination
RCloneReturn = RCloneSync(str(taskSource), str(destFull), str(destDiff), logFile, taskExcludedFolders)
#if has no error
if RCloneReturn == 0:
tableHTML.append('<tr><td>'+ folderName +'</td><td>')
#check if have any file modified on diff folder of this folder item
if IsTheFolderEmpty(str(destDiff)):
noChanges = False
if firstTime == 1:
RCloneRunCommand('copy "' + str(folderStructDB) + '" "' + destFull + '"')
else:
RCloneRunCommand('move "' + str(Path(destDiff) / lastFolderStructDBName) + '" "' + destFull + '"')
noChanges = True
#check if folder struct database is on destination full folder
if CheckFileStructDB(destFull, folderStructDBName) or noChanges:
if firstTime == 1:
tableHTML.append('The initial data was sent successfully.</tr>')
else:
tableHTML.append('No changes on data.</tr>')
os.remove(folderStructDB)
else:
error = 1
tableHTML.append('Cannot locate struct folder database on application directory.</tr>')
DeleteFolder(destDiff)
else:
RCloneRunCommand('copy "' + str(folderStructDB) + '" "' + destFull + '"')
# check if internal data base file is on destination
if CheckFileStructDB(destFull, folderStructDBName):
SetTaskInfo(taskInfoDB, taskID, startTime)
tableHTML.append('The data was updated successfully.</tr>')
os.remove(folderStructDB)
# clean old backup folders based on task config
ClearOldBackups(taskDiff, str(taskDestination), folderName)
else:
error = 1
tableHTML.append('Cannot locate struct folder database on destination.</tr>')
else:
error = 1
tableHTML.append('<tr><td>' + folderName + '</td><td>')
tableHTML.append('<strong style="color: #ff0000;">Finished with some problems. Please check the logs to get a details of the errors.</strong></tr>')
#dismount VSS
if VSSPath is not None:
VSSUnmount(str(Path(VSSPath)))
# create html table tasks result for email body
for line in tableHTML:
Messages.append(line)
Messages.append(thtml + '</table>')
#remove created shadow copies
for value in VssTable.values():
if value[0] == "{" :
VSSDelete(value)
if error == 0:
backupStatus = 'Status: Success'
Messages.append('<br><div style="color: blue"><b>General ' + backupStatus + '</b></div><br>')
else:
backupStatus = 'Status: Failure'
Messages.append('<br><div style="color: red"><b>General ' + backupStatus + '</b></div><br>')
subject = str(taskName).upper() + "|" + backupStatus
endTime = datetime.datetime.now()
elapsedTime = endTime - startTime
Messages.append("<b>Started on:</b> " + startTime.strftime('%d/%m/%Y %H:%M:%S') + '<br>')
Messages.append("<b>Finished on:</b> " + endTime.strftime('%d/%m/%Y %H:%M:%S') + '<br>')
Messages.append("<b>Time elapsed:</b> " + str(elapsedTime) + '<br>')
#prepare email to send
emailContent = '<html><head>' + css + '</head><body><div style="font-family: Verdana; font-size: 12px;">'
for line in Messages:
emailContent = emailContent + line
emailContent = emailContent + "</div></body></html>"
#send email
emailResult = SendEmail(taskEmailServer, taskEmailPort, taskEmailUser, taskEmailPassword, taskEmailTo, subject, emailContent)
WriteLog(logFile, "=============================================================================")
WriteLog(logFile, "Email Status: " + emailResult)
WriteLog(logFile, "Started on: " + startTime.strftime('%d/%m/%Y %H:%M:%S'))
WriteLog(logFile, "Finished on: " + endTime.strftime('%d/%m/%Y %H:%M:%S'))
WriteLog(logFile, "Time elapsed: " + str(elapsedTime))
def HasAdministrativePrivilegies():
rvalue = False
if str(platform.system()).upper() == "WINDOWS":
proc = subprocess.Popen(["net", "session"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
if err.decode("utf-8") == "":
rvalue = True
else:
if os.getuid() == 0:
rvalue = True
return rvalue
def Main():
if HasAdministrativePrivilegies():
currentDir = os.path.dirname(os.path.realpath(__file__))
os.chdir(currentDir)
parser = ArgumentParser(description = 'Backup with RClone made by Andrei Bernardo Simoni.')
parser.add_argument('-t', '--task', action='store', required = True ,dest='taskFile', default='Backup with RClone made by Andrei Bernardo Simoni.', help='Task configuration file to run.')
arguments = parser.parse_args()
if os.path.exists(arguments.taskFile):
RunTask(arguments.taskFile)
else:
print('The task file does not exist.')
else:
print('This tool need administrative privileges to work correctly.')
Main()