Here’s a cheat sheet for ZFS snapshots, focusing on essential commands and concepts to help you efficiently work with them.
ZFS Snapshot Basics
- What is a ZFS Snapshot?
- A snapshot is a read-only, point-in-time copy of a ZFS file system or volume. It takes almost no space at creation but grows as the data in the file system changes.
1. Creating Snapshots
- Command:
bash zfs snapshot <pool>/<dataset>@<snapshot-name>
- Example:
bash zfs snapshot mypool/mydataset@backup-2024-09-26
2. Listing Snapshots
- Command:
bash zfs list -t snapshot
- This will show all snapshots with their size and creation date.
3. Renaming Snapshots
- Command:
bash zfs rename <pool>/<dataset>@<old-snapshot-name> <pool>/<dataset>@<new-snapshot-name>
- Example:
bash zfs rename mypool/mydataset@backup-2024-09-26 mypool/mydataset@backup-sept
4. Rolling Back to a Snapshot
- Command:
bash zfs rollback <pool>/<dataset>@<snapshot-name>
- Rolling back returns the dataset to the state when the snapshot was taken. Note: Any changes made after the snapshot will be lost.
- Example:
bash zfs rollback mypool/mydataset@backup-sept
5. Cloning Snapshots
- Command:
bash zfs clone <pool>/<dataset>@<snapshot-name> <pool>/<new-dataset>
- Creates a new writeable dataset based on a snapshot.
- Example:
bash zfs clone mypool/mydataset@backup-sept mypool/clone-dataset
6. Deleting Snapshots
- Command:
bash zfs destroy <pool>/<dataset>@<snapshot-name>
- Example:
bash zfs destroy mypool/mydataset@backup-sept
- Delete multiple snapshots in one go:
bash zfs destroy <pool>/<dataset>@<snap1>,<snap2>,<snap3>
7. Incremental Send/Receive with Snapshots
- Sending a snapshot:
- Command:
bash zfs send <pool>/<dataset>@<snapshot-name> | <destination>
-
Example:
bash zfs send mypool/mydataset@backup-sept > /backup/backup-sept.zfs
-
Incremental send between snapshots:
bash zfs send -i <snapshot1> <pool>/<dataset>@<snapshot2> | <destination>
-
Example:
bash zfs send -i backup-2024-09-20 mypool/mydataset@backup-sept > /backup/diff-backup-sept.zfs
-
Receiving a snapshot:
- Command:
bash zfs receive <pool>/<dataset>
- Example:
bash zfs receive mypool/restore-dataset < /backup/backup-sept.zfs
8. Automatic Snapshot Creation Using cron and zfs-auto-snapshot
- Install and configure
zfs-auto-snapshot
: - On many systems, you can install
zfs-auto-snapshot
to automate snapshot creation. - Set schedules (e.g., hourly, daily, weekly).
Example usage:
bash
zfs-auto-snapshot --label=hourly --keep=24 <pool>/<dataset>
9. Showing Differences Between Snapshots
- Command:
bash zfs diff <pool>/<dataset>@<snapshot1> <pool>/<dataset>@<snapshot2>
- Shows a list of differences between two snapshots.
- Example:
bash zfs diff mypool/mydataset@backup-sept mypool/mydataset@backup-oct
10. Hold/Release Snapshots
- To prevent a snapshot from being deleted, you can hold it:
bash zfs hold <tag> <pool>/<dataset>@<snapshot-name>
-
Example:
bash zfs hold keep mypool/mydataset@backup-sept
-
To release it:
bash zfs release <tag> <pool>/<dataset>@<snapshot-name>
- Example:
bash zfs release keep mypool/mydataset@backup-sept
11. ZFS Snapshot Retention Policies (Manual Cleanup)
- It’s good practice to periodically delete old snapshots, which can be done manually using
zfs destroy
or automated with a custom script or tools likezfs-auto-snapshot
.
12. Snapshot Space Usage
- Check how much space snapshots are using:
bash zfs list -t snapshot
13. Exporting a Pool with Snapshots
- Command:
bash zpool export <pool-name>
Useful Commands Summary:
- Create:
zfs snapshot <pool>/<dataset>@<name>
- List:
zfs list -t snapshot
- Rollback:
zfs rollback <pool>/<dataset>@<name>
- Send:
zfs send <pool>/<dataset>@<name> | <destination>
- Receive:
zfs receive <pool>/<dataset>
- Clone:
zfs clone <pool>/<dataset>@<name> <pool>/<clone-name>
- Delete:
zfs destroy <pool>/<dataset>@<name>
- Hold:
zfs hold <tag> <pool>/<dataset>@<name>
- Release:
zfs release <tag> <pool>/<dataset>@<name>
This cheat sheet should help streamline your ZFS snapshot operations! Let me know if you need more details on any of the commands or features.