-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicity-backup.py
More file actions
executable file
·392 lines (318 loc) · 13.5 KB
/
duplicity-backup.py
File metadata and controls
executable file
·392 lines (318 loc) · 13.5 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
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
#!/usr/bin/env python
import optparse as op
import os
import glob
import datetime
import subprocess as sp
import sys
secondsPerDay=60.0*60.0*24.0
class backupData:
def __init__(self,**kwargs):
#set default values
self.daysBeforeFullBackups=2
self.nBackupsToKeep=2
self.pathsToExclude=None
self.pathsToInclude=None
self.lPathsToRestore=None
self.dryRun=False
for key in kwargs:
#set directory to store log files
if key=="logFileDirectory":
#make sure log file path ends with '/'
logFileDirectoryTemp=kwargs[key]
if logFileDirectoryTemp[len(logFileDirectoryTemp)-1] !='/':
logFileDirectoryTemp=logFileDirectoryTemp+'/'
self.logFileDirectory=logFileDirectoryTemp
else:
#other variables just add
self.__dict__[key]=kwargs[key]
'''
#set directory to backup
if key=="fromDirectory":
self.fromDirectory=kwargs[key]
#set directory to hold backup
if key=="toDirectory":
self.toDirectory=kwargs[key]
#set number of days before full backup
if key=="daysBeforeFullBackups":
self.daysBeforeFullBackups=kwargs[key]
#set number of full backups to keep
if key=="nBackupsToKeep":
self.nBackupsToKeep=kwargs[key]
#set list of paths to exclude from backup
if key=="pathsToExclude":
self.pathsToExclude=kwargs[key]
#set list of paths to include in backup
if key=="pathsToInclude":
self.pathsToInclude=kwargs[key]
#set path to put mysql dump in
if key=="mySQLDumpPath":
self.mySQLDumpPath=kwargs[key]
if key=="mySQLUser":
self.mySQLUser=kwargs[key]
if key=="mySQLPass":
self.mySQLPass=kwargs[key]'''
def setDryRun(self):
self.dryRun=True
def setRestorePath(self,sRestorePath):
self.fromDirectory=sRestorePath
def getParser():
#create parser for command line options and arguments
parser=op.OptionParser(usage="%prog [options] [path to restore to]", version="%prog 0.0"
,description="This script provides a simple front end for duplicity, allowing settings to be "+
"set in this script, so that when called from crotab those settings can be applied. It also "+
"adds a log management system allowing log files to be created for each new full backup, and "+
"added to for each incremental backup until the next full backup. [options] is the list of "+
"options given below, and [path to restore to] sets either the path and directory name, or "+
"filename, if it is a specific file, to restore the backup to and cannot "+
"already exist. The [path to restore to] only has an effect if the -r options is set also."+
"See \"man duplicity\" for more backup functionality not provided with this script.")
restore_group=op.OptionGroup(parser,"Restore options", "All of these options only have an "+
"effect if the -r option is also specified")
parser.add_option("-d","--dry-run",action="store_true",dest="dryRun",default=False
,help="If set it will perform a dry run only, which will print the duplicity commands that "+
"would be issued if it wasn't a dry run, but doesn't actually run the commands")
#add restore options
restore_group.add_option("-r","--restore",action="store_true",dest="restore",default=False
,help="setting this flag activates restore mode, which if no path-to-restore is given it will "+
"restore the entire backup to the directory specified, or the directory set in the "+
"script.")
restore_group.add_option("-p","--path-to-restore",type="string",dest="pathToRestore"
,help="This option sets a path to be restored from the archive. If not set when the -r option "+
"is set the entire backup will be restored. This path is RELATIVE TO THE ROOT DIRECTORY OF "+
"THE INITIAL BACKUP. This can be either a directory or a file.")
restore_group.add_option("-t","--time",type="string",dest="timeToRestoreFrom",default="now"
,help="This option sets the time at which to restore the file from. This is"
+" the same as the duplicity time string, see 'man duplicity' for details. "
+"[default: %default]")
parser.add_option_group(restore_group)
return parser
def main():
#set backup persistent options
backupInfo=backupData(
logFileDirectory="/var/log/python-duplicity-backup-logs"#place to store log files
,fromDirectory="/"#path to backup
,toDirectory="sftp://Acenet@142.12.33.10/diversity_vm_data_backup"#path to place backup in (can be local directory (file://) or some remote (sftp://) etc.)
,daysBeforeFullBackups=90#Number of days before pqreforming a full backup (0.010417, every 15 mins)
,nBackupsToKeep=2#Number of full backups to keep
,daysBeforeLogsRemoved=180#30 mins
,pathsToExclude=["/"]#list of paths to exclude from backup under fromDirectory
,pathsToInclude=["/var/lib/mysqlbackups/mysql","/usr/local/fedora","/var/www","/opt","/etc/apache2","/etc/letsencrypt"]# a list of paths to include in backup under fromDirectory
,mySQLDumpPath=""
,mySQLUser=""
,mySQLPass="")
#parse arguments and options
parser=getParser()
(options,args)=parser.parse_args()
if options.dryRun:
backupInfo.setDryRun()
#restore the backup
if options.restore :
pathToRestore=None
if options.pathToRestore!=None:
if len(options.pathToRestore)>0:
pathToRestore=options.pathToRestore
pathToRestoreTo=None
if len(args)>0:
if args[0]!=None:
if len(args[0])>0 :
pathToRestoreTo=args[0]
restore(backupInfo,options.pathToRestore,options.timeToRestoreFrom
,pathToRestoreTo)
else:
#do backup
backup(backupInfo)
def removeOldLogs(backupInfo):
logFiles=glob.glob(backupInfo.logFileDirectory+"*.log")
now=datetime.datetime.now()
for logFile in logFiles:
dateLogFile=logFile.rpartition('/')#remove path
dateLogFile=dateLogFile[2].rstrip('.log')#remove extension
fileDate=dateLogFile.split("-")#break up into year/month/day/hour/min/second
logFileDateTime=datetime.datetime(int(fileDate[0]),int(fileDate[1])
,int(fileDate[2]),int(fileDate[3]),int(fileDate[4]),int(fileDate[5]))
delta=now-logFileDateTime
if delta.total_seconds()/secondsPerDay>backupInfo.daysBeforeLogsRemoved:
#remove log
os.remove(logFile)
def backup(backupInfo):
#dump mysql databases if path setDryRun
if backupInfo.mySQLDumpPath:
mysqldump(backupInfo)
#get date of last log file
daysSinceLastLogFile=getDaysSinceLastLogFile(backupInfo)
#if none found, assume no full backup
if daysSinceLastLogFile==None:
fullBackup(backupInfo) #do full backup
else:
#if date of last log file is older than specified number of days do full backup
if daysSinceLastLogFile>=backupInfo.daysBeforeFullBackups:
fullBackup(backupInfo) #do full backup
#if date of last log file is < younger than specified number of days do incremental backup
if daysSinceLastLogFile<backupInfo.daysBeforeFullBackups:
incrementalBackup(backupInfo) #do incremental backup
#remove old log files
removeOldLogs(backupInfo)
def runCommand(cmd,dryRun,stream):
if dryRun:
print cmd
else:
#run the command
process=sp.Popen(cmd,stdout=sp.PIPE,stderr=sp.PIPE)
stdout,stderr=process.communicate()
stream.write(stdout)
stream.write(stderr)
#if there was an error
returnCode=process.returncode
if returnCode!=0:
return False
return True
def restore(backupInfo,pathToRestore,time,pathToRestoreTo):
cmd=["duplicity","--no-encryption"]
if pathToRestore!=None:
cmd.append("--file-to-restore")
cmd.append(pathToRestore)
cmd.append("-t")
cmd.append(time)
if pathToRestoreTo!=None:
pathToRestoreTo=pathToRestoreTo
else :
pathToRestoreTo=backupInfo.fromDirectory
cmd.append(backupInfo.toDirectory)
cmd.append(pathToRestoreTo)
if not runCommand(cmd,backupInfo.dryRun,sys.stdout):
msg=str(__name__)+":"+str(restore.__name__)+": error restoring backup!"
print msg
return False
return True
def getDaysSinceLastLogFile(backupInfo):
"""Returns days since last log file
It includes fractional part of days as a decimal and is accurate to the second.
"""
#get most recent log file date
logFiles=glob.glob(backupInfo.logFileDirectory+"*.log")
logFileDateTime=datetime.datetime.min
if len(logFiles)==0:
return None
for logFile in logFiles:
dateLogFile=logFile.rpartition('/')#remove path
dateLogFile=dateLogFile[2].rstrip('.log')#remove extension
fileDate=dateLogFile.split("-")#break up into year/month/day/hour/min/second
logFileDateTimeTemp=datetime.datetime(int(fileDate[0]),int(fileDate[1])
,int(fileDate[2]),int(fileDate[3]),int(fileDate[4]),int(fileDate[5]))
if logFileDateTimeTemp>logFileDateTime:
logFileDateTime=logFileDateTimeTemp
#take difference with today's date
todaysDateTime=datetime.datetime.now()
timeSinceLastLogFile=(todaysDateTime-logFileDateTime)
return timeSinceLastLogFile.total_seconds()/secondsPerDay
def getLastLogFileName(backupInfo):
#get most recent log file date
logFiles=glob.glob(backupInfo.logFileDirectory+"*.log")
logFileDateTime=datetime.datetime.min
if len(logFiles)==0:
return None
for logFile in logFiles:
dateLogFile=logFile.rpartition('/')#remove path
dateLogFile=dateLogFile[2].rstrip('.log')#remove extension
fileDate=dateLogFile.split("-")#break up into year/month/day/hour/min/second
logFileDateTimeTemp=datetime.datetime(int(fileDate[0]),int(fileDate[1])
,int(fileDate[2]),int(fileDate[3]),int(fileDate[4]),int(fileDate[5]))
if logFileDateTimeTemp>logFileDateTime:
logFileDateTime=logFileDateTimeTemp
logFileName=str(logFileDateTime.date())+"-"+str(logFileDateTime.hour)+"-" \
+str(logFileDateTime.minute)+"-"+str(logFileDateTime.second)+".log"
return os.path.join(backupInfo.logFileDirectory,logFileName)
def fullBackup(backupInfo):
#does a full backup
#get today's date
todaysDateTime=datetime.datetime.now()
#make log file name with full path
logFileName=str(todaysDateTime.date())+"-"+str(todaysDateTime.hour)+"-" \
+str(todaysDateTime.minute)+"-"+str(todaysDateTime.second)+".log"
logFileWithPath=backupInfo.logFileDirectory+logFileName
#create duplicity command
cmd=["duplicity","full","--no-encryption","--allow-source-mismatch"]
if backupInfo.pathsToInclude != None:
for path in backupInfo.pathsToInclude:
cmd.append("--include")
cmd.append(path)
if backupInfo.pathsToExclude != None:
for path in backupInfo.pathsToExclude:
cmd.append("--exclude")
cmd.append(path)
cmd.append(backupInfo.fromDirectory)
cmd.append(backupInfo.toDirectory)
f=open(logFileWithPath,'a')
if not backupInfo.dryRun:
f.write("====================FULL BACK UP====================\n")
if not runCommand(cmd,backupInfo.dryRun,f):
msg=str(__name__)+":"+str(fullBackup.__name__)+": error performing full backup!"
f.write(msg)
f.close()
return False
f.close()
#remove old backups
removeOldBackups(backupInfo) #remove all backups before y full backups
cleanUp(backupInfo) #clean up left over stuff
return True
def incrementalBackup(backupInfo):
#get most recent log file name
logFileName=getLastLogFileName(backupInfo)
cmd=["duplicity","incremental","--no-encryption","--allow-source-mismatch"]
if backupInfo.pathsToInclude != None:
for path in backupInfo.pathsToInclude:
cmd.append("--include")
cmd.append(path)
if backupInfo.pathsToExclude != None:
for path in backupInfo.pathsToExclude:
cmd.append("--exclude")
cmd.append(path)
cmd.append(backupInfo.fromDirectory)
cmd.append(backupInfo.toDirectory)
f=open(logFileName,'a')
if not backupInfo.dryRun:
f.write("\n=================INCREMENTAL BACK UP=================\n")
if not runCommand(cmd,backupInfo.dryRun,f):
msg=str(__name__)+":"+str(incrementalBackup.__name__)+": error performing incremental backup!\n"
f.write(str(datetime.datetime.now())+" UTC")
f.write(msg)
f.close()
return False
f.close()
return True
def removeOldBackups(backupInfo):
cmd=["duplicity","remove-all-but-n-full",str(backupInfo.nBackupsToKeep),"--force","--no-encryption"]
#get most recent log file name
logFileName=getLastLogFileName(backupInfo)
cmd.append(backupInfo.toDirectory)
f=open(logFileName,'a')
if not backupInfo.dryRun:
f.write("\n=================REMOVE OLD BACK UPS=================\n")
if not runCommand(cmd,backupInfo.dryRun,f):
msg=str(__name__)+":"+str(fullBackup.__name__)+": error removing old backups!"
f.write(msg)
f.close()
return False
f.close()
return True
def cleanUp(backupInfo):
cmd=["duplicity","cleanup","--extra-clean","--force","--no-encryption"]
#get most recent log file name
logFileName=getLastLogFileName(backupInfo)
cmd.append(backupInfo.toDirectory)
f=open(logFileName,'a')
if not backupInfo.dryRun:
f.write("\n=================CLEANUP=================\n")
if not runCommand(cmd,backupInfo.dryRun,f):
msg=str(__name__)+":"+str(fullBackup.__name__)+": error cleaning up!"
f.write(msg)
f.close()
return False
f.close()
return True
def mysqldump(backupInfo):
os.system("mysqldump -u "+backupInfo.mySQLUser+" -p"+backupInfo.mySQLPass+" --all-databases>"+backupInfo.mySQLDumpPath\
+"mysql_all_databases_dump.sql");
if __name__=="__main__":
main()