apfs-auto-snapshot icon indicating copy to clipboard operation
apfs-auto-snapshot copied to clipboard

Time Machine automatically nuking snapshots

Open vimalloc opened this issue 7 years ago • 10 comments

It looks like time machine will occasionally nuke snapshots itself. If I had to guess, it is probably doing something along the lines of tmutil thinlocalsnapshots /.

As it stands, the only way I found to create APFS snapshots is via tmutil, so unless whatever is deleting the snapshots we created is run on a cron or something that we can disable, there isn't much to be done about this. Ideally we would have a C API or a different set of tools outside of time machine that let us create APFS snapshots, but until that is a thing we will take what we can get.

This won't cause the program to crash, if it finds that a snapshot was removed out from under it, it will just remove that snapshots metadata from the database and keep happily running. But it does mean that I cannot guarantee the amount of snapshots to keep limits found in the config file will be honored.

vimalloc avatar Sep 28 '17 17:09 vimalloc

Doing testing with an actual snapshot schedule, it looks like it doesn't let me keep more then 4 or 5 snapshots at a time, which equates to never having hourly snapshots kept. This is super unfortunate, but unless apply provides a way around this i don't see what could be done.

vimalloc avatar Sep 29 '17 00:09 vimalloc

It looks like there is a way to create snapshots via a C syscall:

#include <fcntl.h>
#include <stdio.h>
#include <sys/attr.h>
#include <sys/snapshot.h>

int main() {
	int dirfd = open("/", O_RDONLY, 0);
	if (dirfd < 0) {
		perror("open");
		return(1);
	}

	int ret = fs_snapshot_create(dirfd, "snapshot_test", 0);
	if (ret != 0)
		perror("fs_snapshot_create");
	return (ret);
}

However, running this, even as root, returns the error fs_snapshot_create: Operation not permitted. Looking at this thread (https://forums.developer.apple.com/thread/89635), it looks like we would need a special entitlement to get that to work, and based on what that sounds like I highly doubt apple would give me one (stupid apple). But, if I have some more time, I will see if it is possible.

vimalloc avatar Nov 11 '17 17:11 vimalloc

Did you find any more info regarding the entitlement required and how to request this?

leftytennis avatar Jan 26 '18 21:01 leftytennis

I havent tried this myself, but when reading the snapshot documentation of CCC, it seems they have found a way around this. The interface distinguishes between snapshots made by CCC and snapshots made by Time Machine. Also it guarantees the following:

† CCC applies a more conservative retention policy for the startup disk — weekly snapshots are not retained, and daily snapshots are only retained for 7 days. You can customize those settings if you want a longer retention for snapshots on the startup disk, but be sure to consider the implications this will have on disk usage on your startup disk.

Jip-Hop avatar Jun 29 '18 10:06 Jip-Hop

I think I found the answer here:

Time Machine should only monkey with its own snapshots. If you create a snapshot via other means, Time Machine should leave it alone. As far as creating such a snapshot, that requires a special entitlement as per this thread. Share and Enjoy

Seem like the key is to use man fs_snapshot_create instead of tmutil.

Jip-Hop avatar Jun 29 '18 11:06 Jip-Hop

Whoops. I now see you already posted that article on 11 Nov 2017. I guess CCC was able to get this special entitlement because they're a backup application developer.

Jip-Hop avatar Jun 29 '18 11:06 Jip-Hop

Seems really odd to me that this special entitlement is required at all and not just require elevated privileges for the effective uid...

leftytennis avatar Jun 29 '18 16:06 leftytennis

Seriously, it's super dumb 👎

vimalloc avatar Jun 29 '18 16:06 vimalloc

You can mount the snapshot somewhere, and Time Machine won't be able to delete it. I've tested mounting multiple snapshots and it did not create any problems.

sclsj avatar Mar 12 '20 01:03 sclsj

Another option: use snaputil. See here: https://github.com/ahl/apfs/issues/2.

sclsj avatar Sep 01 '21 14:09 sclsj