-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzsync
executable file
·648 lines (551 loc) · 23.4 KB
/
zsync
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#!/usr/bin/env python
# Licensed under an MIT-style License:
#
# Copyright (c) 2012 Fog Creek Software, Inc.
#
# 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.
# Changelog
# ---------
# 2011-02-13 Tim Stewart <tim@stoo.org>
# * Initial Release
from __future__ import print_function
import os
import sys
import syslog
import shlex
import subprocess
import signal
import re
import textwrap
import select
import datetime
from datetime import datetime
from contextlib import contextmanager
class NonZeroExitStatus(Exception): pass
class InvalidDestinationError(Exception): pass
class NoSuchDatasetError(Exception): pass
class LockfileExists(Exception): pass
class SendNoSuchDataset(Exception): pass
class Zfs(object):
"""This class represents the overall ZFS system.
It can be used to get a list of all ZFS pools and datasets on the
system."""
@classmethod
def _datasets(cls, dset_type, filter, cmd_prefix="", cmd_postfix=""):
"""Internal implementation of dataset selection code."""
cmd = '%s zfs list -t %s -r -Ho name,creation -s creation %s' % (
cmd_prefix, dset_type, cmd_postfix)
stdout, stderr = cls._run_cmd(cmd)
lines = [line for line in stdout.splitlines() if line.strip()]
results = []
filter_re = re.compile(filter)
for line in lines:
cols = line.split('\t')
name = cols[0]
cdate = datetime.strptime(cols[1], '%a %b %d %H:%M %Y')
if filter_re.match(name):
results.append(Dataset(name, create=cdate))
return results
@classmethod
def _run_cmd(cls, args):
args_list = shlex.split(args)
p = subprocess.Popen(args_list, stdin=None,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode:
raise NonZeroExitStatus(stderr.strip())
return (stdout, stderr)
@classmethod
def pools(cls):
"""Return a list of ZFS pools."""
stdout, stderr = cls._run_cmd('zpool list -Ho name')
return [Zpool(l) for l in stdout.strip().splitlines()]
@classmethod
def datasets(cls, filter=".*", cmd_prefix=""):
"""Return a list of datasets in all pools."""
return cls._datasets('all', filter, cmd_prefix)
@classmethod
def filesystems(cls, filter=".*", cmd_prefix=""):
"""Return a list of all datasets that are filesystems in all pools."""
return cls._datasets('filesystem', filter, cmd_prefix)
@classmethod
def snapshots(cls, filter=".*", cmd_prefix=""):
"""Return a list of all datasets that are snapshots in all pools."""
return cls._datasets('snapshot', filter, cmd_prefix)
@classmethod
def volumes(cls, filter=".*", cmd_prefix=""):
"""Return a list of all datasets that are volumes in all pools."""
return cls._datasets('volume', filter, cmd_prefix)
class Zpool(object):
"""Represents an individual ZFS pool.
Can be used to perform pool-wide operations and to get a list of
datasets within a pool."""
def __init__(self, name):
self._name = name
@property
def name(self):
"""The ZFS pool's name"""
return self._name
def datasets(self, filter=".*", cmd_prefix=""):
"""Return a list of datasets in all pools."""
return Zfs._datasets('all', filter, cmd_prefix, self.name)
def filesystems(self, filter=".*", cmd_prefix=""):
"""Return a list of all datasets that are filesystems in all pools."""
return Zfs._datasets('filesystem', filter, cmd_prefix, self.name)
def snapshots(self, filter=".*", cmd_prefix=""):
"""Return a list of all datasets that are snapshots in all pools."""
return Zfs._datasets('snapshot', filter, cmd_prefix, self.name)
def volumes(self, filter=".*", cmd_prefix=""):
"""Return a list of all datasets that are volumes in all pools."""
return Zfs._datasets('all', filter, cmd_prefix, self.name)
class Dataset(object):
"""Represents a dataset within a ZFS pool.
It could be a filesystem, a volume, or a snapshot. Allows access
to properties of the dataset."""
def __init__(self, name, cmd_prefix="", **attrs):
"""Initialize a new Dataset.
If we are provided only a name then fill in the attributes
ourselves. Otherwise, fill with the values provided. Only
the ZFS classes should use **attrs directly in this
constructor."""
self._name = name
self._cmd_prefix = cmd_prefix
self._attrs = attrs
if not attrs:
dsets = Zfs._datasets('all', '^%s$' % (name), cmd_prefix)
if not dsets:
raise NoSuchDatasetError
if len(dsets) > 1:
raise Exception("Unexpected logic bug")
@property
def name(self):
"""The dataset's name."""
return self._name
@property
def attrs(self):
"""Dictionary of dataset attributes."""
return self._attrs
@property
def snapshots(self):
"""A list of snapshots based on this dataset."""
return Zfs._datasets('snapshot', '%s@.*'
% (self.name), self._cmd_prefix)
def __str__(self):
return 'dset:%s create:%s' % (self.name, self.attrs['create'])
@contextmanager
def zfs_recv_lockfile(remhost, remfs):
""" Lock file is created based on the destination host and
dataset, to allow for concurrent replications when safe"""
lockfile = '/tmp/zfs-replicate-%s-%s.lock' \
% (remhost.split('.')[0], remfs.replace('/', '_'))
lockfile_fd = 0
try:
lockfile_fd = os.open(lockfile, os.O_EXCL|os.O_CREAT)
os.close(lockfile_fd)
yield
except OSError:
raise LockfileExists
finally:
if lockfile_fd:
os.unlink(lockfile)
def log(msg, log=True, stdout=False, stderr=False):
for line in msg.split('\n'):
if stdout:
print(line)
sys.stdout.flush()
if stderr:
print('ERROR: %s' % line, file=sys.stderr)
sys.stderr.flush()
if log:
if stderr:
syslog.syslog('ERROR: %s' % line)
else:
syslog.syslog('INFO: %s' % line)
def split_dest(dest):
m = re.match('^([^:]+):([^:]+)$', dest)
if not m:
raise InvalidDestinationError
return m.group(1), m.group(2)
def run_repl(begin_snap, end_snap, dst_host, dst_fs, verbose=False,
dryrun=False):
"""Perform replication to dst_host into dst_fs.
If begin_snap is None, perform a full replication, otherwise,
perform an incremental send/receive between begin_snap and
end_snap."""
send_args = ''
if begin_snap:
send_args = send_args + ' -I @%s' % (begin_snap.name.split('@')[1])
send_cmd = 'zfs send -v%s %s' % (send_args, end_snap.name)
recv_cmd = 'ssh %s zfs receive -v -F %s' % (dst_host, dst_fs)
send_list = shlex.split(send_cmd)
recv_list = shlex.split(recv_cmd)
if dryrun:
log('[DRY RUN] Not running command: %s | %s' %
(send_cmd, recv_cmd), stdout=True)
else:
log('Running command: %s | %s' % (send_cmd, recv_cmd), stdout=verbose)
if not dryrun:
send_p = subprocess.Popen(send_list,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
recv_p = subprocess.Popen(recv_list,
stdin=send_p.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
send_p.stdout.close() # Ensures EPIPE is properly sent to send_p
no_such_dset = ''
no_such_dset_RE = "^warning: cannot send '(.*)': no such pool or dataset$"
send_err_EOF = False
recv_out_EOF = False
recv_err_EOF = False
while True:
rready, wready, xready = select.select(
[send_p.stderr, recv_p.stdout, recv_p.stderr], [], [])
buf = ''
for fd in rready:
buf = fd.readline().rstrip()
if fd is send_p.stderr:
if buf:
log(' SEND (stderr): %s' % (buf), stdout=verbose)
dset = re.search(no_such_dset_RE, buf)
if dset:
no_such_dset = dset.group(1)
else:
send_err_EOF = True
elif fd is recv_p.stdout:
if buf:
log(' RECV (stdout): %s' % (buf), stdout=verbose)
else:
recv_out_EOF = True
elif fd is recv_p.stderr:
if buf:
log(' RECV (stderr): %s' % (buf), stdout=verbose)
else:
recv_err_EOF = True
if send_err_EOF and recv_out_EOF and recv_err_EOF:
break
send_retcode = send_p.wait()
recv_retcode = recv_p.wait()
if no_such_dset:
raise SendNoSuchDataset(no_such_dset)
if send_retcode:
raise NonZeroExitStatus(send_cmd)
if recv_retcode:
raise NonZeroExitStatus(recv_cmd)
return
def rm_remote_dsets(dsetlist, dst_host, verbose=False, dryrun=False):
"""Remove remote datasets"""
for dset in dsetlist:
cmd = 'ssh %s zfs destroy %s' % (dst_host, dset.name)
cmd_list = shlex.split(cmd)
if dryrun:
log('[DRY RUN] Not running command: %s' % (cmd), stdout=True)
else:
log('Running command: %s' % (cmd), stdout=verbose)
if not dryrun:
p = subprocess.Popen(cmd_list,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out_EOF = False
err_EOF = False
while True:
rready, wready, xready = select.select(
[p.stdout, p.stderr], [], [])
buf = ''
for fd in rready:
buf = fd.readline().rstrip()
if fd is p.stdout:
if buf:
log(' REMOTE (stdout): %s' % (buf), stdout=verbose)
else:
out_EOF = True
elif fd is p.stderr:
if buf:
log(' REMOTE (stderr): %s' % (buf), stdout=verbose)
else:
err_EOF = True
if out_EOF and err_EOF:
break
retcode = p.wait()
if retcode:
raise NonZeroExitStatus(cmd)
return
def replicate(fs, remhost, remfs, options):
# Validate source dataset
try:
fs_dset = Dataset(fs)
except NoSuchDatasetError:
log('No such local dataset: %s' % (fs), stderr=True)
sys.exit(1)
if len(fs_dset.snapshots) == 0:
log('No snapshots for local dataset: %s' % (fs), stderr=True)
sys.exit(1)
local_snaps = fs_dset.snapshots
local_snaps.reverse()
if options.end:
try:
end_snap = Dataset('%s@%s' % (fs, options.end))
except NoSuchDatasetError:
log('No such remote dataset: %s@%s' % (fs, options.end),
stderr=True)
sys.exit(1)
local_text = '(specified): '
else:
end_snap = local_snaps[0]
staging_snap = local_snaps[-1]
local_text = '(newest): '
# Validate destination filesystem
remfs_dset = None
try:
remfs_dset = Dataset(remfs, 'ssh %s' % (remhost))
except NoSuchDatasetError:
try:
rslash = remfs.rfind('/')
if rslash < 0:
raise NoSuchDatasetError
Dataset(remfs[:rslash], 'ssh %s' % (remhost))
except NoSuchDatasetError:
log('Remote dataset or parent does not exist: %s' % (remfs),
stderr=True)
sys.exit(1)
except NonZeroExitStatus, e:
log('Error running ZFS command: %s' % (e), stderr=True)
sys.exit(1)
# Find the newest remote snapshot that also exists in the local
# list
begin_snap = None
rem_text = remfs
repl_text = 'Full replication, no snapshots on remote host'
rem_snaps_todel = []
if remfs_dset and len(remfs_dset.snapshots) > 0:
rem_snaps = remfs_dset.snapshots
rem_snaps.reverse()
for rsnap in rem_snaps:
matches = [lsnap for lsnap in local_snaps
if lsnap.name.split('@')[1] == rsnap.name.split('@')[1]]
if matches:
begin_snap = matches[0]
break
rem_snaps_todel.append(rsnap)
if not begin_snap:
log(textwrap.fill(textwrap.dedent('''\
No common snapshots between local and remote datasets.
You will need to perform a full replication. Please
destroy the remote dataset to begin.'''), 70), stderr=True)
sys.exit(1)
rem_text = begin_snap.name
repl_text = 'Incremental update since last replication'
# Report lag and exit if we're in lag mode
if options.truelag or options.snaplag:
if begin_snap:
if options.truelag:
prefix = "truelag: "
td = datetime.now() - begin_snap.attrs['create']
else:
prefix = "snaplag: "
td = end_snap.attrs['create'] - begin_snap.attrs['create']
minutes = int(td.seconds/60 + td.days*24*60)
log("%s%s minutes (dset:%s, rsnap:%s, lsnap:%s)"
% (prefix, minutes, fs,
begin_snap.name.split('@')[1],
end_snap.name.split('@')[1]),
stdout=True)
sys.exit(0)
else:
log("Dataset not yet replicated, cannot calculate lag",
stderr=True)
sys.exit(1)
# Are snapshots the same on both sides?
if begin_snap and \
end_snap.name.split('@')[1] == begin_snap.name.split('@')[1]:
log('Remote snapshot of %s is up-to-date, no work to do' %
end_snap.name, stdout=(not options.cron))
sys.exit(0)
try:
with zfs_recv_lockfile(remhost, remfs):
# Print out validated parameters
log(textwrap.dedent('''\
=== Replication parameters validated ===
Local snapshot %s %s
Remote dataset, host: %s on %s
Replication type: %s
Remote starting dataset: %s\
''' % (local_text, end_snap.name, remfs, remhost,
repl_text, rem_text)), stdout=options.verbose)
# Snapshot analysis
if begin_snap:
log(textwrap.dedent("""\
=== Incremental Snapshot Analysis ===
`-->' indicates starting snapshot on src and dest
`DEL' indicates remote snapshots to be destroyed
Local snapshot candidates (newest first):"""),
stdout=options.verbose)
for snap in local_snaps:
if snap.name.split('@')[1] == begin_snap.name.split('@')[1]:
log('--> %s' % (snap), stdout=options.verbose)
break
else:
log(' %s' % (snap), stdout=options.verbose)
log('Remote snapshot candidates (newest first)',
stdout=options.verbose)
for snap in rem_snaps:
if snap.name.split('@')[1] == begin_snap.name.split('@')[1]:
log('--> %s' % (snap), stdout=options.verbose)
break
elif snap in rem_snaps_todel:
log('DEL %s' % (snap), stdout=options.verbose)
else:
log(' %s' % (snap), stdout=options.verbose)
if options.dryrun:
log(textwrap.dedent('''\
==============================================
DRY RUN - Replication commands will not be run
==============================================\
'''), stdout=options.verbose)
if rem_snaps_todel:
log('Removing remote snapshots...', stdout=options.verbose)
rm_remote_dsets(rem_snaps_todel, remhost, options.verbose,
options.dryrun)
start_time = datetime.now()
log('=== Starting replication for %s at %s'
% (fs, start_time.strftime('%c')), stdout=options.verbose)
if not begin_snap:
# Perform a staging replication if necessary. This is
# only necessary when doing a full replication, since
# we don't get -I's behavior of sending all
# intermediate snapshots. Here we "stage" to the
# first snapshot and then do a regular incremental
# outside of the if statement.
run_repl(None, staging_snap, remhost, remfs,
options.verbose, options.dryrun)
begin_snap = staging_snap
# If begin_snap and end_snap have the same name, then the
# above staging replication got us fully up-to-date, so
# skip the follow-up incremental
if begin_snap.name.split('@')[1] != end_snap.name.split('@')[1]:
run_repl(begin_snap, end_snap, remhost, remfs,
options.verbose, options.dryrun)
end_time = datetime.now()
log('=== Replication complete for %s at %s'
% (fs, end_time.strftime('%c')), stdout=options.verbose)
except LockfileExists:
log("Lock file exists for %s:%s, receive already in progress" %
(remhost.split('.')[0], remfs), stderr=(not options.cron))
if options.cron:
sys.exit(0)
sys.exit(1)
def main():
# Set up signal handling
try:
os.setpgid(0, 0)
except OSError:
# We shouldn't stop exeuction if we can't create our own
# process group
pass
for sig in [signal.SIGINT, signal.SIGQUIT, signal.SIGTERM]:
signal.signal(sig, sig_handler)
from optparse import OptionParser
usage = textwrap.dedent("""\
Usage: %prog [-hclLnv] [-e snapshot] filesystem dest
""")
usage += '\n'
usage += textwrap.fill(textwrap.dedent("""\
Replicate filesystem to dest using zfs send and receive commands
(unless -l or -L is specified). `filesystem' should be a ZFS
dataset and `dest' should be of the form:
"""), 76)
usage += "\n\n remote_host:filesystem\n\n"
usage += textwrap.fill(textwrap.dedent("""\
SSH will be used to connect to `remote_host' and receive the
stream into the given dataset. Activity will be logged to syslog,
and to standard output if -v is used.
"""), 76)
parser = OptionParser(usage=usage)
parser.add_option('-c', '--cron', dest='cron',
action="store_true", default=False,
help="use when running via cron: suppress -v "
"output when no work is to be done and do "
"not complain about lockfile presence")
parser.add_option('-e', '--end', dest='end',
help="use specified snapshot as the replication target, "
"rather than choosing the newest snapshot available")
parser.add_option('-l', '--truelag', dest='truelag', action="store_true",
help="do not replicate filesystems; instead, report "
"the lag time in minutes between the newest remote "
"snapshot and the current time to standard output. "
"Useful for monitoring systems")
parser.add_option('-L', '--snaplag', dest='snaplag', action="store_true",
help="do not replicate filesystems; instead, report "
"the lag time in minutes between the newest local and "
"remote snapshots to standard output. Useful "
"for monitoring systems")
parser.add_option('-n', '--dryrun', dest='dryrun',
action="store_true", default=False,
help="show steps without running actual commands, "
"implies --verbose")
parser.add_option('-v', '--verbose', dest='verbose', action="store_true",
default=False, help="report progress on standard "
"output as well as syslog")
options, args = parser.parse_args()
if not args:
parser.print_help()
sys.exit(0)
if len(args) != 2:
parser.error("Please supply filesystem and dest arguments")
if options.dryrun:
options.verbose = True
fs = args[0]
try:
remhost, remfs = split_dest(args[1])
except InvalidDestinationError:
parser.error("Invalid dest specification")
# Options look good, set up logging
syslog.openlog("zsync", syslog.LOG_PID, syslog.LOG_LOCAL0)
# Begin replication
done = False
while not done:
try:
replicate(fs, remhost, remfs, options)
done = True
except SendNoSuchDataset, e:
log(textwrap.fill(textwrap.dedent("""\
WARNING: Source snapshot (%s) disappeared during
replication run. `zfs send -R ...' creates a list of
snapshots to send when the command is started and will
fail mid-transfer if a snapshot disappears. This happens
often with hourly snapshots dropping off then end of the
scheduled snapshot rotation. Replication will be
restarted--you can probably ignore this warning.
""" % (e)), 70), stdout=options.verbose)
def sig_handler(sig, frame):
# Ignore the signal we're about to send out to our process group,
# since it will come back to us
signal.signal(sig, signal.SIG_IGN)
os.killpg(os.getpgid(0), sig)
log(textwrap.fill(textwrap.dedent('''\
Replication aborted. Snapshots that have already transferred will
remain on the destination host; only the in-progress snapshot will
have to be transferred again. You should be able to restart later
where you left off.
'''), 70), stderr=True)
sys.exit(1)
if __name__ == "__main__":
main()