My Backup Script (2009-07-24)

This is my simple backup script. It keeps everything I don't already have backed up in version control safe and sound from hardware failure. Not from me though - if I delete a file accidentally, that file is really gone (unless you act fast before the next backup runs!). If I make a bad edit, that file is busted.

But I can live with that.

backup.rb

Backup.rb uses rsync over ssh to backup a number of different server. Rsync is good because it only sends what changed, so we're not transferring gigs every night.

You'll have to make some changes to the SSH_USER, REMOTE_DIR and HOSTS constants. You could also add any file types you want to ignore to the EXCLUDES list.
#!/usr/bin/env ruby

NAME = %x{ hostname }.strip
SSH_USER = "brad"
REMOTE_DIR = "/home/brad/Backups/#{ NAME }"

HOSTS = [ "clamps", "hubert.lucky-dip.net" ]
DIRS = [
        "~/Library", 
        "~/Documents",
        "~/Pictures"
]
EXCLUDES = [
            "*Cache*",
            "*NewsFire*",
            "*Cookies*",
            "*.log",
            "HistoryIndex.ox*",
            "*Virtual Machines*",
            "*.resume",
            "*.ipsw",
            "*.dmg",
            "*.corrupt"
]

HOSTS.each do |host|
  DIRS.each do |dir|
    puts "--------------------------------------------"
    excludes = EXCLUDES.map { |e| "--exclude \"#{ e }\"" }.join(' ')
    cmd = "rsync -av --delete --delete-excluded "
    cmd += " --rsh=\"ssh -l #{ SSH_USER }\" "
    cmd += " #{ dir } #{ host }:#{ REMOTE_DIR } #{ excludes }"
    puts cmd
    system(cmd)
  end
end
  

Crontab

With the crontab below, a backup will be run every night at 10pm
    0 22 * * * /Users/brad/projects/scripts/backup.rb
  

blog comments powered by Disqus