summaryrefslogtreecommitdiff
path: root/example.py
blob: 39e34cc2362f77a0e6a3687ac65a53e85a8a84be (plain)
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
#!/usr/bin/python3

import argparse
import logging
import sys

from lbup.repository import Repo
from lbup.ssh_remote import SSHRemote
from lbup.targets    import TargetLocal, TargetSSH, TargetSSHLXCLVM

def list_targets(tgts):
    for tgt in tgts:
        sys.stdout.write('%s\n  %s\n' % (tgt.name, str(tgt)))

# parse the commandline
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--list-targets', action = 'store_true')
parser.add_argument('-n', '--dry-run', action = 'store_true')
parser.add_argument('-v', '--verbose', action = 'count', default = 0)
parser.add_argument('targets', nargs = argparse.REMAINDER)
args = parser.parse_args()

# configure logging
logging.basicConfig(level = max(3 - args.verbose, 0) * 10)
# define the backup targets
tgts = (
    #TargetLocal('local', dirs = ['/usr/local/share']),
)


if args.list_targets:
    list_targets(tgts)
    sys.exit(0)

if len(args.targets) > 0:
    tgts_run = []
    for requested in args.targets:
        try:
            tgts_run.append(next(filter(lambda tgt: tgt.name == requested, tgts)))
        except StopIteration:
            sys.stderr.write('Requested target "%s" not defined\n' % requested);
            sys.exit(1)
else:
    tgts_run = tgts

repo = Repo()
res = repo.backup(tgts_run, dry_run = args.dry_run)
for tgt, tgt_res in res.target_results.items():
    if tgt_res.success:
        continue

    logging.error('Backing up target "%s" failed', tgt)
    if tgt_res.output:
        output = tgt_res.output.decode('utf-8', errors = 'backslashreplace')
        output = '\n'.join(('  ' + line for line in output.splitlines()))
        logging.error('Target output:')
        logging.error('%s', output)

if not res.par2_result.success:
    sys.stderr.write('Generating par2 recovery information failed with code %d, bup output:\n' %
                     (res.par2_result.retcode))
    sys.stderr.write(res.par2_result.output.decode('utf-8', errors = 'backslashreplace') + '\n')