| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import os, sys, re, subprocess, shutil |
| 8 |
|
| 9 |
ALT_DEST_ARG_RE = re.compile('^--[a-z][^ =]+-dest(=|$)') |
| 10 |
|
| 11 |
RSYNC_PROG = '/usr/bin/rsync' |
| 12 |
|
| 13 |
def main(): |
| 14 |
cmd_args = sys.argv[1:] |
| 15 |
if '--help' in cmd_args: |
| 16 |
usage_and_exit() |
| 17 |
|
| 18 |
if len(cmd_args) < 2: |
| 19 |
usage_and_exit(True) |
| 20 |
|
| 21 |
dest_dir = cmd_args[-1].rstrip('/') |
| 22 |
if dest_dir == '' or dest_dir.startswith('-'): |
| 23 |
usage_and_exit(True) |
| 24 |
|
| 25 |
if not os.path.isdir(dest_dir): |
| 26 |
die(dest_dir, "is not a directory or a symlink to a dir.\nUse --help for help.") |
| 27 |
|
| 28 |
bad_args = [ arg for arg in cmd_args if ALT_DEST_ARG_RE.match(arg) ] |
| 29 |
if bad_args: |
| 30 |
die("You cannot use the", ' or '.join(bad_args), "option with atomic-rsync.\nUse --help for help.") |
| 31 |
|
| 32 |
|
| 33 |
allowed_exit_codes = '0 ' + os.environ.get('ATOMIC_RSYNC_OK_CODES', '24') |
| 34 |
try: |
| 35 |
allowed_exit_codes = set(int(num) for num in re.split(r'[, ]+', allowed_exit_codes) if num != '') |
| 36 |
except ValueError: |
| 37 |
die('Invalid integer in ATOMIC_RSYNC_OK_CODES:', allowed_exit_codes[2:]) |
| 38 |
|
| 39 |
symlink_content = os.readlink(dest_dir) if os.path.islink(dest_dir) else None |
| 40 |
|
| 41 |
dest_arg = dest_dir |
| 42 |
dest_dir = os.path.realpath(dest_dir) |
| 43 |
if dest_dir == '/': |
| 44 |
die('You must not use "/" as the destination directory.\nUse --help for help.') |
| 45 |
|
| 46 |
old_dir = new_dir = None |
| 47 |
if symlink_content is not None and dest_dir.endswith(('-1','-2')): |
| 48 |
if not symlink_content.endswith(dest_dir[-2:]): |
| 49 |
die("Symlink suffix out of sync with dest_dir name:", symlink_content, 'vs', dest_dir) |
| 50 |
num = 3 - int(dest_dir[-1]); |
| 51 |
old_dir = None |
| 52 |
new_dir = dest_dir[:-1] + str(num) |
| 53 |
symlink_content = symlink_content[:-1] + str(num) |
| 54 |
else: |
| 55 |
old_dir = dest_dir + '~old~' |
| 56 |
new_dir = dest_dir + '~new~' |
| 57 |
|
| 58 |
cmd_args[-1] = new_dir + '/' |
| 59 |
|
| 60 |
if old_dir is not None and os.path.isdir(old_dir): |
| 61 |
shutil.rmtree(old_dir) |
| 62 |
if os.path.isdir(new_dir): |
| 63 |
shutil.rmtree(new_dir) |
| 64 |
|
| 65 |
child = subprocess.run([RSYNC_PROG, '--link-dest=' + dest_dir, *cmd_args]) |
| 66 |
if child.returncode not in allowed_exit_codes: |
| 67 |
die('The rsync copy failed with code', child.returncode, exitcode=child.returncode) |
| 68 |
|
| 69 |
if not os.path.isdir(new_dir): |
| 70 |
die('The rsync copy failed to create:', new_dir) |
| 71 |
|
| 72 |
if old_dir is None: |
| 73 |
atomic_symlink(symlink_content, dest_arg) |
| 74 |
else: |
| 75 |
os.rename(dest_dir, old_dir) |
| 76 |
os.rename(new_dir, dest_dir) |
| 77 |
|
| 78 |
|
| 79 |
def atomic_symlink(target, link): |
| 80 |
newlink = link + "~new~" |
| 81 |
try: |
| 82 |
os.unlink(newlink); |
| 83 |
except OSError: |
| 84 |
pass |
| 85 |
os.symlink(target, newlink) |
| 86 |
os.rename(newlink, link) |
| 87 |
|
| 88 |
|
| 89 |
def usage_and_exit(use_stderr=False): |
| 90 |
usage_msg = """\ |
| 91 |
Usage: atomic-rsync [RSYNC-OPTIONS] [HOST:]/SOURCE/DIR/ /DEST/DIR/ |
| 92 |
atomic-rsync [RSYNC-OPTIONS] HOST::MOD/DIR/ /DEST/DIR/ |
| 93 |
|
| 94 |
This script lets you update a hierarchy of files in an atomic way by first |
| 95 |
creating a new hierarchy (using hard-links to leverage the existing files), |
| 96 |
and then swapping the new hierarchy into place. You must be pulling files |
| 97 |
to a local directory, and that directory must already exist. For example: |
| 98 |
|
| 99 |
mkdir /local/files-1 |
| 100 |
ln -s files-1 /local/files |
| 101 |
atomic-rsync -aiv host:/remote/files/ /local/files/ |
| 102 |
|
| 103 |
If /local/files is a symlink to a directory that ends in -1 or -2, the copy |
| 104 |
will go to the alternate suffix and the symlink will be changed to point to |
| 105 |
the new dir. This is a fully atomic update. If the destination is not a |
| 106 |
symlink (or not a symlink to a *-1 or a *-2 directory), this will instead |
| 107 |
create a directory with "~new~" suffixed, move the current directory to a |
| 108 |
name with "~old~" suffixed, and then move the ~new~ directory to the original |
| 109 |
destination name (this double rename is not fully atomic, but is rapid). In |
| 110 |
both cases, the prior destintaion directory will be preserved until the next |
| 111 |
update, at which point it will be deleted. |
| 112 |
|
| 113 |
By default, rsync exit-code 24 (file vanished) is allowed without halting the |
| 114 |
atomic update. If you want to change that, specify the environment variable |
| 115 |
ATOMIC_RSYNC_OK_CODES with numeric values separated by spaces and/or commas. |
| 116 |
Specify an empty string to only allow a successful copy. An override example: |
| 117 |
|
| 118 |
ATOMIC_RSYNC_OK_CODES='23 24' atomic-rsync -aiv host:src/ dest/ |
| 119 |
|
| 120 |
See the errcode.h file for a list of all the exit codes. |
| 121 |
|
| 122 |
See the "rsync" command for its list of options. You may not use the |
| 123 |
--link-dest, --compare-dest, or --copy-dest options (since this script |
| 124 |
uses --link-dest to make the transfer efficient). |
| 125 |
""" |
| 126 |
print(usage_msg, file=sys.stderr if use_stderr else sys.stdout) |
| 127 |
sys.exit(1 if use_stderr else 0) |
| 128 |
|
| 129 |
|
| 130 |
def die(*args, exitcode=1): |
| 131 |
print(*args, file=sys.stderr) |
| 132 |
sys.exit(exitcode) |
| 133 |
|
| 134 |
|
| 135 |
if __name__ == '__main__': |
| 136 |
main() |
| 137 |
|
| 138 |
|