2 from __future__ import absolute_import
3 from calendar import timegm
4 from subprocess import check_call
5 from time import strptime
6 import os, sys, tempfile
8 from bup import git, helpers, options
9 from bup.compat import argv_bytes
10 from bup.helpers import (log,
17 bup import-duplicity [-n] <duplicity-source-url> <bup-save-name>
19 n,dry-run don't do anything; just print what would be done
25 log(shstr(cmd).decode(errors='backslashreplace') + '\n')
27 def exc(cmd, shell=False):
30 check_call(cmd, shell=shell)
32 def exo(cmd, shell=False, preexec_fn=None, close_fds=True):
35 return helpers.exo(cmd, shell=shell, preexec_fn=preexec_fn,
36 close_fds=close_fds)[0]
38 def redirect_dup_output():
46 log('\nbup: import-duplicity is EXPERIMENTAL (proceed with caution)\n\n')
48 o = options.Options(optspec)
49 opt, flags, extra = o.parse_bytes(argv[1:])
52 if len(extra) < 1 or not extra[0]:
53 o.fatal('duplicity source URL required')
54 if len(extra) < 2 or not extra[1]:
55 o.fatal('bup destination save name required')
57 o.fatal('too many arguments')
59 source_url, save_name = extra
60 source_url = argv_bytes(source_url)
61 save_name = argv_bytes(save_name)
62 bup_path = bup.path.exe()
64 git.check_repo_or_die()
66 tmpdir = tempfile.mkdtemp(prefix=b'bup-import-dup-')
68 dup = [b'duplicity', b'--archive-dir', tmpdir + b'/dup-cache']
69 restoredir = tmpdir + b'/restore'
70 tmpidx = tmpdir + b'/index'
73 exo(dup + [b'collection-status', b'--log-fd=3', source_url],
74 close_fds=False, preexec_fn=redirect_dup_output) # i.e. 3>&1 1>&2
75 # Duplicity output lines of interest look like this (one leading space):
76 # full 20150222T073111Z 1 noenc
77 # inc 20150222T073233Z 1 noenc
79 for line in collection_status.splitlines():
80 if line.startswith(b' inc '):
81 assert(len(line) >= len(b' inc 20150222T073233Z'))
82 dup_timestamps.append(line[5:21])
83 elif line.startswith(b' full '):
84 assert(len(line) >= len(b' full 20150222T073233Z'))
85 dup_timestamps.append(line[6:22])
86 for i, dup_ts in enumerate(dup_timestamps):
87 tm = strptime(dup_ts.decode('ascii'), '%Y%m%dT%H%M%SZ')
88 exc([b'rm', b'-rf', restoredir])
89 exc(dup + [b'restore', b'-t', dup_ts, source_url, restoredir])
90 exc([bup_path, b'index', b'-uxf', tmpidx, restoredir])
91 exc([bup_path, b'save', b'--strip', b'--date', b'%d' % timegm(tm),
92 b'-f', tmpidx, b'-n', save_name, restoredir])
95 exc([b'rm', b'-rf', tmpdir])
98 log('warning: %d errors encountered\n' % len(saved_errors))