Last active
September 2, 2018 17:08
-
-
Save pmav99/29668c49ba8aeb4cc02d168794586fa4 to your computer and use it in GitHub Desktop.
pysync - A wrapper around `rsync`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| #author: Panagiotis Mavrogiorgos | |
| #email: gmail, pmav99 | |
| """ | |
| A wrapper around ``rsync``. | |
| If the ``source`` or the ``destination`` directories are on a remote host | |
| then you must specify the user and ip. E.g. | |
| [email protected]:/path/to/destination | |
| If you need to specify the port, use the ``-p`` parameter. | |
| The ``rsync`` arguments which are used by default are: | |
| --archive --compress --progress -- partial -- human-readable -- verbose | |
| Usage | |
| ----- | |
| - copying a local dir to a remote host: | |
| pysync local_dir/ [email protected]:~/outer_dir/inner_dir/some_dir/ | |
| """ | |
| import shlex | |
| import argparse | |
| import subprocess | |
| DEFAULT_RSYNC_ARGS = " ".join([ | |
| '--archive', | |
| '--compress', | |
| '--progress', | |
| '--partial', | |
| '--human-readable', | |
| '--verbose', | |
| ]) | |
| epilog = """ | |
| If the `source` or the `destination` directories are on a remote host | |
| then you must specify the user and ip. E.g. | |
| [email protected]:/path/to/destination | |
| """ | |
| def parse_arguments(): | |
| parser = argparse.ArgumentParser(description="""A wrapper around `rsync`.""", prog=__file__, epilog=epilog) | |
| parser.add_argument('-p', dest='port', action='store', default=22, help='The port sshd runs on') | |
| parser.add_argument('-n', '--dry-run', action="store_const", dest='dry_run', const="--dry-run", default="", help='perform a trial run with no changes made') | |
| parser.add_argument('source', action='store', help='The source directory') | |
| parser.add_argument('destination', action='store', help='The destination directory') | |
| parser.add_argument('-e', action='store', dest='extra', default="", help='Pass extra options to rsync (e.g. --delete)') | |
| options = parser.parse_args() | |
| return options | |
| def main(): | |
| options = parse_arguments() | |
| # determine if ssh is being in use | |
| if "@" in options.source or "@" in options.destination: | |
| ssh = '-e "ssh -p {port}" '.format(**options.__dict__) | |
| else: | |
| ssh = '' | |
| command = "rsync {dry_run} {default_rsync_args} {extra} {ssh} {source} {destination}" | |
| command = command.format(ssh=ssh, default_rsync_args=DEFAULT_RSYNC_ARGS, **options.__dict__) | |
| print("The following command will be executed:\n\n%s\n" % command) | |
| value = input("Press ENTER to continue or any other key to abort:\n") | |
| if not value: | |
| subprocess.check_call(shlex.split(command)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment