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