Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Time Machine: Mount backup disk on demand System
It is both inconvenient and less safe to keep backup volumes mounted when not in use. A mounted volume is more prone to corruption, and open file dialogs needlessly spin up mounted volumes, contributing to a user experience latency that is most apparent if the primary drive is solid state.

By creating an /etc/periodic/hourly directory modeled after the existing /etc/periodic/daily directory, and adapting com.apple.periodic-daily.plist, there is a place to put a script that handles mounting and unmounting the Time Machine backup disk, and launching Time Machine.

Scripts that implement nightly tasks can be placed in the /etc/periodic/daily directory, which is run at 3:15 AM by /System/Library/LaunchDaemons/com.apple.periodic-daily.plist. Using sudo in Terminal, create the parallel directory /etc/periodic/hourly and the parallel file /System/Library/LaunchDaemons/com.apple.periodic-hourly.plist.

Then clone the file com.apple.periodic-hourly.plist from com.apple.periodic-daily.plist, replacing each instance of daily by hourly, and replacing the StartCalendarInterval by a StartInterval of 3600 seconds. Specifically, replace:
<key>StartCalendarInterval</key>
<dict>
 <key>Hour</key>
 <integer>3</integer>
 <key>Minute</key>
 <integer>15</integer>
</dict>
with
<key>StartInterval</key>
<integer>3600</integer>
The syntax for this file is documented by the man page launchd.plist(5). Now, after restart, any script placed in /etc/periodic/hourly will be run every hour as super user.

Turn off Time Machine in System Preferences, but leave the correct backup disk selected. One can still launch Time Machine from the command line or a script, by calling backupd-helper. Our script will mount and unmount the backup disk as necessary, and call Time Machine by this mechanism.

One can create a similar script to automate SuperDuper! backups. One can reverse engineer a replacement for backupd-helper by watching SuperDuper! as it runs using the command line command ps -ax | grep SDCopy. Calling SDCopy is unsupported, but it has worked flawlessly for me for years, and opens up the possibility of updating folders as easily as volumes.

You can suggest that Apple implement this functionality for Time Machine, by providing feedback at Mac OS X Feedback. SuperDuper! requests that I not document this functionality for their SDCopy tool, so you should ask them to support mounting and unmounting backup volumes on demand.

Together, these scripts implement the software portion of silencing my external drives. To complete this task, one can either use passively cooled external enclosures, or replace the fan on an actively cooled enclosure. See Modding the WiebeTech RTX220-QJ for an example of how one might do this.

Here is the Time Machine script, written in Python. I have hard-wired my backup disk and log file directory, so the script needs to be adapted for use on a different system. (In general, one should never run any script as super user that one doesn't completely understand.)

#!/usr/bin/env python

# place in /etc/periodic/daily to run nightly (or hourly if set up):
#
# sudo cp -f timemachine.py /etc/periodic/daily/667.timemachine
# sudo cp -f timemachine.py /etc/periodic/hourly/667.timemachine

import re
import datetime
import time
from subprocess import Popen, PIPE

# do executes a shell command, returning stdout and stderr

def do(cmd):
 p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
 (fi, fo, fe) = (p.stdin, p.stdout, p.stderr)
 fi.close()
 msg = fo.read()
 fo.close()
 fe.close()
 return msg

# mounted checks if vol is mounted

def mounted(vol):
 pat = r'/Volumes/(\S+)\s+[(](.+)[)]'
 msg = do('/sbin/mount')
 vols = dict(re.compile(pat).findall(msg))
 return vol in vols.keys()

# disks returns a dictionary associating e.g. 'Flash' to 'disk0s2'

def disks():
 pat = r'Apple_HFS\s+(\S+)\s+.+(disk\d+s\d+)'
 msg = do('/usr/sbin/diskutil list')
 vols = re.compile(pat).findall(msg)
 return dict(vols)

# sleep waits a bit

def sleep():
 time.sleep(30)

# mount_volume returns pair (now mounted?, was mounted?),
# after attempting to mount volume for "name" as needed

def mount_volume(vol, logfile):
 if mounted(vol):
 (now, was) = (True, True)
 else:
 was = False
 diskID = disks()
 mountable = diskID.keys()
 if vol in mountable:
  do("/usr/sbin/diskutil mount %s >>'%s' 2>&1" % (diskID[vol], logfile))
  sleep()
 now = mounted(vol)
 return (now, was)

# unmount_volume unmounts volume

def unmount_volume(vol, logfile):
 do("/usr/sbin/diskutil unmount '/Volumes/%s' >>'%s' 2>&1" % (vol, logfile))

# run Time Machine
# Time Machine has finished when 'backupd' is no longer running

logdir = '/Local/Backup Logs'
backupd = "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper >>'%s' 2>&1"

def start_backupd(logfile):
 do("echo 'starting Time Machine' >>'%s'" % logfile)
 do(backupd % logfile)

def backupd_running(logfile):
 return do("ps -ax | grep [b]ackupd >>'%s' 2>&1" % logfile)

def timemachine(vol):
 now = datetime.datetime.now().strftime("%F %H-%M-%S")
 logfile = "%s/%s (Time Machine)" % (logdir, now)
 (tnow, twas) = mount_volume(vol, logfile)
 if tnow:
 start_backupd(logfile)
 sleep()
 while backupd_running(logfile):
  sleep()
 if not twas:
  sleep()
  for i in range(1,8):
  sleep()
  unmount_volume(vol, logfile)
  if not mounted(vol):
   break

# execute (replace 'Time' by name of Time Machine volume)

timemachine('Time')


[crarko adds: I haven't tested this one. The python script is mirrored here. As advised, be sure to gain an understanding of what the script does before attempting to use it.]
    •    
  • Currently 2.88 / 5
  You rated: 1 / 5 (24 votes cast)
 
[18,115 views]  

Time Machine: Mount backup disk on demand | 22 comments | Create New Account
Click here to return to the 'Time Machine: Mount backup disk on demand' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Time Machine: Mount backup disk on demand
Authored by: jmontana on Jul 21, '10 08:38:04AM

That first sentence is not selling me on this hint at all.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: crarko on Jul 21, '10 08:54:18AM

Try it now. Thanks,

Craig A.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: wallybear on Jul 21, '10 10:09:57AM

I simply made my TM drive invisible, so it is not shown on desktop nor in Open dialogs.
Right, that doesn't mean it's not mounted, but solves the lag problem.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: asmeurer on Jul 21, '10 10:32:36AM

Hrmm. I rather dislike cron jobs. I would have preferred a solution that keeps some kind of dummy volume mounted that automatically mounts and unmounts the real thing when Time Machine tries to go through it (but not anything else, without your permission). Of course, that would require a bit more work to create and get right. Really, the ideal solution would be for Mac OS X to support this directly.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: joekewe on Jul 21, '10 12:46:53PM

Just make the volume invisible. The small Utility "TimeMachineScheduler" makes it easy with a GUI checkbox too.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: rjmorita on Jul 21, '10 01:38:59PM

The shareware TimeMachineScheduler has the option to automatically mount/unmount the backup volume.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: jpkelly on Jul 21, '10 09:57:04PM
I use an AppleScript in conjunction with backupd-helper to accomplish this. I set the script to run via a launchd item every 3 hours using Lingon Works pretty well for me. Here is the Applescript:

tell application "Finder" to if not (disk "BACKUP" exists) then do shell script "diskutil mount " & last word of (do shell script "diskutil list | grep 'Apple_HFS BACKUP'")
delay 5

do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper > /dev/null 2>&1  &"

repeat
	delay 10
	if not IsProcRunning("backupd") then
		ignoring application responses
			--tell application "loginwindow" to «event aevtshut»
			do shell script "diskutil eject " & last word of (do shell script "diskutil list | grep 'Apple_HFS BACKUP'")
			--display dialog "backup done"
		end ignoring
		exit repeat
	end if
end repeat

on IsProcRunning(theProc)
	try
		do shell script "ps auxc | grep \"" & theProc & "\""
		return true
	on error
		return false
	end try
end IsProcRunning
I also have an AppleScript in my startup items to unmount the drive after system startup:

on run
	delay 60
	ignoring application responses
		--tell application "loginwindow" to «event aevtshut»
		do shell script "diskutil eject " & last word of (do shell script "diskutil list | grep 'Apple_HFS BACKUP'")
		--display dialog "backup done"
	end ignoring
end run


[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: Alrescha on Jul 22, '10 06:05:22AM

infinite variety:

#!/bin/sh
grep -q "Backup completed successfully." /var/log/system.log
if [ $? = 0 ]
then
exit 0
fi
test -d /Volumes/Time\ Machine
if [ $? = 1 ]
then
/usr/sbin/diskutil mount time-machine-uuid
fi
grep -q "Backup completed successfully." /var/log/system.log
while [ $? = 1 ]
do
sleep 300
grep -q "Backup completed successfully." /var/log/system.log
done
/usr/sbin/diskutil unmount time-machine-uuid



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: syzygies on Jul 22, '10 06:25:38AM

TimeMachineScheduler looks like a nice alternative, if one prefers GUIs and doesn't care about seeing source code. Many of us use Mac OS X Hints as a source of information for how to do things for ourselves; this is a matter of personal preference.

It is interesting that TimeMachineScheduler lists unmounting as a known problem; I had to tweak my script to get unmounting to work reliably. My observation is that there is an unpredictable lag after backupd stops running, before the OS realizes that the TM disk is no longer in use. My script waits, and gives this eight tries. According to my logs, I sometimes need the second try, but never more.

I didn't know about making volumes invisible to avoid dialog lags. Nice idea. I still prefer my backup volumes to be unmounted except in use. Safer, and saves a step when swapping disks.

OS X long ago (as of 10.4) stopped using cron jobs in favor of launchd. However, setting up a manual launchd job is more work than dropping a script in /etc/periodic/daily or creating the parallel /etc/periodic/hourly. If one objects to all periodic jobs on principle, just turn off Time Machine; it's an periodic job, after all. Perhaps my hint is most useful as an example of how to use /etc/periodic/daily or /etc/periodic/hourly, the quickest in-and-out for setting up a periodic task.

Time Machine is only part of my backup strategy; I also rely on a similar script to handle SuperDuper! backups. This avoids their GUI, manages automounting backup volumes, and suppresses error dialogs for unavailable volumes. I can list every conceivable backup dependency (e.g. backup "Media" to "T-Media" if "Media" and "T-Media" are available to be mounted) without ever seeing a SuperDuper! error dialog for unavailable volumes. Before this script, I used to see dozens of these SD! dialogs over coffee each morning. I've tried nudging SuperDuper! in this direction; their pre- and post- scripts won't run if the backup volume isn't already mounted. They have a very active user community who are happy as things are, and they are justifiably very conservative about making any changes that would impact the support needs of users.

The latest builds of rsync do a great job of preserving metadata; for many purposes one could replace SuperDuper! by rsync. I haven't bothered. I'm thrilled with how well SuperDuper! does the part of the job I still let it do.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: gvitale on Jul 22, '10 07:11:09AM

How do you tweak the TMS unmount procedure? What is the "my script" you mention?
Thanks in advance



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: crarko on Jul 22, '10 07:19:00AM

The script in the hint; syzygies is the author.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: gvitale on Jul 22, '10 07:32:43AM

Thanks and sorry, I didn't realize you were referring to the hint' script, I though the comment was about TMS.
Automatically unmounting the TimeMachine Volume is also beneficial if you set the computer to hibernate, and is very unfortunate that TMS doesn't do it in a predictable way.
I'll give a try to your strategy. Thanks for the hint.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: JimMueller on Jul 24, '10 08:30:57AM

This discussion seems to be solely about a locally-connected TM volume. Has anyone had any experience with making a mounted shared network TM volume invisible (or un-/mounting it via scripts)? I am referring to a drive attached to a networked Mac that another Mac (in our case a MacBook Pro) would be using as its TM target.

I had contacted TimeMachineScheduler's author (Stefan Klieme) about this but he said TMS did not handle network TM volumes but he implied that Time Machine itself mounted and un-mounted TimeCapsule drives, so though the ability was there he did not know how to access it.

In our situation, for various reasons, a TimeCapsule is not really the solution we want.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: JimMueller on Jul 25, '10 02:33:02PM

Never mind - ignore my questions.
Evidently TM automatically mounts and un-mounts the backup image as needed. It does not appear to mount the entire shared network volume you initially configure TM to use, but only the sparse image of the backup.
I had set up a shared volume TM_TEST, mounted it on a different Mac's Desktop (creatively named Mac) and configured this source Mac's TM to use it. When the backup was done I noticed TM_TEST was no longer on the source Mac's Desktop. When I kicked off a manual TM backup a plain drive named "Backup of Mac" appeared then disappeared once the backup completed.

It appears that Time Machine is brighter than I am.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: syzygies on Jul 25, '10 08:57:02PM

The test that Time Machine has finished needs to be fixed:

return do("ps -ax | grep [b]ackupd >>'%s' 2>&1" % logfile)
will always return false, because the (recently added) logging steals any true response. Because of the delay before backupd-helper returns, and the scripted sleeps, this only affects longer backups, but it needs to be changed.

Instead, one can write

return do("ps -ax | grep [b]ackupd | tee -a '%s'" % logfile)
or following a suggestion in the comments,
return not do("grep backupd /var/log/system.log | tail -1 | grep successfully")
I have tested both variants extensively before posting here. The second depends on a sufficient delay to allow Time Machine to start up, so one doesn't end up reading the previous success message for that day.

The first is more conservative; backupd will continue to run for several minutes after the success message is written to the system log, and if one unmounts the Time Machine volume during this period, one sees the further message

Stopping backupd to allow ejection of backup destination disk!
However, the backup appears to be successful. So this is a matter of taste and temperament; do you prefer to have the Time Machine volume unmount as soon as possible, and are willing to have these messages appear in the system log?

In general, with any script like this, one needs to determine if it is running correctly in one's local environment. The acid test of any backup scheme is a successful restore. Short of this, enter Time Machine and see if the expected time periods are present. Look at the folders in the Time Machine volume. And study the system log, using the terminal command

grep backupd /var/log/system.log



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: mnewman on Jul 26, '10 04:15:42PM

Thanks to this hint I now have both my Time Machine and Carbon Copy Cloner volumes mount only on demand. The reduction in latency with respect to the appearance of file dialog boxes is quite noticeable. Definitely a significant performance enhancement.

---
http://www.mgnewman.com



[ Reply to This | # ]
SuperDuper! actually does this automatically
Authored by: dnanian on Jul 27, '10 10:54:24AM

When a scheduled backup is done, SuperDuper! will automatically mount, copy and unmount your drives. So, no special scripting or anything else need be done: just unmount your drive with Disk Utility (don't eject it - unmount it) and the scheduler will do the rest.

Note, too: you don't have to have a time or an "on mount" event set up in the scheduler. Instead, you can open the Scheduled Copies window (Window > Scheduled Copies), select the schedule you want to run, and click Copy Now.

If you do that, we'll do what we do with an 'automatic' schedule—that is, we'll mount the drive, perform the backup and unmount. Automatically.

Hope that's of use!

--Dave Nanian (from Shirt Pocket, the SuperDuper! guys)



[ Reply to This | # ]
SuperDuper! actually does this automatically
Authored by: syzygies on Jul 28, '10 10:00:51AM

That's great. I remember old SD! forum discussions from when SD! didn't work this way. I hadn't realized that this had changed. I tend to feel like a fringe user when I ask for anything, so I turn to Mac OS X Hints and my own scripts.

The other issues that I had with letting SD! schedule backups were the flurry of error messages from scheduled backups that couldn't run (I have perhaps a dozen potential backup drives, mostly offsite, with at most two or three attached at once) and the requirement that SD! run through its GUI, while I am logged in, only backing up from volume to volume rather than from folder to folder. Calling the SD! command line tool from my own script, run in background whether I am logged in or not, resolved all of these issues for me.

At this point one could as easily use rsync or CCCloner, but I prefer the paid version of SD!, which has never let me down.



[ Reply to This | # ]
SuperDuper! actually does this automatically
Authored by: dnanian on Jul 28, '10 12:34:52PM

Yes, I remember discussing this with you in our forums some time ago. It'll still give errors when drives aren't attached (although we now have a "backup on mount" option that you can use in lieu of, or in addition to, timed scheduled), and we do require the GUI for our scheduling... for most users, that works fine... :-)



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: drudus on Jul 29, '10 05:57:39PM

Is there any evidence to the claim that leaving disks mounted is more unsafe than this solution?

Anyway, it is good practice to leave /System alone - it belongs to Apple & their updates can do anything including removing your new launchd.plist.

The correct place is /Library/LaunchDaemons/ the processes launched will still have root access, but Apple are unlikely to delete from that location when an update comes out.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: syzygies on Aug 02, '10 11:09:01PM

Mounted drives are unsafe for exactly the reasons that Apple wants you to unmount a drive before physically unplugging it. Maximizing the fraction of the time that one can safely yank a drive off the computer without warning just isn't part of Apple's spec. However, try teaching this to the California power companies! I do have a UPS protecting my equipment, but a cautious backup strategy demands that I plan on its eventual failure.

Developing a backup strategy requires one to be constructively paranoid. If I get hosed by a failure mode I wasn't expecting, I still have only myself to blame.

Yes, /Library/LaunchDaemons/ is the official place for user-provided launchd jobs (cron is dead), and one shouldn't modify /System. In practice, the overhead of setting up a launchd job is much greater than that of dropping an arbitrary script into /etc/periodic/daily. No plist required; can you write a launchd plist from memory?

I monitor the logs from my daily and hourly tasks, as part of the routine of rotating media. Were an update to clobber these tasks, I'd notice right away.



[ Reply to This | # ]
Time Machine: Mount backup disk on demand
Authored by: kylecara on Jul 30, '10 08:13:17PM

Try it now. Thanks for the hint.



[ Reply to This | # ]