forked from turnkeylinux/tklbam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicity.py
269 lines (196 loc) · 8.03 KB
/
duplicity.py
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
#
# Copyright (c) 2010-2015 Liraz Siri <[email protected]>
#
# This file is part of TKLBAM (TurnKey GNU/Linux BAckup and Migration).
#
# TKLBAM is open source 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 3 of
# the License, or (at your option) any later version.
#
import os
from os.path import *
import sys
from subprocess import *
from squid import Squid
from utils import AttrDict, iamroot
import resource
RLIMIT_NOFILE_MAX = 8192
def _find_duplicity_pylib(path):
if not isdir(path):
return None
for fpath, dnames, fnames in os.walk(path):
if 'duplicity' in dnames:
return fpath
return None
PATH_DEPS = os.environ.get('TKLBAM_DEPS', '/usr/lib/tklbam/deps')
PATH_DEPS_BIN = join(PATH_DEPS, "bin")
PATH_DEPS_PYLIB = _find_duplicity_pylib(PATH_DEPS)
from cmd_internal import fmt_internal_command
class Error(Exception):
pass
class Duplicity:
"""low-level interface to Duplicity"""
def __init__(self, *args):
"""Duplicity command. The first member of args can be a an array of tuple arguments"""
if isinstance(args[0], list):
opts = args[0][:]
args = args[1:]
else:
opts = []
if not args:
raise Error("no arguments!")
if iamroot():
opts += [ ('archive-dir', '/var/cache/duplicity') ]
opts = [ "--%s=%s" % (key, val) for key, val in opts ]
self.command = ["duplicity"] + opts + list(args)
def run(self, passphrase, creds=None, debug=False):
sys.stdout.flush()
if creds:
if creds.type in ('devpay', 'iamuser'):
os.environ['AWS_ACCESS_KEY_ID'] = creds.accesskey
os.environ['AWS_SECRET_ACCESS_KEY'] = creds.secretkey
os.environ['X_AMZ_SECURITY_TOKEN'] = (",".join([creds.producttoken,
creds.usertoken])
if creds.type == 'devpay'
else creds.sessiontoken)
elif creds.type == 'iamrole':
os.environ['AWS_STSAGENT'] = fmt_internal_command('stsagent')
if PATH_DEPS_BIN not in os.environ['PATH'].split(':'):
os.environ['PATH'] = PATH_DEPS_BIN + ':' + os.environ['PATH']
if PATH_DEPS_PYLIB:
pythonpath = os.environ.get('PYTHONPATH')
pythonpath = ((PATH_DEPS_PYLIB + ':' + pythonpath)
if pythonpath else PATH_DEPS_PYLIB)
os.environ['PYTHONPATH'] = pythonpath
os.environ['PASSPHRASE'] = passphrase
if debug:
print """
The --debug option has dropped you into an interactive shell in which you can
explore the state of the system just before the above duplicity command is
run, and/or execute it manually.
For Duplicity usage info, options and storage backends, run "duplicity --help".
To exit from the shell and continue running duplicity "exit 0".
To exit from the shell and abort this session "exit 1".
"""
import executil
shell = os.environ.get("SHELL", "/bin/bash")
if shell == "/bin/bash":
shell += " --norc"
executil.system(shell)
child = Popen(self.command)
del os.environ['PASSPHRASE']
exitcode = child.wait()
if exitcode != 0:
raise Error("non-zero exitcode (%d) from backup command: %s" % (exitcode, str(self)))
def __str__(self):
return " ".join(self.command)
def _raise_rlimit(type, newlimit):
soft, hard = resource.getrlimit(type)
if soft > newlimit:
return
if hard > newlimit:
return resource.setrlimit(type, (newlimit, hard))
try:
resource.setrlimit(type, (newlimit, newlimit))
except ValueError:
return
class Target(AttrDict):
def __init__(self, address, credentials, secret):
AttrDict.__init__(self)
self.address = address
self.credentials = credentials
self.secret = secret
class Downloader(AttrDict):
"""High-level interface to Duplicity downloads"""
CACHE_SIZE = "50%"
CACHE_DIR = "/var/cache/tklbam/restore"
def __init__(self, time=None, cache_size=CACHE_SIZE, cache_dir=CACHE_DIR):
AttrDict.__init__(self)
self.time = time
self.cache_size = cache_size
self.cache_dir = cache_dir
def __call__(self, download_path, target, debug=False, log=None, force=False):
if log is None:
log = lambda s: None
if self.time:
opts = [("restore-time", self.time)]
else:
opts = []
if iamroot():
log("// started squid: caching downloaded backup archives to " + self.cache_dir + "\n")
squid = Squid(self.cache_size, self.cache_dir)
squid.start()
orig_env = os.environ.get('http_proxy')
os.environ['http_proxy'] = squid.address
_raise_rlimit(resource.RLIMIT_NOFILE, RLIMIT_NOFILE_MAX)
args = [ '--s3-unencrypted-connection', target.address, download_path ]
if force:
args = [ '--force' ] + args
command = Duplicity(opts, *args)
log("# " + str(command))
command.run(target.secret, target.credentials, debug=debug)
if iamroot():
if orig_env:
os.environ['http_proxy'] = orig_env
else:
del os.environ['http_proxy']
log("\n// stopping squid: download complete so caching no longer required\n")
squid.stop()
sys.stdout.flush()
class Uploader(AttrDict):
"""High-level interface to Duplicity uploads"""
VOLSIZE = 25
FULL_IF_OLDER_THAN = "1M"
S3_PARALLEL_UPLOADS = 1
def __init__(self,
verbose=True,
volsize=VOLSIZE,
full_if_older_than=FULL_IF_OLDER_THAN,
s3_parallel_uploads=S3_PARALLEL_UPLOADS,
includes=[],
include_filelist=None,
excludes=[],
):
AttrDict.__init__(self)
self.verbose = verbose
self.volsize = volsize
self.full_if_older_than = full_if_older_than
self.s3_parallel_uploads = s3_parallel_uploads
self.includes = includes
self.include_filelist = include_filelist
self.excludes = excludes
def __call__(self, source_dir, target, force_cleanup=True, dry_run=False, debug=False, log=None):
if log is None:
log = lambda s: None
opts = []
if self.verbose:
opts += [('verbosity', 5)]
if force_cleanup:
cleanup_command = Duplicity(opts, "cleanup", "--force", target.address)
log(cleanup_command)
if not dry_run:
cleanup_command.run(target.secret, target.credentials)
log("\n")
opts += [('volsize', self.volsize),
('full-if-older-than', self.full_if_older_than),
('gpg-options', '--cipher-algo=aes')]
for include in self.includes:
opts += [ ('include', include) ]
if self.include_filelist:
opts += [ ('include-filelist', self.include_filelist) ]
for exclude in self.excludes:
opts += [ ('exclude', exclude) ]
args = [ '--s3-unencrypted-connection', '--allow-source-mismatch' ]
if dry_run:
args += [ '--dry-run' ]
if self.s3_parallel_uploads > 1:
s3_multipart_chunk_size = self.volsize / self.s3_parallel_uploads
if s3_multipart_chunk_size < 5:
s3_multipart_chunk_size = 5
args += [ '--s3-use-multiprocessing', '--s3-multipart-chunk-size=%d' % s3_multipart_chunk_size ]
args += [ source_dir, target.address ]
backup_command = Duplicity(opts, *args)
log(str(backup_command))
backup_command.run(target.secret, target.credentials, debug=debug)
log("\n")