Skip to content

Instantly share code, notes, and snippets.

@v1rtual
Created December 28, 2016 10:06
Show Gist options
  • Save v1rtual/6a09ec1d48832d3aaa9fd788b35bf5e3 to your computer and use it in GitHub Desktop.
Save v1rtual/6a09ec1d48832d3aaa9fd788b35bf5e3 to your computer and use it in GitHub Desktop.
rsync backup script (macOs)
#!/usr/bin/env ruby
#
# rsync backup script to sync files to external USB backup disks.
#
# - checks for mounted volume and name
# - saves log to backup volume
#
# Volumes have to be named BACKUP_001, then call with "./backup.rb 001"
#
# Warning: This script syncs and deletes! NO INCREMENTAL BACKUP!
#
# global default excludes
#
DEFAULT_EXCLUDES = [
".Spotlight-V100",
".Trashes",
".fseventsd",
"_TEMP__",
"FC_render",
"*.lrdata"
]
#
# rsync wrapper, logs to stdout and logfile
#
def bak(directories, target, excludes = [])
exclude_option = '--exclude ' + (DEFAULT_EXCLUDES + excludes).join(' --exclude ')
logfile="/Volumes/#{target}/backup_#{target}_#{Time.now.strftime("%Y-%m-%d_%H%M")}.log"
rsync = "rsync -ahuEP --delete --log-file=#{logfile} --modify-window=1 #{ARGV[1] == 'noop' ? '-n' : ''} --delete-excluded --ignore-errors #{exclude_option}"
directories.each do |dir|
cmd = "#{rsync} \"#{dir}\" /Volumes/#{target}/#{dir.gsub('/','_').gsub(' ','_').gsub("\\",'_').gsub("(",'_').gsub(")",'_')}/"
puts "------------- #{Time.now} -- #{cmd}"
p = IO.popen(cmd)
while (line = p.gets)
puts line
end
end
end
# check for vol number argument
number = ARGV[0]
puts number
exit unless number
# examples below .....
# !----------------------- BACKUP VOLUMES -----------------------
# --- BACKUP_001 ---
# 2TB 3,5
#
if File.exists?("/Volumes/BACKUP_001") && number == '001'
unless File.exists?('/Volumes/music')
puts "do mount the music!"
exit
end
directories = [
'/Users/knut',
'/Volumes/music'
]
excludes = [
"'iPod Photo Cache'",
"knut/Library",
"knut/src"
]
bak(directories, 'BACKUP_001', excludes)
end
# --- BACKUP_002 ---
# 2TB 3,5
#
if File.exists?("/Volumes/BACKUP_002") && number == '002'
directories = [
'/Volumes/daten/ARCHIV',
]
excludes = [
]
bak(directories, 'BACKUP_002', excludes)
end
# ...... more ......
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment