X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/fa170b2e5a684891e6b31f7fcf31b545f5db2fcf..4ecf3e067151e45dc7dead9862416722dcb4a20f:/support/atomic-rsync diff --git a/support/atomic-rsync b/support/atomic-rsync index 4173abe9..64967367 100755 --- a/support/atomic-rsync +++ b/support/atomic-rsync @@ -1,9 +1,15 @@ #!/usr/bin/perl +# +# This script lets you update a hierarchy of files in an atomic way by +# first creating a new hierarchy using rsync's --link-dest option, and +# then swapping the hierarchy into place. **See the usage message for +# more details and some important caveats!** use strict; use Cwd 'abs_path'; -my $RSYNC = '/usr/bin/rsync'; +my $RSYNC_PROG = '/usr/bin/rsync'; +my $RM_PROG = '/bin/rm'; my $dest_dir = $ARGV[-1]; usage(1) if $dest_dir eq '' || $dest_dir =~ /^--/; @@ -28,13 +34,11 @@ if ($dest_dir eq '/') { my $old_dir = "$dest_dir~old~"; my $new_dir = $ARGV[-1] = "$dest_dir~new~"; -if (-d $old_dir) { - rename($old_dir, $new_dir) or die "Unable to rename $old_dir to $new_dir: $!"; -} +system($RM_PROG, '-rf', $old_dir) if -d $old_dir; -if (system($RSYNC, "--link-dest=$dest_dir", @ARGV)) { +if (system($RSYNC_PROG, "--link-dest=$dest_dir", @ARGV)) { if ($? == -1) { - print "failed to execute $RSYNC: $!\n"; + print "failed to execute $RSYNC_PROG: $!\n"; } elsif ($? & 127) { printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; @@ -55,20 +59,32 @@ sub usage my($ret) = @_; my $fh = $ret ? *STDERR : *STDOUT; print $fh <