3 # This script lets you update a hierarchy of files in an atomic way by
4 # first creating a new hierarchy using --link-dest to rsync, and then
5 # swapping the hierarchy into place. See the usage message for more
6 # details and some important caveats!
11 my $RSYNC_PROG = '/usr/bin/rsync';
12 my $RM_PROG = '/bin/rm';
14 my $dest_dir = $ARGV[-1];
15 usage(1) if $dest_dir eq '' || $dest_dir =~ /^--/;
18 print STDERR "$dest_dir is not a directory.\n\n";
22 if (@_ = grep(/^--(link|compare)-dest/, @ARGV)) {
23 $_ = join(' or ', @_);
24 print STDERR "You may not use $_ as an rsync option.\n\n";
28 $dest_dir = abs_path($dest_dir);
29 if ($dest_dir eq '/') {
30 print STDERR 'You must not use "/" as the destination directory.', "\n\n";
34 my $old_dir = "$dest_dir~old~";
35 my $new_dir = $ARGV[-1] = "$dest_dir~new~";
37 system($RM_PROG, '-rf', $old_dir) if -d $old_dir;
39 if (system($RSYNC_PROG, "--link-dest=$dest_dir", @ARGV)) {
41 print "failed to execute $RSYNC_PROG: $!\n";
43 printf "child died with signal %d, %s coredump\n",
44 ($? & 127), ($? & 128) ? 'with' : 'without';
46 printf "child exited with value %d\n", $? >> 8;
51 rename($dest_dir, $old_dir) or die "Unable to rename $new_dir to $old_dir: $!";
52 rename($new_dir, $dest_dir) or die "Unable to rename $new_dir to $dest_dir: $!";
60 my $fh = $ret ? *STDERR : *STDOUT;
62 Usage: atomic-rsync [RSYNC-OPTIONS] HOST:/SOURCE/DIR/ /DEST/DIR/
63 atomic-rsync [RSYNC-OPTIONS] HOST::MOD/DIR/ /DEST/DIR/
65 This script lets you update a hierarchy of files in an atomic way by first
66 creating a new hierarchy (using hard-links to leverage the existing files),
67 and then swapping the new hierarchy into place. You must be pulling files
68 to a local directory, and that directory must already exist. For example:
70 atomic-rsync -av host:/remote/files/ /local/files/
72 This would make the transfer to the directory /local/files~new~ and then
73 swap out /local/files at the end of the transfer by renaming it to
74 /local/files~old~ and putting the new directory into its place. The
75 /local/files~old~ directory will be preserved until the next update, at
76 which point it will be deleted.
78 Do NOT specify this command:
80 atomic-rsync -av host:/remote/files /local/
82 ... UNLESS you want the entire /local dir to be swapped out!
84 See the "rsync" command for its list of options. You may not use the
85 --link-dest or --compare-dest options (since this script uses --link-dest
86 to make the transfer efficient). Also, the destination directory cannot