Apple’s Time Machine can be considered a revolution in creating backups for average users. Plug in a hard drive, set it for Time Machine and it just works. When Apple introduced the Time Capsule routers, they brought this ease of use to devices that didn’t have a dedicated external hard drive for backups. For the most part, this worked as well but there are sometimes issues.
When using Time Machine connecting to a Time Capsule, NAS, or other file server, a disc image in the form of a sparse bundle is created for each machine that is connected to the server. The sparse bundle is basically a wrapper that contains the hard drive. Inside of the wrapper are many files called bands that contain the data. This bundle grows as more data is added which is great. Unfortunately if something happens such as unplugging a computer from the network or closing the computer while the disc image is in use, the entire image can become corrupt.
Yesterday I disconnected my Mac from my Thunderbolt Display which was connected to Ethernet during a Time Machine backup. This caused the disc image to become corrupt. Up until now I had just accepted that I’d have to start over with the backup. I spent last week getting a new backup strategy in place and didn’t want to start over again. I did some searching and found a few articles on repairing corrupted sparse bundles. I started with this article but instead of trying to perform the repair on the network, I screen shared to my Mac Pro and attempted to do the recovery locally. Unfortunately the steps outlined failed to repair my disc image. Another article had similar commands but didn’t quite work either. Combining information from the two, I came up with my own steps to repair the image:
Attach to the disc image, but don’t verify it or attempt to mount it. Note the
readwrite option as this was key for my repair.
sudo hdiutil attach -nomount -noverify -noautofsck -readwrite /Volumes/Backups/Shared\ Items/Backups/Scott’s\ MacBook\ Pro.sparsebundle
Look at the output that will be something like:
/dev/disk6 Apple_partition_scheme /dev/disk6s1 Apple_partition_map /dev/disk6s2 Apple_HFS
Make a note of the disk for the last entry that has HFS in it.
Perform the verification and repair using:
sudo fsck_hfs -drfy /dev/disk6s2
I had to do the above item twice to get a message that the volume was repaired successfully. This will take awhile depending on the damage and size of the disc image.
Once the volume has been repaired, issue the final command
sudo hdiutil detach /dev/disk6s2
After that I did a Time Machine backup and everything worked again! I tested out restoring a file and that worked as well. Now I have a way to fix the disc images when this happens in the future. Apple really needs to do something about this issue as the articles I referenced are 6 and 9 years old meaning this isn’t a new problem.