Monday, March 17, 2014

Shrink Your Time Machine Backups and Free Disk Space

Update: In a newer post, I describe a shell script I published to automate Time Machine backup deletion.

Time Machine is a backup and restore tool from Apple which is very well integrated into OS X. In my personal opinion Time Machine is not yet awesome and its shortcomings often force me to use alternative methods when I need to backup files and folders with certain characteristics.

The most important use case in which I strongly discourage anybody to use Time Machine is the backup of frequently updated big files. Time Machine always copies the whole file because it is not able to transfer only the differences between the previous state of a file and the current one. This fact has many detrimental consequences:
  • Backups are slow.
  • Backups may take a huge amount of disk space.
When I say huge, I mean enormous. Think about a virtual machine disk stored as a file in your disk: if just one bit of that file changes (and you can bet it changes every time you use your virtual machine), Time Machine will perform a copy of the whole file the next time a backup is run.

Fortunately, there exist tools which are able to examine a file and transfer only the difference with a previous stored state, such as rsync, on which you can rely to build your custom backup policies.

Anyway, no matter how Time Machine has eaten up a lot of your disk space, it may come a moment when you really need to free some of it, possibly deleting old backups and shrinking a Time Machine sparse bundle disk image.

Deleting Old Backups

Old backups can be deleted in many ways, the simplest one being the following:
  • Open the Time Machine application when the Finder application is in the foreground.
  • Navigate to the backup to be deleted double clicking on the corresponding position of the ruler on the right side of the screen.
  • Right click with the mouse on the empty space in the Finder.
  • Select Delete Backup.
Time Machine will ask you to introduce the super user password and then it will delete the selected backup.

Unfortunately this method is very clumsy if you need to delete many backups.

Another way is using the tmutil command to perform the deletion. Not only it is simpler, but it will allow you to do it programmatically if you need to. To delete a backup using tmutil you must perform the following operations:
  • Make sure your time machine backup disk is mounted (the simplest way to do it is opening the Time Machine application).
  • The format of the backup folders created by Time Machine is YYYY-MM-DD-HHmmss. If you want to delete a specific backup, you can use the following command:
$ sudo tmutil delete /full/path/to/backup/Backups.backupdb/machine/backup-name

Time Machine currently stores its backups in a folder named after the backed up machine, into the Backups.backupdb folder in the backup disk. This means that, if your machine is called iMac and your backup disk is mounted on /Volumes/Time Machine Backups, then backups will be located in the following folder:

/Volumes/Time Machine Backups/Backups.backupdb/iMac

If you want to delete the 2014-02-02-123411 backup, you must run the following command:

$ sudo tmutil delete /Volumes/Time Machine Backups/Backups.backupdb/iMac/2014-02-02-123411

You can easily make a script, for example, to delete all the backups of a specific month. The following commands will delete all the backups created in January 2014 (lines were split with \):

$ sudo bash
Password:
$ for i in /Volumes/Time\ Machine\ Backups/Backups.backupdb/iMac/2014-01* ; \
  do \
    tmutil delete "$i" ; \
  done

Deleting Backups of Any Machine

The same commands can be used to delete backups performed by any machine, not only the machine you are executing them from. This is particularly useful if your time machine backup disk is located on a NAS (such as Apple's Time Capsule).

In this case, though, you will have to mount the disk image file created for every machine and named after it. Time machine creates sparse bundle disk images which can be mounted using the Disk Utility application:
  • Open the Disk Utility application.
  • Choose the File/Open Disk Image... menu item (⌥⌘O).
  • Choose the disk image file corresponding to the backup disk of the machine whose backups you want to delete.
Disk Utility will mount the disk image and you can now use the commands shown above to delete the time machine backups stored in this disk image.

Freeing Space

Now that you are able to delete backups, you may notice that no disk space is relinquished if a sparse bundle disk image is being used as backup disk, which is the default behaviour when the backup disk is not a locally attached physical disk.

In this case, when Time Machine frees disk space, the space is freed inside the disk image, but unless the disk image itself frees it as well, the result is that no space is relinquished outside of it.

Fortunately, unallocated space can be freed from a sparse bundle using the following command (make sure the disk image is not mounted when executing this command):

$ sudo hdiutil compact /path/to/disk-image
Starting to compact…
Reclaiming free space…
.................................................................................................................................................................................................................
Finishing compaction…
Reclaimed 11.2 GB out of 118.7 GB possible.

As you can see, hdiutil reports the space which has been reclaimed and you should now see the specified amount of space as additional free space in the corresponding disk.

11 comments:

Eric Coffman said...

Hello. How about creating a script to delete all backups that are older than 3 months or 6 months?

Thanks.

Enrico Maria Crisostomo said...

Hi Eric,

Here it is:

https://github.com/emcrisostomo/Time-Machine-Cleanup

Anonymous said...

No need for a script, you can just

$ sudo tmutil delete /Volumes/Time Machine Backups/Backups.backupdb/iMac/2014-02-*

Enrico Maria Crisostomo said...

I guess you are missing the point of the script.

Phillip Taud said...

Hi Enrico!

I am having a problem with my Time Machine when backing up to a NAS. It tells me the drive is full and the old backups cannot be deleted. So I think your script would be a great solution to my issue as my backups within 30 days are never that huge.

But I don't know much about the use of terminal in mac. Can I simple double click on the install.conf file and that's it? And can I place the files anywhere?

Regards!
anak

Fabio Reggio said...

Thanks Enrico, very useful

Kenneth Rousseau said...

Enrico:

When I try to do that, I get the following message in Terminal:

Starting to compact…
Reclaiming free space…
Usage: hdiutil compact [options]
hdiutil compact -help

Any Idea what I might be doing wrong? I've used "hdiutil compact" to compact SuperDuper images without problem; not sure what I'm doing wrong here.

Thanks!

Ken

Anonymous said...

Great script do you crontab this? if so how do you bypass entering in the password every time?

-Thank you!
Eric

Anonymous said...

FYI the script should be /usr/bin not /user/local/bin in install.conf

Hope that helps also.

-Eric

Enrico Maria Crisostomo said...

Hi Eric,

Nope. Nowadays /usr/bin is for binaries installed by the OS and its package manager, while /usr/local/bin is for binaries installed by the administrator. Also, the difference is historical: "local" indicated the fact that the file system was locally mounted, while /usr could be network mounted. In either case, I believe that you should install it under /usr/local

Jesiu said...

hello Enrico,

thanks for the article, I have a question I can't find the answer to (on internet in general, thats probably my key-words are "too general" in desciribng the issue).

I plugged the disk I use for the time machine after a long break in making back-ups. Last one is from 17th of November, today we have 5th of January. The back-up started, but Time Machine not only copies the files I created on my computer between these dates, but also copies all files. So it means the backup from today takes the same size as the previous one (ca. 240 GB).

When I was using Time Machine on another Mac and was doing it regularly it was back-uping only added files, so the space taken wasn't doubled, like in today case.

I am curious if I can somehow work on this so the Time Machine back-ups only new or changed files, not all the existing ones, so I can save some space on the external hard drive. I guess the cause of my issue is that I haven't plug my time machine hard drive for so long. But I cannot change the past and it is rather impossible to check all the files manually and not missing a single one..,

I hope I expressed myself clearly, I am rather a noob in computer-raleted things :)

looking forward for your response,
Ewa