Why all the snapshots?
Snapshots are a popular feature of modern file systems like APFS. This article explains what they are, how they come about, and what their benefits and problems are in macOS.
What is a snapshot?
Each volume, whether in HFS+ or APFS, is a complete file system, containing all the information about the files within it, details of directories or folders, and where all the data for those files is to be found on the disk hosting that volume. File system data is constantly changing, as files are created, updated and deleted. When a file is deleted, its entry in the list of files in that volume is removed, and the storage blocks used to contain the file’s data are marked for removal, thus to be returned for reuse.
If you were to make a complete copy of that file system, that would describe the volume at that instant in time. To accompany that you could then preserve all the storage blocks containing its file data. From that moment on, instead of returning storage blocks for reuse, if you retain them, you could revert that volume to the state it was in at that instant. That’s just what a snapshot does.
Concentrating for a moment on a single file, the file system has an inode record containing attributes for that file, such as its time of creation, and those are linked to a list of the storage blocks containing the file’s data, its file extents. At the moment that a snapshot is made of that volume, those inode records and extents are copied to form the snapshot itself, and all the data blocks listed in the extents must be preserved.
This shows the same file in a snapshot and in the current volume, a little after that snapshot had been made. Extents for the data of the earlier version of that file contained in the snapshot are shown at the top, and consist of blocks EA, EB, EC and ED. After that snapshot was made, the file was edited and now consists of the blocks shown at the bottom, EA, FB, FC and ED.
Thus, the extents listed for that file in the snapshot consist of two blocks, EA and ED, that are currently in use and included in its current extents, and two blocks, EB and EC, that were deleted after the snapshot was made. As those are referenced in the snapshot’s extents, those storage blocks are retained to enable the snapshot to restore the volume’s previous state. When that snapshot is deleted, blocks EB and EC will then be returned to the pool of free blocks for erasure and reuse.
Snapshot size
Snapshots are stored within the same container holding the volume, and are linked to the current volume, although not stored inside it (as that would create a recursive volume structure and catastrophe). The size of the snapshot itself, the copy of the file system, is relatively small, but the size of the retained storage, blocks EB and EC in the diagram, can be far larger.
Imagine that, just after making a snapshot, you delete a 100 GB file, like a Virtual Machine. So long as that snapshot remains, the storage occupied by that huge file has to be retained, and can’t be released for reuse. So that snapshot has a minimum effective size of 100 GB until it’s deleted.
If you make a snapshot now and keep it for several days, it grows steadily in size. Each storage block containing deleted file data must be retained as long as the snapshot is kept, so over hours and days, that snapshot accumulates more retained data, and grows. Ultimately, it could come to occupy all the space in that container, unless you delete the snapshot. When you do, all its retained storage is released for reuse, and free space grows as a result.
Because the largest snapshot is normally the oldest, as it contains the most preserved file data, when deleting snapshots it’s usual to delete the oldest first. If you did want to delete a newer snapshot, APFS would work out what file data can be removed without affecting older snapshots, just don’t expect the removal to free up as much space.
Using snapshots
Because HFS+ was designed in the last century for use primarily on rotating hard disks, it doesn’t support snapshots at all. They’re a central feature of APFS, so we have more limited experience of working and living with their peculiarities.
Making a snapshot is so quick as to be almost instant, and typically takes around 0.01 second, but deleting one takes upwards of 0.2 seconds because all it retained data have to be marked for reuse, and any remaining snapshots left intact.
Snapshots can be used like a backup that has been made to the same disk. As they’re a complete file system, they can be mounted like any other volume, and you can use that to copy files, or even to restore the current volume, a process often known as rolling back to that snapshot. Although those are convenient, as they don’t require a separate backup, they depend on much of the contents of the current volume, and can easily be affected by the same corruption or other problems affecting the volume you’re trying to fix. So they’re an additional benefit and not a substitute for proper backups made on separate and independent storage.
Time Machine and third-party backup utilities therefore make and maintain snapshots as well as regular backups. Time Machine does so because it uses the data in snapshots to help create its backups. So every hour, when Time Machine comes to make an automatic backup, it creates a local snapshot of each volume it’s going to back up. To prevent those snapshots from taking ever-increasing amounts of storage space, it automatically deletes those snapshots after 24 hours. Time Machine’s backups are also snapshots, but are specially created on backup storage, not alongside the volume being backed up like its local snapshots.
Scope and limits
There are two other important properties of snapshots in APFS: they’re always whole-volume, and are read-only.
Some modern file systems allow you to create snapshots of only part of a volume, but APFS doesn’t. All its snapshots contain the whole contents of a volume. If you add a folder from that volume to the list of exceptions for TM backups, its local snapshots will still contain that folder, and can’t be excluded from the snapshot.
This is good in some ways, as it means that you can always copy or restore from a snapshot even when some of its contents were excluded from your backups. It’s bad in others if you put very large files into a folder in a volume that’s being backed up: changes in those large files will make that volume’s snapshots very large. If you want to exclude files from taking up space in snapshots, then you have to move them to a separate volume that isn’t backed up at all, so doesn’t have snapshots made of it.
If a snapshot does contain a very large or unwanted file, because snapshots are read-only, there’s no way to delete that file from the snapshot, and it’ll remain there until the snapshot itself is deleted. Other modern file systems may allow you to tamper with a snapshot after it has been made, but APFS doesn’t, so you can always rely on that snapshot remaining faithful to the original volume.
One final limitation at present is that you can’t copy a snapshot, in particular to another storage drive. You can mount that snapshot and copy its contents to another volume, make a snapshot of that volume, and use that, but that’s not the same.
Maintenance
You can maintain and remove snapshots using a range of different utilities, including Disk Utility (where you need to enable their display in the View menu), third-party backup utilities such as Carbon Copy Cloner, and some other tools like BackupLoupe.
Some features in macOS still don’t handle snapshots at all well. For instance, Storage in General settings usually classifies the space occupied by snapshots as ‘system data’, which is vague and unhelpful. Unless you have a third-party utility, it’s best to trust the management of snapshots to Disk Utility, which handles them most reliably.
Summary
- A snapshot is composed of a copy of the file system at an instant in time, together with all preserved file data required to recreate that volume as it was at that instant. It’s stored alongside that volume, in the same container.
- The size of a snapshot is almost entirely determined by the amount of file data preserved by it. As more changes are made to the current volume, so more file data must be kept to be able to recreate the volume as it was when the snapshot was made, and the size of the snapshot grows.
- Snapshots can mounted as a volume, and files copied from them, or the whole volume restored. They’re not independent from the current volume, though, so can’t replace proper backups on separate storage.
- Time Machine makes hourly local snapshots of each volume it’s backing up, and automatically deletes them after 24 hours. Snapshots are also used by other backup utilities.
- Snapshots include everything in a volume, without exception. To ensure files aren’t included in snapshots, move them to a volume that isn’t backed up by Time Machine.
- You can’t remove anything from a snapshot once it has been made, as they’re read-only and immutable.
- You can’t copy snapshots.
- Space occupied by snapshots can grow large. Use Disk Utility or a third-party utility to inspect and maintain them.