atomic-rsync 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/perl
  2. #
  3. # This script lets you update a hierarchy of files in an atomic way by
  4. # first creating a new hierarchy using rsync's --link-dest option, and
  5. # then swapping the hierarchy into place. **See the usage message for
  6. # more details and some important caveats!**
  7. use strict;
  8. use warnings;
  9. use Cwd 'abs_path';
  10. my $RSYNC_PROG = '/usr/bin/rsync';
  11. my $RM_PROG = '/bin/rm';
  12. my $dest_dir = $ARGV[-1];
  13. &usage if !defined $dest_dir || $dest_dir =~ /(^-|^$)/ || grep(/^--help/, @ARGV);
  14. $dest_dir =~ s{(?<=.)/+$} {};
  15. if (!-d $dest_dir) {
  16. die "$dest_dir is not a directory.\nUse --help for help.\n";
  17. }
  18. if (@_ = grep(/^--[a-z]+-dest\b/, @ARGV)) {
  19. $_ = join(' or ', @_);
  20. die "You cannot use the $_ option with atomic-rsync.\nUse --help for help.\n";
  21. }
  22. my $symlink_content = readlink $dest_dir; # undef when a real dir
  23. my $dest_arg = $dest_dir;
  24. # This gives us the real destination dir, with all symlinks dereferenced.
  25. $dest_dir = abs_path($dest_dir);
  26. if ($dest_dir eq '/') {
  27. die qq|You must not use "/" as the destination directory.\nUse --help for help.\n|;
  28. }
  29. my($old_dir, $new_dir);
  30. if (defined $symlink_content && $dest_dir =~ /-([12])$/) {
  31. my $num = 3 - $1;
  32. $old_dir = undef;
  33. ($new_dir = $dest_dir) =~ s/-[12]$/-$num/;
  34. $symlink_content =~ s/-[12]$/-$num/;
  35. } else {
  36. $old_dir = "$dest_dir~old~";
  37. $new_dir = "$dest_dir~new~";
  38. }
  39. $ARGV[-1] = "$new_dir/";
  40. system($RM_PROG, '-rf', $old_dir) if defined $old_dir && -d $old_dir;
  41. system($RM_PROG, '-rf', $new_dir) if -d $new_dir;
  42. if (system($RSYNC_PROG, "--link-dest=$dest_dir", @ARGV)) {
  43. if ($? == -1) {
  44. print "failed to execute $RSYNC_PROG: $!\n";
  45. } elsif ($? & 127) {
  46. printf "child died with signal %d, %s coredump\n",
  47. ($? & 127), ($? & 128) ? 'with' : 'without';
  48. } else {
  49. printf "child exited with value %d\n", $? >> 8;
  50. }
  51. exit $?;
  52. }
  53. if (!defined $old_dir) {
  54. atomic_symlink($symlink_content, $dest_arg);
  55. exit;
  56. }
  57. rename($dest_dir, $old_dir) or die "Unable to rename $dest_dir to $old_dir: $!";
  58. rename($new_dir, $dest_dir) or die "Unable to rename $new_dir to $dest_dir: $!";
  59. exit;
  60. sub atomic_symlink
  61. {
  62. my($target, $link) = @_;
  63. my $newlink = "$link~new~";
  64. unlink($newlink); # Just in case
  65. symlink($target, $newlink) or die "Unable to symlink $newlink -> $target: $!\n";
  66. rename($newlink, $link) or die "Unable to rename $newlink to $link: $!\n";
  67. }
  68. sub usage
  69. {
  70. die <<EOT;
  71. Usage: atomic-rsync [RSYNC-OPTIONS] HOST:/SOURCE/DIR/ /DEST/DIR/
  72. atomic-rsync [RSYNC-OPTIONS] HOST::MOD/DIR/ /DEST/DIR/
  73. This script lets you update a hierarchy of files in an atomic way by first
  74. creating a new hierarchy (using hard-links to leverage the existing files),
  75. and then swapping the new hierarchy into place. You must be pulling files
  76. to a local directory, and that directory must already exist. For example:
  77. mkdir /local/files-1
  78. ln -s files-1 /local/files
  79. atomic-rsync -av host:/remote/files/ /local/files/
  80. If /local/files is a symlink to a directory that ends in -1 or -2, the
  81. copy will go to the alternate suffix and the symlink will be changed to
  82. point to the new dir. This is a fully atomic update. If the destination
  83. is not a symlink (or not a symlink to a *-1 or a *-2 directory), this
  84. will instead create a directory with "~new~" suffixed, move the current
  85. directory to a name with "~old~" suffixed, and then move the ~new~
  86. directory to the original destination name (this double rename is not
  87. fully atomic, but is rapid). In both cases, the prior destintaion
  88. directory will be preserved until the next update, at which point it
  89. will be deleted.
  90. In all likelihood, you do NOT want to specify this command:
  91. atomic-rsync -av host:/remote/files /local/
  92. ... UNLESS you want the entire /local dir to be swapped out!
  93. See the "rsync" command for its list of options. You may not use the
  94. --link-dest, --compare-dest, or --copy-dest options (since this script
  95. uses --link-dest to make the transfer efficient).
  96. EOT
  97. }