-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsnapshot-once.py
executable file
·349 lines (318 loc) · 12.7 KB
/
rsnapshot-once.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
#!/usr/bin/python3
#########################################################################################
# rsnapshot-once python #
# Copyright (C) 2017 Jochen Bauer <[email protected]> #
#########################################################################################
# Wrapper script for rsnapshot. Based on the php version of rsnapshot-once by
# Philipp Heckel.
#
# Original blog post at:
# http://blog.philippheckel.com/2013/06/28/script-run-rsnapshot-backups-only-once-and-rollback-failed-backups-using-rsnapshot-once/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Usage:
rsnapshot-once [-c CFGFILE] (sync|hourly [<N>]|daily|weekly|monthly)
rsnapshot-once -h
Options:
-c CFGFILE specify the configfile that rsnapshot should use
[default: /etc/rsnapshot.conf]
-h display help
By passing the <N> argument with hourly, you can adjust the hourly intervals. Defaults
to 6 if not given, meaning every 4 hours. This script will, as it is designed for now,
run in systemd environments. You can use it in non systemd environments by removing or
adjusting the wakeup-part.
"""
import os.path
import logging
import sys
import re
import datetime
import subprocess
from time import strftime
from shutil import rmtree
from systemd import journal
from docopt import docopt
from natsort import versorted
RSNAPSHOT_BINARY = "/usr/local/bin/rsnapshot"
def logf(logstring, logfile=None, prefix=""):
'''Logs in rsnapshot's logfile when existing'''
if logfile:
logstring = prefix + logstring
try:
with open(logfile, 'a') as lf:
lf.write(logstring + "\n")
except FileNotFoundError:
logging.critical("Specified logfile does not exist")
logging.info("Writing to logfile: "+logstring)
return True
def logft(logstring, logfile=None, prefix=""):
'''Logs with timestamp'''
logstring = strftime("[%Y-%m-%dT%H:%M:%S] ") + logstring
logf(logstring, logfile, prefix)
return True
def abortlog(logfile):
'''Logs aborting the script'''
logft("## BACKUP ABORTED #######################\n", logfile)
def uptime():
'''Gets the machine uptime from /proc'''
with open('/proc/uptime', 'r') as procUptime:
uptime_seconds = float(procUptime.readline().split()[0])
return uptime_seconds
def removepid(pidfile, logfile=None, prefix=""):
'''removes the pidfile of rsnapshot-once'''
logft("Removing rsnapshot-once pidfile at "+pidfile+" (CLEAN EXIT).", logfile, prefix)
os.remove(pidfile)
def parseConfig(configpath, configfile):
'''recursively parses the rsnapshot configs'''
abspath = configpath+"/"+configfile
logfile = None
syncFirst = None
snapshotRoot = None
with open(abspath, "r") as cf:
for line in cf:
if line.lower().startswith("include_conf"):
referencedpath = os.path.split(line.strip().split("\t")[1])[0]
referencedfile = os.path.split(line.strip().split("\t")[1])[1]
if not os.path.isabs(referencedpath):
logft("referenced file is given by relative path. This might lead to errors \
depending on where this script has been invoked from", LOGFILE)
referencedpath = configpath +"/"+ referencedpath
logft("referenced file: "+referencedpath+"/"+referencedfile, LOGFILE)
lf, sf, sr = parseConfig(referencedpath, referencedfile)
if lf != None:
logfile = lf
if sf != None:
syncFirst = sf
if sr != None:
snapshotRoot = sr
if line.lower().startswith("logfile"):
logfile = line.strip().split("\t")[1]
if line.lower().startswith("snapshot_root"):
snapshotRoot = line.strip().split("\t")[1]
if snapshotRoot.strip()[-1] != "/":
snapshotRoot = None
logft("Snapshot has no trailing slash", logfile)
if line.lower().startswith("sync_first"):
syncFirst = line.strip().split("\t")[1]
logging.debug("Syncfirst: "+syncFirst)
if syncFirst == 1:
break
logging.debug("return: " + " " + str(logfile) + " " + str(syncFirst) + " " + str(snapshotRoot))
return logfile, syncFirst, snapshotRoot
ARGS = docopt(__doc__, version='0.0.1-alpha')
logging.basicConfig(stream=sys.stdout, level=logging.INFO, formatter=logging.BASIC_FORMAT)
logging.debug("Parsed arguments:\n" + str(ARGS))
# getting snapshotroot and logfile
SNAPSHOT_ROOT = None
LOGFILE = None
SYNC_FIRST = None
CONFIGFILE = ARGS.get("-c")
CONFIGPATH = os.path.split(os.path.abspath(CONFIGFILE))[0]
CONFIGFILE = os.path.split(os.path.abspath(CONFIGFILE))[1]
logft("Configpath: "+CONFIGPATH)
try:
LOGFILE, SYNC_FIRST, SNAPSHOT_ROOT = parseConfig(CONFIGPATH, CONFIGFILE)
if not LOGFILE:
logft("No logfile entry in settings file. Is this intended?", LOGFILE)
except FileNotFoundError:
logft("Specified settings file does not exist", LOGFILE)
raise SystemExit(-1)
if SYNC_FIRST == "1":
logft("Rsnapshot is configured to use sync_fist. This is not supported for now.", LOGFILE)
abortlog(LOGFILE)
raise SystemExit(-1)
if not SNAPSHOT_ROOT:
logft("No valid snapshot root entry in settings file. Aborting", LOGFILE)
abortlog(LOGFILE)
raise SystemExit(-1)
logft("## STARTING BACKUP ######################", LOGFILE)
# get command
COMMAND = None
for k, v in ARGS.items():
if isinstance(v, bool) and v:
COMMAND = k
if COMMAND != "sync":
# Check pid file
PIDFILE = SNAPSHOT_ROOT + ".rsnapshot-once.pid"
logft("Checking rsnapshot-once pidfile at " + PIDFILE + " ... ", LOGFILE)
try:
with open(PIDFILE) as pf:
pid = pf.read().strip()
if os.path.isfile("/proc/" + pid):
logf("Process " + pid + " still running. Aborting", LOGFILE)
abortlog(LOGFILE)
raise SystemExit(-1)
logf("PID file exists but process " + pid + " is not running. Script crashed before", LOGFILE)
sortedBackups = []
for entry in os.listdir(SNAPSHOT_ROOT):
if COMMAND in entry:
sortedBackups.append(entry)
# hack for sorting with natsort 3.5.1: User versorted to handle the dot
sortedBackups = versorted(sortedBackups)
sortedBackupsCount = len(sortedBackups)
if sortedBackupsCount == 0:
logft("No previous backups found. No cleanup necessary.", LOGFILE)
else:
logft("Cleaning up unfinished backup ...", LOGFILE)
firstBackup = sortedBackups.pop(0)
logft("Deleting " + firstBackup + " ...", LOGFILE)
# double check before deleting (!)
regExpMatches = re.search(r'^(.+)\.(\d+)$', firstBackup)
if not os.path.isdir(SNAPSHOT_ROOT + firstBackup) or len(firstBackup) < 2 or not regExpMatches:
logf("Script security issue. EXITING.", LOGFILE)
abortlog(LOGFILE)
raise SystemExit(-1)
else:
# Delete
try:
rmtree(SNAPSHOT_ROOT + firstBackup)
logf("Done", LOGFILE)
except:
logft("Unexpected error " + str(sys.exc_info()[0]) + " on deleting " + SNAPSHOT_ROOT + firstBackup, LOGFILE)
raise SystemExit(-1)
sortedBackupsCount = len(sortedBackups)
while sortedBackupsCount > 0:
backup = sortedBackups.pop(0)
sortedBackupsCount = len(sortedBackups)
#logging.debug("Backup: "+backup)
regExpMatches = re.search(r"^(.+)\.(\d+)$", backup)
if regExpMatches:
#logging.debug("M: "+str(m))
#logging.debug("M1: " + regExpMatches.group(1)+" M2: "+regExpMatches.group(2))
#logging.debug("Prev: "+previousBackup)
previousBackup = regExpMatches.group(1)+"."+str(int(regExpMatches.group(2))-1)
logft("Moving "+backup+" to " +previousBackup+" ... ")
try:
os.rename(SNAPSHOT_ROOT+backup, SNAPSHOT_ROOT+previousBackup)
logft("DONE", LOGFILE)
except OSError:
logft("Could not rename directory. Target already exists")
abortlog(LOGFILE)
raise SystemExit(-1)
except FileNotFoundError:
logft("Does not exist. Last backup was clean.", LOGFILE)
logft("Checking delays (minimum 15 minutes since startup/wakeup) ...", LOGFILE)
upMinutes = int(uptime()//60)
if upMinutes < 15:
logft("- Computer uptime is "+str(upMinutes)+" minutes. NOT ENOUGH. EXITING.", LOGFILE)
abortlog(LOGFILE)
raise SystemExit(-1)
else:
logft("- Computer uptime is "+str(upMinutes)+" minutes. THAT'S OKAY.", LOGFILE)
# Get time of resume from systemd journal
j = journal.Reader()
j.this_boot()
j.add_match(SYSLOG_IDENTIFIER="systemd-sleep")
j.seek_tail()
ENTRY = j.get_previous()
WAKEUPMINUTES = None
if ENTRY:
while ENTRY["MESSAGE"] != "System resumed.":
ENTRY = j.get_previous()
if not ENTRY:
break
if ENTRY:
timediff = datetime.datetime.now() - ENTRY["__REALTIME_TIMESTAMP"]
WAKEUPMINUTES = timediff.seconds // 60
if WAKEUPMINUTES:
if WAKEUPMINUTES < 15:
logft("- Computer wakeup time is "+str(WAKEUPMINUTES)+" minutes. NOT ENOUGH. EXITING.", LOGFILE)
abortlog(LOGFILE)
raise SystemExit(-1)
else:
logft("- Computer wakeup time is "+str(WAKEUPMINUTES)+" minutes. THAT'S OKAY.", LOGFILE)
# Get date of newest folder (e.g. weekly.0, daily.0, monthly.0)
# to figure out if the job needs to run
NEEDSTORUN = False
NEWESTBACKUP = SNAPSHOT_ROOT + COMMAND + ".0"
if not os.path.isdir(NEWESTBACKUP):
logft("No backup exists for job "+COMMAND+" at "+NEWESTBACKUP, LOGFILE)
NEEDSTORUN = True
else:
BACKUPTIME = datetime.datetime.fromtimestamp(os.path.getmtime(NEWESTBACKUP))
logft("Last backup for job "+COMMAND+" at "+NEWESTBACKUP+" was at "+str(BACKUPTIME), LOGFILE)
TIMESINCELAST = None
TIMEMIN = None
if COMMAND == "hourly":
intervalsPerDay = ARGS.get("<N>")
if intervalsPerDay is None:
intervalsPerDay = 6 # the default
if int(intervalsPerDay) > 24 or int(intervalsPerDay) < 2:
logft("Invalid interval for hourly given. Must be between 2 and 24. Aborting.", LOGFILE)
abortlog(LOGFILE)
raise SystemExit
TIMEMIN = datetime.timedelta(minutes=(24/intervalsPerDay)*60-5)
elif COMMAND == "daily":
TIMEMIN = datetime.timedelta(hours=23)
elif COMMAND == "weekly":
TIMEMIN = datetime.timedelta(days=6, hours=22)
elif COMMAND == "monthly":
TIMEMIN = datetime.timedelta(days=28)
else:
logft("Error: This should not happen. ERROR.", LOGFILE)
abortlog(LOGFILE)
raise SystemExit(-1)
TIMESINCELAST = (datetime.datetime.now() - BACKUPTIME)
NEEDSTORUN = TIMESINCELAST > TIMEMIN
logging.debug("Time since last: "+str(TIMESINCELAST))
logging.debug("Mintime: "+str(TIMEMIN))
if not NEEDSTORUN:
logft("Job does NOT need to run. Last run is only "+str(TIMESINCELAST)+" ago (min. is "+str(TIMEMIN)+") EXITING.", LOGFILE)
abortlog(LOGFILE)
raise SystemExit
else:
logft("Last run is "+str(TIMESINCELAST)+" ago (min. is "+str(TIMEMIN)+")", LOGFILE)
PID = os.getpid()
logft("Writing rsnapshot-once pidfile (PID "+str(PID)+") to "+PIDFILE)
with open(PIDFILE, "w") as pf:
pf.write(str(PID))
SYSCMD = RSNAPSHOT_BINARY+" -c "+ARGS.get("-c")+" "+COMMAND
logft("NOW RUNNING JOB: "+SYSCMD+"\n", LOGFILE)
SYSCMD = SYSCMD.split(" ")
EXITCODE = 0
CONFIGERROR = False
COMPLETEPROCESS = None
try:
COMPLETEPROCESS = subprocess.check_output(SYSCMD, universal_newlines=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as retVal:
EXITCODE = retVal.returncode
COMPLETEPROCESS = retVal.output
for index, LINE in enumerate(COMPLETEPROCESS.split("\n")):
if index == 0:
logft(" captured output: "+LINE,LOGFILE,"\n")
else:
logft(" captured output: "+LINE,LOGFILE)
#logging.info(" captured output: "+LINE)
if "rsnapshot encountered an error" in LINE:
CONFIGERROR = True
if CONFIGERROR:
logft("Exiting rsnapshot-once, because error in rsnapshot config detected.", LOGFILE, "\n")
removepid(PIDFILE, LOGFILE)
abortlog(LOGFILE)
raise SystemExit(-1)
# pidfile should NOT exist if exit was clean
if EXITCODE == 1: # 1 means 'fatal error' in rsnapshot terminology
logft("No clean exit. Backup aborted. Cleanup necessary on next run (DIRTY EXIT).", LOGFILE, "\n")
abortlog(LOGFILE)
raise SystemExit(-1)
elif EXITCODE == 2: # 2 means that warnings ocurred
logft("Rsnapshot encountered warnings, this is worth a look at the log file", LOGFILE, "\n")
removepid(PIDFILE, LOGFILE, "\n")
logft("## BACKUP COMPLETE ######################\n", LOGFILE)
else:
# sync is not supported in this context
logft("Sync is not supported as command", LOGFILE)
abortlog(LOGFILE)
raise SystemExit(-1)