]> arthur.barton.de Git - bup.git/blob - cmd/import-duplicity-cmd.py
Port import-duplicity to python
[bup.git] / cmd / import-duplicity-cmd.py
1 #!/usr/bin/env python
2
3 from calendar import timegm
4 from pipes import quote
5 from subprocess import check_call, check_output
6 from time import strftime, strptime
7 import sys
8 import tempfile
9
10 from bup import git, options, vfs
11 from bup.helpers import handle_ctrl_c, log, saved_errors, unlink
12 import bup.path
13
14 optspec = """
15 bup import-duplicity [-n] <duplicity-source-url> <bup-save-name>
16 --
17 n,dry-run  don't do anything; just print what would be done
18 """
19
20
21 def logcmd(cmd):
22     if isinstance(cmd, basestring):
23         log(cmd + '\n')
24     else:
25         log(' '.join(map(quote, cmd)) + '\n')
26
27
28 def exc(cmd, shell=False):
29     global opt
30     logcmd(cmd)
31     if not opt.dry_run:
32         check_call(cmd, shell=shell)
33
34 def exo(cmd, shell=False):
35     global opt
36     logcmd(cmd)
37     if not opt.dry_run:
38         return check_output(cmd, shell=shell)
39
40
41 handle_ctrl_c()
42
43 o = options.Options(optspec)
44 opt, flags, extra = o.parse(sys.argv[1:])
45
46 if len(extra) < 1 or not extra[0]:
47     o.fatal('duplicity source URL required')
48 if len(extra) < 2 or not extra[1]:
49     o.fatal('bup destination save name required')
50 if len(extra) > 2:
51     o.fatal('too many arguments')
52
53 source_url, save_name = extra
54 bup = bup.path.exe()
55
56 git.check_repo_or_die()
57 top = vfs.RefList(None)
58
59 tmpdir = tempfile.mkdtemp(prefix='bup-import-dup-')
60 try:
61     dup = ['duplicity', '--archive-dir', tmpdir + '/dup-cache']
62     restoredir = tmpdir + '/restore'
63     tmpidx = tmpdir + '/index'
64     collection_status = \
65         exo(' '.join(map(quote, dup))
66             + ' collection-status --log-fd=3 %s 3>&1 1>&2' % quote(source_url),
67             shell=True)
68     # Duplicity output lines of interest look like this (one leading space):
69     #  full 20150222T073111Z 1 noenc
70     #  inc 20150222T073233Z 1 noenc
71     dup_timestamps = []
72     for line in collection_status.splitlines():
73         if line.startswith(' inc '):
74             assert(len(line) >= len(' inc 20150222T073233Z'))
75             dup_timestamps.append(line[5:21])
76         elif line.startswith(' full '):
77             assert(len(line) >= len(' full 20150222T073233Z'))
78             dup_timestamps.append(line[6:22])
79     for i, dup_ts in enumerate(dup_timestamps):
80         tm = strptime(dup_ts, '%Y%m%dT%H%M%SZ')
81         exc(['rm', '-rf', restoredir])
82         exc(dup + ['restore', '-t', dup_ts, source_url, restoredir])
83         exc([bup, 'index', '-uxf', tmpidx, restoredir])
84         exc([bup, 'save', '--strip', '--date', str(timegm(tm)), '-f', tmpidx,
85              '-n', save_name, restoredir])
86 finally:
87     exc(['rm', '-rf', tmpdir])
88
89 if saved_errors:
90     log('warning: %d errors encountered\n' % len(saved_errors))
91     sys.exit(1)