terom@0: #!/usr/bin/python terom@0: terom@8: """ terom@67: SSH authorized_keys command="..." wrapper for rsync sender/server, with additional support for LVM snapshots. terom@28: terom@67: Testing: terom@67: PYTHONPATH=. ./bin/pvl.backup-rsync -- -rlptx /etc/apache2 test/foo terom@67: terom@62: sudo PYTHONPATH=. ./bin/pvl.backup-rsync --command 'rsync --server --sender -ax . lvm:asdf:test' -vD terom@42: terom@62: sudo sh -c "PYTHONPATH=. rsync -e './bin/pvl.backup-rsync --debug -C --' -ax testing:lvm:asdf:test test/tmp" terom@8: """ terom@8: terom@73: import pvl.args terom@73: terom@30: from pvl.backup import __version__ terom@5: from pvl.backup.rsync import RSyncCommandFormatError terom@5: from pvl.backup.invoke import InvokeError terom@42: from pvl.backup import rsync, lvm terom@3: terom@5: import optparse terom@28: import shlex terom@5: import os terom@0: import logging terom@0: terom@62: log = logging.getLogger('main') terom@0: terom@3: def parse_options (argv) : terom@3: """ terom@3: Parse command-line arguments. terom@3: """ terom@3: terom@28: # import sys; sys.stderr.write("%s\n" % (argv, )) terom@28: terom@8: parser = optparse.OptionParser( terom@8: prog = argv[0], terom@3: terom@8: # module docstring terom@8: description = __doc__, terom@30: version = __version__, terom@8: ) terom@3: tero@80: parser.add_option_group(pvl.args.parser(parser, config=False)) terom@8: terom@8: # terom@8: parser.add_option('-c', '--command', metavar='CMD', default=os.environ.get('SSH_ORIGINAL_COMMAND'), terom@70: help="Rsync command to execute") terom@3: terom@28: parser.add_option('-C', '--given-command', action='store_true', default=False, terom@70: help="Use given command in `rsync -e '%prog -C --' ...` format") terom@28: terom@43: parser.add_option('-n', '--noop', action='store_true', default=False, terom@43: help="Parse command, but do not execute") terom@43: terom@3: parser.add_option('-R', '--readonly', action='store_true', default=False, terom@70: help="Restrict to read/source mode") terom@3: terom@67: parser.add_option('-P', '--restrict-path', metavar='PATH', action='append', terom@70: help="Restrict to given path prefix") terom@3: terom@67: parser.add_option('--allow-remote', action='store_true', default=False, terom@67: help="Allow remote rsync sources") terom@67: terom@70: parser.add_option('--sudo', action='store_true', terom@70: help="Execute rsync under sudo") terom@70: terom@42: # lvm options terom@42: parser.add_option('-L', '--snapshot-size', metavar='SIZE', default=lvm.LVM_SNAPSHOT_SIZE, terom@42: help="create snapshot with given LV size (used to store writes during backup)") terom@42: terom@42: parser.add_option('--snapshot-wait', metavar='SECONDS', default=lvm.LVM_SNAPSHOT_WAIT, type='float', terom@42: help="wait for snapshot to settle after unmounting") terom@42: terom@62: parser.add_option('--snapshot-retry', metavar='RETRY', default=lvm.LVM_SNAPSHOT_RETRY, type='int', terom@62: help="retry snapshot removal by given iterations") terom@62: terom@3: # defaults terom@3: parser.set_defaults( terom@67: restrict_path = [], terom@3: ) terom@3: terom@3: # parse terom@3: options, args = parser.parse_args(argv[1:]) terom@73: terom@73: # general logging/etc terom@73: pvl.args.apply(options) terom@32: terom@3: return options, args terom@3: terom@67: def rsync_wrapper (options, command, local=False) : terom@3: """ terom@67: Wrap given rsync command, parsing options/path, determining source, and running rsync in the source. terom@8: terom@8: Parses the command, the source path, and then executes rsync within the source path (which may be a special terom@8: pseudo-path with additional handling). terom@43: terom@43: local - the given command is a full local command, not an --server mode operation terom@3: """ terom@3: terom@43: # parse the rsync command sent by the client terom@3: try : terom@43: # path = the real source path terom@43: rsync_cmd, rsync_options, path, srcdst = rsync.parse_command(command, terom@43: restrict_server = not local, terom@3: restrict_readonly = options.readonly, terom@3: ) terom@3: terom@3: except RSyncCommandFormatError, e: terom@3: log.error("invalid rsync command: %r: %s", command, e) terom@3: return 2 terom@3: terom@3: terom@43: # parse source terom@3: try : terom@8: # parse the source path as given by the client, may be a real path or pseudo-path terom@5: source = rsync.parse_source(path, terom@67: restrict_paths = options.restrict_path, terom@67: allow_remote = options.allow_remote, terom@67: lvm_opts = dict( terom@70: sudo = options.sudo, terom@67: size = options.snapshot_size, terom@67: wait = options.snapshot_wait, terom@67: retry = options.snapshot_retry, terom@67: ), terom@3: ) terom@3: terom@3: except RSyncCommandFormatError, e: terom@3: log.error("invalid rsync source: %r: %s", path, e) terom@3: return 2 terom@3: terom@43: # noop? terom@43: if options.noop : terom@43: log.info("noop: %r -> %r: execute(%r, %r)", path, source, rsync_options, srcdst) terom@43: return 0 terom@43: terom@43: # execute terom@3: try : terom@67: # run rsync within the source (may perform additional stuff like snapshot...) terom@70: source.execute(rsync_options, srcdst, terom@70: sudo = options.sudo, terom@70: ) terom@3: terom@3: except InvokeError, e: terom@3: log.error("%s failed: %d", e.cmd, e.exit) terom@3: return e.exit terom@3: terom@3: # ok terom@3: return 0 terom@3: terom@3: def main (argv) : terom@43: """ terom@43: Run, with full argv terom@43: """ terom@3: terom@3: # global options + args terom@3: options, args = parse_options(argv) terom@3: terom@43: # was a local command, not remote? terom@43: local = False terom@43: terom@43: # from args (as given by `rsync -e pvlbackup-rsync-wrapper`) -> 'pvlbackup-rsync-wrapper ( ...)' terom@28: if options.given_command : terom@28: host = args.pop(0) terom@28: command_parts = args terom@28: terom@42: log.debug("host=%r, using command from args: %r", host, command_parts) terom@43: terom@43: # from env/--command terom@28: elif options.command: terom@28: # as given terom@28: command_parts = shlex.split(options.command) terom@28: terom@43: # normal args terom@43: elif args : terom@43: command_parts = [argv[0]] + args terom@43: local = True terom@43: terom@43: log.debug("using given rsync args: %r", command_parts) terom@43: terom@28: else : terom@3: log.error("SSH_ORIGINAL_COMMAND not given") terom@3: return 2 terom@3: terom@28: # run terom@3: try : terom@67: return rsync_wrapper(options, command_parts, local=local) terom@3: terom@3: except Exception, e: terom@3: log.error("Internal error:", exc_info=e) terom@3: return 3 terom@3: terom@3: # ok terom@3: return 0 terom@0: terom@0: if __name__ == '__main__' : terom@0: import sys terom@0: terom@0: sys.exit(main(sys.argv)) terom@0: