Skip to content

Instantly share code, notes, and snippets.

@bitsandbooks
Last active December 22, 2015 20:38
Show Gist options
  • Save bitsandbooks/6527502 to your computer and use it in GitHub Desktop.
Save bitsandbooks/6527502 to your computer and use it in GitHub Desktop.
My Time Machine Server doesn't need to be serving all day, so I wrote this script to start/stop the relevant services (netatalk, avahi-daemon) and mount/umount the volume. Cron calls this script twice a day: once at 6:00 AM to shut off the services (so my laptop isn't in the middle of a backup when I leave for work) and again at 5:00 (so when I …
#!/usr/bin/env ruby
# tmvolume.rb
# by Rob Dumas <rob@bitsandbooks.com>
# Starts and stops the Time Machine volume. Intended to be called from cron.
action = ARGV[0]
mountpoint = "/mnt/timecapsule"
servicefolder = "/etc/init.d/"
servicenames = [ "netatalk", "avahi-daemon" ]
commands = Array.new(3, "sudo ")
case action
when "start"
commands[0] += "mount " + mountpoint + "; sleep 30"
servicenames.each_with_index { |s,i| commands[i+1] += servicefolder + s + " start"}
commands.each { |c| system(c) }
when "stop"
commands[0] += "umount " + mountpoint + "; sleep 30"
servicenames.each_with_index { |s,i| commands[i+1] += servicefolder + s + " stop"}
commands.reverse!
commands.each { |c| system(c) }
else
STDOUT.puts <<-EOF
ERROR: Bad argument (or no argument) passed to script.
Usage:
tmvolume.rb start
tmvolume.rb stop
EOF
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment