Skip to content

Instantly share code, notes, and snippets.

@kylhill
Last active February 19, 2024 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kylhill/afcb9e5cbb4b39b54269437288cc7275 to your computer and use it in GitHub Desktop.
Save kylhill/afcb9e5cbb4b39b54269437288cc7275 to your computer and use it in GitHub Desktop.
Backup & Restore ZFS to External USB Drive

Initial Backup

1. Create zpool on external drive

sudo zpool create -o ashift=12 backup /dev/disk/by-id/XXXX

2. Take a snapshot

Take a snapshot of the zpool you wish to backup

sudo zfs snapshot -r srv@backup-$(date '+%Y-%m-%d')

3. Send full snapshot to external drive

Send the full snapshot to the external drive, this may take some time.

sudo zfs send -R srv@backup-2023-08-30 | pv | sudo zfs recv -F backup

4. Hold snapshots

Hold snapshots, this prevents accidental deletion for later incremental reference.

sudo zfs hold -r keep srv@backup-2023-08-30
sudo zfs hold -r keep backup@backup-2023-08-30

5. Export the external zpool

Export the external zpool so your machine isn't looking for it

sudo zpool export backup

Incremental Backup

1. Import the external zpool

sudo zpool import backup

2. Take a snapshot

Take a snapshot of the zpool you wish to backup

sudo zfs snapshot -r srv@backup-$(date '+%Y-%m-%d')

3. Send incremental snapshot to external drive

Send the incremental snapshot to the external drive, this may take some time.

sudo zfs send -R -i srv@backup-2023-08-30 srv@backup-2023-08-31 | pv | sudo zfs recv -F backup

4. Hold new snapshots

Hold new snapshots, this prevents accidental deletion for later incremental reference.

sudo zfs hold -r keep srv@backup-2023-08-31
sudo zfs hold -r keep backup@backup-2023-08-31

5. Release old snapshots

Release old snapshots to allow deletion.

sudo zfs release -r keep srv@backup-2023-08-30
sudo zfs release -r keep backup@backup-2023-08-30

6. Delete old snapshots

Delete old snapshots to free up space.

sudo zfs destroy -r srv@backup-2023-08-30
sudo zfs destroy -r backup@backup-2023-08-30

7. Export the external zpool

Export the external zpool so your machine isn't looking for it

sudo zpool export backup

Restore

1. Import the external zpool

sudo zpool import backup

3. Send the snapshot from the external drive to the data volume

Send the snapshot to the data volume, this may take some time.

sudo zfs send backup@backup-2023-08-31 | pv | sudo zfs recv srv

4. Roll back to the snapshot

Roll back to the snapshot to make the data active

sudo zfs rollback srv@backup-2023-08-31

5. Export the external zpool

Export the external zpool so your machine isn't looking for it

sudo zpool export backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment