From 716b46c550d12c14dc198bebafcca8ec7ebc3c76 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Fri, 14 Jan 2005 19:23:20 +0000 Subject: [PATCH] - We now remove the DEST~old~ dir instead of renaming it to DEST~new~. - Improved the usage message and added an introductory comment. --- support/atomic-rsync | 48 +++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/support/atomic-rsync b/support/atomic-rsync index 4173abe9..2f43e1ee 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 --link-dest to rsync, 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 <