Skip to content

Instantly share code, notes, and snippets.

@kazuho
Last active January 12, 2023 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazuho/0bc28dc265632b0532db7d8cef81308d to your computer and use it in GitHub Desktop.
Save kazuho/0bc28dc265632b0532db7d8cef81308d to your computer and use it in GitHub Desktop.
generational backup using rsync
#! /usr/bin/ruby
if ARGV.empty?
puts "Usage: #{$0} [rsync-options] <src-host:src-dir>"
exit 0
end
generations = Dir.glob("*").grep(/^[0-9]{8}-[0-9]{6}$/).sort
argv = ARGV
argv.push Time.new.strftime('%Y%m%d-%H%M%S') # destination directory
argv.prepend '--chmod=a-w' # prohibit write, as one file may represent multiple generations of backups
argv.prepend '--link-dest', "../#{generations.last}" unless generations.empty?
exec 'rsync', *argv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment