-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsya
executable file
·509 lines (411 loc) · 14.7 KB
/
sya
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
#!/usr/bin/env python3
#
# sya, a simple front-end to the borg backup software
# Copyright (C) 2016 Alexandre Rossi <[email protected]>
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import argparse
import errno
import glob
import logging
import os
import platform
import re
import traceback
import shutil
import socket
import subprocess
import sys
import tomli
DEFAULT_CONFDIR = "/etc/sya"
DEFAULT_USERCONFDIR = os.path.join(
os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "sya"
)
DEFAULT_CONFFILE = "config"
GLOBAL_PRESCRIPT_NAME = "pre.sh"
GLOBAL_POSTSCRIPT_NAME = "post.sh"
BINARY = shutil.which("borg")
class LockInUse(Exception):
pass
class ProcessLock(object):
# This class comes from this very elegant way of having a pid lock in order
# to prevent multiple instances from running on the same host.
# http://stackoverflow.com/a/7758075
def __init__(self, process_name):
self.pname = process_name
def acquire(self):
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
# The bind address is the one of an abstract UNIX socket (begins
# with a null byte) followed by an address which exists in the
# abstract socket namespace (Linux only). See unix(7).
self.socket.bind("\0" + self.pname)
except socket.error:
raise LockInUse
def release(self):
self.socket.close()
def run(path, args=None, env=None, dryrun=False):
if dryrun:
logging.info(
"$ %s %s"
% (
path,
" ".join(args or []),
)
)
return
cmdline = [path]
if args is not None:
cmdline.extend(args)
subprocess.check_call(cmdline, env=env)
def run_or_exit(path, args=None, env=None, dryrun=False):
try:
run(path, args, env, dryrun)
except subprocess.CalledProcessError as e:
sys.exit(e)
def isexec(path):
return os.path.isfile(path) and os.access(path, os.X_OK)
def check_matches(pattern):
if "**" in pattern:
return True
if glob.glob(pattern, recursive=True):
return True
else:
logging.warning(
"include/exclude pattern '%s' does not match " "anything." % pattern
)
return False
class BackupError(Exception):
pass
DATE_RE = re.compile(r"\-\d{4}\-\d{2}\-\d{2}")
class BorgRepository(object):
def __init__(self, allconf, confname, conf):
self.allconf = allconf
self.confname = confname
# Loading target dir
if "repository" not in conf:
raise BackupError("'repository' is mandatory for each task in config")
# check if we have a passphrase file
if "passphrase_file" in conf:
conf["passphrase_file"] = os.path.join(
self.allconf["confdir"], conf["passphrase_file"]
)
with open(conf["passphrase_file"]) as f:
conf["passphrase"] = f.readline().strip()
else:
conf["passphrase_file"] = ""
conf["passphrase"] = ""
if "ssh_key" in conf:
if conf["ssh_key"][-4:] == ".pub":
raise BackupError(
"Supplied ssh key '%s' seems to be a "
"public key, need private key." % conf["ssh_key"]
)
if not os.path.isfile(conf["ssh_key"]):
raise BackupError(
"Supplied ssh key '%s' is not a file." % conf["ssh_key"]
)
else:
conf["ssh_key"] = ""
self.conf = conf
def __borg_env(self):
env = {}
if self.conf["passphrase"]:
env["BORG_PASSPHRASE"] = self.conf["passphrase"]
borg_rsh = [
"ssh",
"-o ControlMaster=auto",
"-o ControlPath=~/.ssh/master-sya-%s-%s" % (platform.node(), self.confname),
"-o ControlPersist=20",
]
if self.conf["ssh_key"]:
borg_rsh.append("-i %s" % self.conf["ssh_key"])
env["BORG_RSH"] = " ".join(borg_rsh)
return env
def borg(self, command, args):
args.insert(0, command)
if self.allconf["verbose"]:
args.append("-v")
try:
run(BINARY, args, env=self.__borg_env(), dryrun=self.allconf["dryrun"])
except subprocess.CalledProcessError as e:
logging.error(e)
raise BackupError
def list_backups(self):
archives = subprocess.check_output(
(BINARY, "list", "--format", "{archive}{NL}", self.conf["repository"]),
env=self.__borg_env(),
)
archives = archives.decode(sys.getdefaultencoding()).strip().split("\n")
prefixes = set()
for archive in archives:
m = DATE_RE.search(archive)
if m:
prefixes.add(archive[: m.start(0)])
logging.debug(
"%s repository has the following prefixes: %s."
% (self.conf["repository"], prefixes)
)
return prefixes
def create(self, includes, excludes):
args = []
if self.allconf["verbose"]:
args.append("--stats")
if self.allconf["progress"]:
args.append("--progress")
if "remote-path" in self.conf:
args.append("--remote-path")
args.append(self.conf["remote-path"])
args.append(self.conf["repository"] + "::{hostname}-{now:%Y-%m-%dT%H:%M:%S}")
for include in includes:
args.append(include)
for exclude in excludes:
args.append("--exclude")
args.append(exclude)
self.borg("create", args)
def prune(self, prefix):
args = []
if self.allconf["verbose"]:
args.append("--list")
args.append("--stats")
for keep in (
"keep-daily",
"keep-weekly",
"keep-monthly",
):
if keep in self.conf:
args.append("--" + keep)
args.append(str(self.conf[keep]))
args.append("--glob-archives=%s*" % prefix)
args.append(self.conf["repository"])
self.borg("prune", args)
# prune alone does not free space, compact is needed
self.borg("compact", [self.conf["repository"]])
def check(self):
self.borg("check", [])
def process_task(conffile, task):
conf = conffile[task]
confdir = conffile["sya"]["confdir"]
dryrun = conffile["sya"]["dryrun"]
# Check if we want to run this backup task
if not conf["run_this"]:
logging.debug("! Task disabled. 'run_this' must be set to 'yes' in %s" % task)
return
# Load and execute if applicable pre-task commands
if "pre" in conf.keys() and isexec(conf["pre"]):
try:
run(os.path.join(confdir, conf["pre"]), None, dryrun=dryrun)
except subprocess.CalledProcessError as e:
logging.error(e)
return
repo = BorgRepository(conffile["sya"], task, conf)
# run the backup
if "paths" in conf or "include_file" in conf:
# Loading source paths
paths = None
if "paths" in conf:
paths = map(str.strip, conf["paths"].strip().split(","))
# include and exclude patterns
includes = [p for p in paths] if paths is not None else []
excludes = []
if "include_file" in conf:
with open(os.path.join(confdir, conf["include_file"])) as f:
for line in f.readlines():
if line.startswith("- "):
p = line[2:].strip()
if check_matches(p):
excludes.append(p)
else:
p = line.strip()
if check_matches(p):
includes.append(line.strip())
if "exclude_file" in conf:
with open(os.path.join(confdir, conf["exclude_file"])) as f:
for line in f.readlines():
p = line.strip()
if check_matches(p):
excludes.append(p)
try:
repo.create(includes, excludes)
except BackupError:
logging.error("'%s' backup failed. You should investigate." % task)
else:
logging.info("no 'paths' in configuration file %s, skipping backup" % task)
# run the pruning
if "keep-daily" in conf or "keep-weekly" in conf or "keep-monthly" in conf:
try:
prefixes = repo.list_backups()
except subprocess.CalledProcessError as e:
logging.error(e)
logging.error("'%s' prefix listing failed. Will not prune." % task)
raise BackupError
for prefix in prefixes:
logging.info("Pruning %s..." % prefix)
try:
repo.prune(prefix)
except BackupError:
logging.error(
"'%s' old backups cleanup failed. You should "
" investigate." % task
)
raise
# Load and execute if applicable post-task commands
if "post" in conf.keys() and isexec(conf["post"]):
try:
run(os.path.join(confdir, conf["post"]), None, dryrun=dryrun)
except subprocess.CalledProcessError as e:
logging.error(e)
def do_backup(conffile):
confdir = conffile["sya"]["confdir"]
dryrun = conffile["sya"]["dryrun"]
taskfilter = conffile["sya"]["taskfilter"]
lock = ProcessLock("sya" + confdir)
try:
lock.acquire()
except LockInUse:
logging.error("Another instance seems to be running on the same conf dir.")
sys.exit(1)
# Run global 'pre' script if it exists
prescript_path = os.path.join(confdir, GLOBAL_PRESCRIPT_NAME)
if isexec(prescript_path):
logging.debug("Running global pre-script '%s'." % prescript_path)
run_or_exit(prescript_path, None, dryrun=dryrun)
# Task loop
errors = []
for task in conffile.keys():
if task == "sya":
continue
if taskfilter != "*" and taskfilter != task:
continue
logging.info("-- Backing up using %s configuration..." % task)
try:
process_task(conffile, task)
except BackupError:
errors.append(task)
logging.error("-- Failed backing up %s." % task)
else:
logging.info("-- Done backing up %s." % task)
# Run global 'post' script if it exists
postcript_path = os.path.join(confdir, GLOBAL_POSTSCRIPT_NAME)
if isexec(postcript_path):
logging.debug("Running global post-script '%s'." % postcript_path)
run_or_exit(postcript_path, None, dryrun=dryrun)
lock.release()
if errors:
raise BackupError(errors)
def do_check(conffile):
taskfilter = conffile["sya"]["taskfilter"]
for task in conffile.keys():
if task == "sya":
continue
if taskfilter != "*" and taskfilter != task:
continue
logging.info("-- Checking using %s configuration..." % task)
repo = BorgRepository(conffile["sya"], task, conf)
try:
repo.check()
except BackupError:
logging.error("'%s' backup check failed. You should investigate." % task)
raise
logging.info("-- Done checking %s." % task)
if __name__ == "__main__":
usage = "usage: %prog [options] [actions]"
parser = argparse.ArgumentParser()
parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=None,
help="Be verbose and print stats.",
)
parser.add_argument(
"-p", "--progress", action="store_true", default=None, help="Show progress."
)
parser.add_argument(
"-d",
"--config-dir",
type=str,
dest="confdir",
help="Configuration directory, default is "
"%s or %s." % (DEFAULT_USERCONFDIR, DEFAULT_CONFDIR),
)
parser.add_argument(
"-t", "--task", type=str, default="*", help="Task to run, default is all."
)
parser.add_argument(
"-n",
"--dry-run",
action="store_true",
dest="dryrun",
default=False,
help="Do not run backup, don't act.",
)
parser.add_argument(
"actions",
metavar="ACTIONS",
type=str,
nargs="*",
default=(
"create",
"purge",
),
help="actions to perform among create, purge and check (default: create and purge)",
)
args = parser.parse_args()
logging.basicConfig(format="%(message)s", level=logging.WARNING)
confdir = None
if args.confdir:
confdir = args.confdir
elif os.path.isdir(DEFAULT_USERCONFDIR):
confdir = DEFAULT_USERCONFDIR
else:
confdir = DEFAULT_CONFDIR
if not os.path.isdir(confdir):
sys.exit("Configuration directory '%s' not found." % confdir)
conffile_path = os.path.join(confdir, DEFAULT_CONFFILE)
if not os.path.isfile(conffile_path):
sys.exit("Configuration file '%s' not found." % conffile_path)
with open(conffile_path, "rb") as f:
conffile = tomli.load(f)
conffile["sya"]["confdir"] = confdir
if args.verbose is not None:
conffile["sya"]["verbose"] = args.verbose
elif "verbose" not in conffile["sya"]:
conffile["sya"]["verbose"] = False
if args.progress is not None:
conffile["sya"]["progress"] = args.progress
elif "progress" not in conffile["sya"]:
conffile["sya"]["progress"] = False
conffile["sya"]["dryrun"] = args.dryrun
conffile["sya"]["taskfilter"] = args.task
if conffile["sya"]["verbose"]:
logging.getLogger().setLevel(logging.DEBUG)
errors = False
if "create" in args.actions or "purge" in args.actions:
try:
do_backup(conffile)
except BackupError:
errors = True
if "check" in args.actions:
try:
do_check(conffile)
except BackupError:
errors = True
logging.shutdown()
if errors:
sys.exit(errno.EIO)
else:
sys.exit(0)
# vim: ts=4 sw=4 expandtab