-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_duplicity_backups.py
executable file
·292 lines (239 loc) · 9.91 KB
/
copy_duplicity_backups.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
# vim: set fileencoding=utf-8 :
""" copy_duplicity_backups.py
Copies most recent duplicity backup files"""
# The MIT License (MIT)
#
# Copyright (c) 2013 Georg Lutz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Standard library imports:
import argparse
import datetime
import logging
import os
import re
import shutil
import sys
def get_args():
'''Configures command line parser and returns parsed parameters'''
parser = argparse.ArgumentParser(
description="Copies most recent duplicity backup files")
parser.add_argument("src", help="Source directory")
parser.add_argument("dst", help="Destination directory")
parser.add_argument(
"--dryrun",
action="store_true",
help="Do not write/delete files. Just print out.")
parser.add_argument(
"--maxsize",
help="Stop copying when dst folder has given size in MB. Default is\
0 (unlimited)",
default=0, type=int)
parser.add_argument(
"--nr",
help="Number of full backups (default is 2)",
default=2, type=int)
parser.add_argument(
"--quiet",
help="If set only errors are printed out",
action="store_true")
return parser.parse_args()
class UnknownFileException(Exception):
'''Exception when we detect a file not belonging to duplicity'''
def ts_regex(number):
'''Returns timestamp regex, matching group with given number, e.g.
"timestamp1" or "timestamp2" '''
if number is not None:
return "(?P<timestamp" + str(number) + r">\d{8}T\d{2}\d{4}[A-Z])"
return r"(?P<timestamp>\d{8}T\d{2}\d{4}[A-Z])"
def get_unix_timestamp(timestamp):
'''Returns a unix timestamp for given textual duplicity timestamp'''
time = datetime.datetime.strptime(timestamp, "%Y%m%dT%H%M%SZ")
return (time - datetime.datetime(1970, 1, 1)).total_seconds()
def add_entry(dup_files, timestamp, is_full, filename):
'''Adds an entry to the dup_files dictionnary.
Either a new entry with timestamp is created or an existing is updated.
Example:
dup_files = {"123456789" :
{ "is_full": True,
"files": ["file1", "file2", "file3"] } }
Raises an exception on error'''
if timestamp in dup_files:
assert dup_files[timestamp]["is_full"] == is_full
if "files" in dup_files[timestamp]:
dup_files[timestamp]["files"].append(filename)
else:
dup_files[timestamp]["files"] = [filename]
else:
entry = {}
entry["is_full"] = is_full
entry["files"] = [filename]
dup_files[timestamp] = entry
def get_duplicity_files(directory):
'''For a given directory returns files which belong to duplicity.
In case that a file is found not belonging to duplicity an exception
is raised.
Duplicity naming scheme:
duplicity-full.timestamp.manifest.gpg
duplicity-full.timestamp.volnr.difftar.gpg
duplicity-full-signatures.timestamp.sigtar.gpg
duplicity-inc.timestamp1.to.timestamp2.manifest.gpg
duplicity-inc.timestamp1.to.timestamp2.volnr.difftar.gpg
duplicity-new-signatures.timestamp1.to.timestamp2.sigtar.gpg
timestamp example: 20130126T070058Z'''
full_prefix = r"^duplicity\-full\." + ts_regex(None) + r"\."
full_manifest = re.compile(full_prefix + r"manifest\.gpg$")
full_difftar = re.compile(full_prefix + r"vol\d+\.difftar\.gpg$")
full_signatures = re.compile(
r"^duplicity\-full-signatures\." + ts_regex(None) + r"\.sigtar\.gpg$")
inc_prefix = r"^duplicity\-inc\." + ts_regex(1) + r"\.to\." + ts_regex(2) + r"\."
inc_manifest = re.compile(inc_prefix + r"manifest\.gpg$")
inc_difftar = re.compile(inc_prefix + r"vol\d+\.difftar\.gpg$")
inc_signatures = re.compile(
r"^duplicity\-new\-signatures\." + ts_regex(1) + r"\.to\."
+ ts_regex(2) + r"\.sigtar\.gpg$")
all_files = os.listdir(directory)
dup_files = {}
for name in all_files:
result = full_manifest.match(name)
if result is not None:
timestamp = get_unix_timestamp(result.group("timestamp"))
add_entry(dup_files, timestamp, True, name)
continue
result = full_difftar.match(name)
if result is not None:
timestamp = get_unix_timestamp(result.group("timestamp"))
add_entry(dup_files, timestamp, True, name)
continue
result = full_signatures.match(name)
if result is not None:
timestamp = get_unix_timestamp(result.group("timestamp"))
add_entry(dup_files, timestamp, True, name)
continue
result = inc_manifest.match(name)
if result is not None:
timestamp = get_unix_timestamp(result.group("timestamp2"))
add_entry(dup_files, timestamp, False, name)
continue
result = inc_difftar.match(name)
if result is not None:
timestamp = get_unix_timestamp(result.group("timestamp2"))
add_entry(dup_files, timestamp, False, name)
continue
result = inc_signatures.match(name)
if result is not None:
timestamp = get_unix_timestamp(result.group("timestamp2"))
add_entry(dup_files, timestamp, False, name)
continue
raise UnknownFileException(name)
return dup_files
def return_last_n_full_backups(directory, nr_full):
'''Returns list of duplicity files for last n full backups into the past
Args:
directory: Directory with duplicity backup files
nr_full: Number of full backups
Returns:
list of files without directory prefixed
'''
dup_files = get_duplicity_files(directory)
result = []
counter = 0
for key in sorted(iter(dup_files.keys()), reverse=True):
if counter < nr_full:
result = result + dup_files[key]["files"]
else:
break
if dup_files[key]["is_full"]:
counter = counter + 1
return result
def sync_files(src_dir, dst_dir, files, dryrun, max_size):
'''Actually copies the files
The files are copied if they are not existent in dst_dir or if
the size differs.
Any existing files in dst_dir not available in files list are deleted!
Args:
src_dir: Source directory, where to copy from
dst_dir: Destination directory, where to copy to
files: List of filenames without path
dryrun: If true only simulate and print out message
max_size: stop with error if dst_dir contains more than max_size bytes.
If max_size is 0 no limit is enforced.
Returns:
1 in case of any error, otherwise 0
'''
files_to_delete = []
files_to_copy = []
current_size = 0
dst_files = os.listdir(dst_dir)
for dst_file in dst_files:
if dst_file in files:
dst_size = os.path.getsize(os.path.join(dst_dir, dst_file))
src_size = os.path.getsize(os.path.join(src_dir, dst_file))
current_size += src_size
if dst_size != src_size:
files_to_delete.append(dst_file)
files_to_copy.append(dst_file)
else:
files_to_delete.append(dst_file)
for file_ in files:
if file_ not in dst_files:
files_to_copy.append(file_)
for file_ in files_to_delete:
dst_file = os.path.join(dst_dir, file_)
if dryrun:
print(("Would delete " + dst_file))
else:
logging.info("Delete %s", dst_file)
os.unlink(dst_file)
for file_ in files_to_copy:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
file_size = os.path.getsize(src_file)
if 0 < max_size < current_size + file_size:
print("Stopping at " + src_file + " .", file=sys.stderr)
print((
"Exceeds file size limit of %d bytes." % (max_size)), file=sys.stderr)
return 1
current_size += file_size
if dryrun:
print("Would copy " + src_file + " to " + dst_file)
else:
logging.info("Copy " + src_file + " to " + dst_file)
shutil.copyfile(src_file, dst_file)
return 0
def main():
'''main function, called when script file is executed directly'''
args = get_args()
if args.quiet:
logging.basicConfig(format="%(message)s", level=logging.WARNING)
else:
logging.basicConfig(format="%(message)s", level=logging.INFO)
if not os.path.isdir(args.src):
print("Directory \"" + args.src + "\" not found", file=sys.stderr)
sys.exit(1)
if not os.path.isdir(args.dst):
print("Directory \"" + args.src + "\" not found", file=sys.stderr)
sys.exit(1)
files = return_last_n_full_backups(args.src, args.nr)
max_size = args.maxsize * 1000000
return_code = sync_files(args.src, args.dst, files, args.dryrun, max_size)
sys.exit(return_code)
if __name__ == "__main__":
main()