Time Machine ?

1 view
Skip to first unread message

Digi

unread,
Sep 6, 2009, 12:31:39 AM9/6/09
to MarcoPolo Discussion
I looked through the documentation and did not see anything, is there
a way to set the default backup drive in time machine based on
location? I have a drive at home and one at work, and do this on a
manual basis all the time. It would be great if I could get it to
work.

Dale

Guillaume

unread,
Sep 7, 2009, 4:34:45 AM9/7/09
to MarcoPolo Discussion
Hi Digi,

How do you achieve this ? Do you manually go and change the drive each
time by clicking on the TimeMachine icon in you menu bar ?

What I have done is that I have a copy of the TimeMachine
configuration PLIST file and I create a symbolic link depending on
what drive I want to backup to. You can do that using a bash script
and trigger this script with MarcoPolo.

Here is the script I use. It manages 3 targets: 2 USB drives of
different capacity and a networked TimeCapsule. The script can be
called w/o arguments (it will then auto-detect the available target)
or by specifying the backup target (e.g. ./backupd-launcher
UltraMax300Gb).

Note that the last section performs the actual backup (it calls what
TimeMachine calls to make the backup). If you just want to change
target drive and let TimeMachine do the rest, you can delete this
part.

#!/bin/bash

# Path where the preference files
path="/Library/Preferences/"
# Basename of the preference files
basename="com.apple.TimeMachine.plist"
# Time Capsule IP Address
tc_addr="10.91.100.69"
# Place the subnets you want to test here
uni_research="10.91.100"

# Backup command to call
backup_cmd="/System/Library/CoreServices/backupd.bundle/Contents/
Resources/backupd-helper"
# Name of this script
scriptname=$(basename $0)
# Echo command
echo_cmd="logger -i -t \"$scriptname\" "
# Target for backup
target=""

# Get my current IP address and subnet
ipaddr=$(ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk
'{ print $2}' | head -1)
subnet=$(echo $ipaddr | cut -d . -f1-3)

# Test if an argument is specified for config file suffix
if [ -z $1 ]
then
#$echo_cmd "No argument specified, exiting"
#exit 2
# Search for possible targets
$echo_cmd "Autodetecting target..."
if [ -d "/Volumes/UltraMax300Gb" ]; then
$echo_cmd "Target set to UltraMax300Gb"
target="UltraMax300Gb"
fi
if [ -d "/Volumes/UltraMax500Gb" ]; then
$echo_cmd "Target set to UltraMax500Gb"
target="UltraMax500Gb"
fi
if [ "$target" == "" ]; then
$echo_cmd "No suitable target found, exiting."
exit 2
fi
else
target=$1
fi

# Special test for the time capsule
if [ "$target" == "TimeCapsule" ]; then
if [ $subnet != $uni_research ]; then
$echo_cmd "Not connetced to UNI Research, exiting."
exit 5
fi
if ! ping -c 1 -t 2 "$tc_addr" &>/dev/null ;
then
$echo_cmd "Time Capsule doesn't reply to ping at $tc_addr, exiting."
exit 6
fi
fi

# A previous link exists, delete it
if [ -f "$path$basename" ]
then
$echo_cmd "Previous link $path$basename exists, unlinking."
rm -f "$path$basename"
fi

# Now create the link with the requested preference file
if [ -f "$path$basename.$target" ]
then
$echo_cmd "$path$basename.$target exists, linking."
ln -s "$path$basename.$target" "$path$basename"
else
$echo_cmd "File $path$basename.$target does not exist, exiting."
exit 3
fi

# Test if the link was correctly performed and,
# if so, launch the actual backup
if [ -f "$path$basename" ]
then
growl_cmd="/usr/local/bin/growlnotify"
$growl_cmd -p 1 -n $scriptname -a "Time Machine" "Time Machine
Backup" -m "Started scheduled backup on $target"

$echo_cmd "Running backup command: $backup_cmd"
$backup_cmd
else
$echo_cmd "Linking to $path$basename failed, exiting."
exit 4
fi

Regards,

Ron

unread,
Sep 8, 2009, 6:07:02 PM9/8/09
to MarcoPolo Discussion
On a 'not Marco Polo related' note, I have been using the following
script, along with Do Something When to do this. It requires ZERO
interaction (beside enabling assistive devices, so that the GUI
scripting works). All you have to do is plug the drive in, and as soon
as it mounts, DSW will see it (after configuring it, of course) and go
through the motions to change TM to use whatever drive was just
attached. I found this script somewhere online a couple of years ago,
but it still works like a champ:

(* This is a script to automatically choose which of two volumes to
have time machine back up to. If the primary volume is available it
will be chosen, otherwise it will choose the secondary volume. If
neither is available it will make no changes to the time machine
preferences.

To use, set the name of the primary and secondary volumes here: *)

set primarydrivename to "Home Time Machine"
set secondarydrivename to "Work Time Machine"

(*
Then launch the script whenever you want to change the time machine
backup. The excellent utility DSW (http://www.azarhi.com/) can be
used to launch the script whenever a volume is mounted or unmounted.

The script attempts to use growl to notify the user of its results.

Note that GUI scripting must be enabled
*)

register_growl()

activate application "System Preferences"
tell application "System Events"
tell process "System Preferences"
click menu item "Show All Preferences" of menu 1 of menu bar item
"View" of menu bar 1
click button "Time Machine" of scroll area 1 of window "System
Preferences"
repeat until exists window "Time Machine"
delay 0.2
end repeat
click button 1 of group 1 of window "Time Machine" -- change disk
(* If primary disk is mounted switch to it, otherwise switch to
secondary disk *)
repeat until exists sheet 1 of window "Time Machine"
delay 0.2
end repeat

set tablecontents to (rows of table 1 of scroll area 1 of sheet 1 of
window "Time Machine")
set rownumber to 0
set primarydriveavailable to false
repeat with tablerow in tablecontents
set rownumber to rownumber + 1
set volumename to value of static text of row rownumber of table 1
of scroll area 1 of sheet 1 of window "Time Machine"
log volumename
if volumename as string is primarydrivename then
set primarydriverownumber to rownumber
set primarydriveavailable to true
else if volumename as string is secondarydrivename then
set secondarydriverownumber to rownumber
end if
end repeat
try
if primarydriveavailable is true then
set desiredrownumber to primarydriverownumber
else
set desiredrownumber to secondarydriverownumber
end if
set volumename to value of static text of row desiredrownumber of
table 1 of scroll area 1 of sheet 1 of window "Time Machine"

select row desiredrownumber of table 1 of scroll area 1 of sheet 1
of window "Time Machine"
click button "Use for Backup" of sheet 1 of window "Time Machine"
set message to "Time Machine has ben set to backup to " &
volumename
my growlnote("General Notification", message)

on error
set message to "Time Machine preferences were not changed"
my growlnote("error notification", message)
end try

delay 1 -- wait for a confirmation dialog that appears if you select
a partition on the same physical disk as that being backed up
if (exists button "Use Selected Disk" of window 1) then
click button "Use Selected Disk" of window 1
end if

click menu item "Quit System Preferences" of menu 1 of menu bar item
"System Preferences" of menu bar 1
end tell
end tell

on register_growl()
try
tell application "GrowlHelperApp"
set the allNotificationsList to {"General Notification", "Debug
Notification", "Error Notification"}
set the enabledNotificationsList to {"General Notification", "Debug
Notification", "Error Notification"}
register as application "Configure TimeMachine" all notifications
allNotificationsList default notifications enabledNotificationsList
icon of application "TimeMachine"
end tell
end try
end register_growl

on growlnote(growltype, str)
try
tell application "GrowlHelperApp"
notify with name growltype title growltype description str
application name "Configure TimeMachine"
end tell
end try
end growlnote

Guillaume

unread,
Sep 9, 2009, 7:21:00 AM9/9/09
to MarcoPolo Discussion
Hello, thanks for this script. Just a set of quick questions:
- Does it work with networked drives (automatically mounted thanks to
MarcoPolo of course! )?
- What happens if two possible target drives are mounted at the same
time (e.g. USB + Time Capsule)?
- Does it provide the ability to also change the backup configuration
(included/excluded drives as well as the target?
- Does it work on Snow Leopard (I consider upgrading, so I'm
interested)
- Is there a way to launch AppleScript from MP? I thought it was just
shell scripts.

Besides, would this mean that it would be (fairly) easy to incorporate
an action to set the TimeMachine target based on MarcoPolo context? I
know man can achieve it with external scripts (like mine or the
AppleScript provided by Ron. I don't know how are actions implemented,
but if it can use AppleScript, then we have a almost ready solution
there, what do you think?

Ron Deagle

unread,
Sep 9, 2009, 12:41:04 PM9/9/09
to marcopol...@googlegroups.com
Hmm... Interesting questions...
1. Yes, it works with Network Drives that are automounted (if using
the hack for unsupported drives)
2. I have no idea what would happen with two sources plugged in at the
same time. It is simply a toggle, so maybe it would get confused??
3. No, the excludes would be the same for all drives
4. It does work in Snow Leopard
5. If you can launch shell scripts, then yes. Make a shell script as
such:

#!/bin/sh
osascript /path/to/your/applescript.scpt

and it will run as it should.

Ron
Reply all
Reply to author
Forward
0 new messages