»jd« powered logo

August 7th, 2015

Fixing your Time Machine backup

When you have Time Machine enabled on your Mac and you use a network attached storage device (=NAS, like Apple Time Capsule or devices made by Synology, Western Digital, Buffalo, Netgear, or others) to house the backup, the following may have happened to you:

Out of the blue, right about when it’s time for Time Machine to perform the latest backup, it may happen that, instead of creating the actual backup, Time Machine will take a longer time as usual to prepare the backup and presents you a nice error message saying something along the lines of:

Time Machine completed a verification of your backups. To improve reliability, Time Machine must create a new backup for you.

Well. What that means is that:

This is bad in two ways: a) it will take forever and b) you loose al your previous backup history.

Fortunately, there is a quite simple solution that let’s you repair your existing Time Machine backup and allows you to keep all your valuable backed up data. In most cases.

Preparations

Please note: You should do the following steps with your network device being attached over a wired gigabit network connection. Don’t try to use them on Wifi connected devices as it might literally take days to complete.

Time Machine will save all backups and every piece of meta information into a disk image in a *.sparsebundle file on your network storage device. So this is the file we need to fix.

In order to access it, you need to connect to your networks device share containing the backup with Finder. It will hold one or more *.sparsebundle files in its root (one for each Mac that uses this drive with Time Machine).

After that, open a Terminal window and switch to the root (=admin) user with the following command:

$ sudo su -

Enter your password if you’re asked to do so. This command allows you to execute the following steps as root user without having to retype your password each time.

When you see the error message mentioned above for the first time, Time Machine has already flagged your backups *.sparsebundle file as bad and locked it. As a first step, we need to unlock it:

$ chflags -R nouchg /Volumes/<name of network share>/<name of backup>.sparsebundle

Make sure to replace <name of network share> and <name of backup> accordingly and give it a few minutes to finish.

Undo flagging as a bad Time Machine backup
Unlocking the Time Machine backup

Mounting your backup

Next, we need to mount/attach your backups *.sparsebundle file to enable your system to run a few checks on it. Again, make sure to replace <name of network share> and <name of backup> according to your setup.

$ hdiutil attach -nomount -noverify -noautofsck /Volumes/<name of network share>/<name of backup>.sparsebundle

This command will attach your backups disk image to your machines file system (without mounting it, so you won’t be able to see it in Finder) and returns something like this:

/dev/diskX Apple_partition_scheme
/dev/diskXs1 Apple_partition_map
/dev/diskXs2 Apple_HFS

What is important to us is the line containing Apple_HFS or Apple_HFSX (usually this is the last line) which is the device ID followed by the device type. The X will be replaced with a number which most likely will be anything from 2 upwards. Take note of the device ID. It will look something like /dev/disk2s2 or /dev/disk3s2.

Repairing the backup

Now we can finally let the magic of trying to repair the backup happen. Run the following command after inserting your actual device ID from the step above:

$ fsck_hfs -drfy <device id>

With this kicked of, you may want to go have a coffee or two as this may take anywhere from a few minutes to multiple hours, depending on the size of your backup and the speed of your network connection. Make sure your Mac doesn’t switch to sleep mode (or standby) while this runs.

After the command finished, you’ll either see “The Volume was repaired successfully” or “The Volume could not be repaired”.

Either way, you need to unmount/detach the backup before taking any further steps:

$ hdiutil detach <device id>

Finishing touches

If the repair command from the previous step failed, there’s not much you can do about it and your backups are probably lost for real. You should just let Time Machine create a new backup for you.

If it succeeded though, there is a last step we need to take in order to convince Time Machine to keep using the existing backup.

Use the Finder to navigate to the backups *.sparsebundle file and with a right click, choose “Show package contents” to get a look inside. There you’ll find a file called com.apple.TimeMachine.MachineID.plist. Since Time Machine uses this file to mark bad backups, too, we need to modify it slightly.

Open the file with a text editor of your choice, find the lines saying

<key>RecoveryBackupDeclinedDate</key>
<date>{any-date-string}</date>

and remove them completely. Next find the lines

<key>VerificationState</key>
<integer>2</integer>

and change them to

<key>VerificationState</key>
<integer>0</integer>

Done. You probably want to eject/unmount the network device from Finder and tell Time Machine to do another regular backup now.


Disclaimer: even though I used the steps outlined above multiple times myself with great success, there’s no guaranty that they will repair every single broken Time Machine backup. If not handled with care, they may create even more damage. Keep this in mind, when following along at your own risk.

Hi there! I’m Jonas Döbertin, a full stack web developer from Hamburg, Germany with a focus on Laravel, Kirby CMS and Vue.js. Sometimes I tweet, share code and post design ideas.

I’m always accepting freelance work so if you want to talk, get in touch at hello@jd-powered.net.