I'm using time machine to backup some servers to a sparse disk image bundle and I'd like to have a script to clean up the old back ups and re-size the image once space has been freed up. I'm fairly certain the data is protected because if I delete the old backups by right clicking, I have to enter a password to be able to delete them. To allow my script to be able to delete them, I've been running it as root. For some reason, it just won't run and every file it tries to delete I get
rm: /file/: Operation not permitted
Here is what I have as my script:
#!/bin/bash
for server in servername; do
/usr/bin/hdiutil attach -mountpoint /path/to/mountpoint /path/to/sparsebundle/$server.sparsebundle/;
/bin/sleep 10;
/usr/bin/find /path/to/mountpoint -type d -mtime +7 -exec /bin/rm -rf {} \;
/usr/bin/hdiutil unmount /path/to/mountpoint;
/bin/sleep 10;
/usr/bin/hdiutil compact /path/to/sparsebundle/$server.sparsebundle/;
done
exit;
One of the problems I thought was causing this was it needed to have a mountpoint specified since the default mount was to /Volumes/Time\ Machine\ Backups/ that's why I created a mountpoint. I also thought that it was trying to delete the files to quickly after mounting and it wasn't actually mounted yet, that's why I added the sleep. I've also tried using the -delete
option for find instead of -exec
, but it didn't make any difference.
Any help on this would be greatly appreciated because I'm out of ideas as to why this won't work.
First, thinning backups this way is a really really bad idea. First, when you use find's
-mtime
test on a directory, you're checking the last time that directory was modified; what you need to check is the last time something in that directory was modified. For instance, it's been 10 days since / (the root directory) on my Mac has been modified, so if I used your script on my backup (and it worked), it would delete my entire startup volume from all backups (including the latest one).Second, you're messing with Time Machine's carefully arranged backup structure, which may effectively corrupt the backup (including even future backups). Do not do this.
(Note: any time you see the command
rm
especially followed by-Rf
especially when running as root, make sure you know what it's pointing at before you press return.)Now, as for the actual problem, I suspect you're running into Time Machine's safety net, a kernel extension that tries to prevent damage to TM's backups. This is a good thing (see last point), and you shouldn't disable it.
There's a simple solution, though: use Time Machine. Specifically, use
tmutil delete /path/to/snapshot
to remove old snapshots. The snapshot path will be something like/path/to/mountpoint/Backups.backupdb/<servername>/<YYYY-MM-DD-HHMMSS>
.Note that deleting old snapshots only actually deletes files that aren't shared with other snapshots; so if you delete all snapshots older than a week, you aren't getting rid of (backups of) files that haven't been modified in a week, you're getting rid of files that were deleted more than a week ago. Which is much more likely to be what you want...