Gentoo Wiki


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

This article is still a Stub. You can help Gentoo-Wiki by expanding it.



This article describes how you can use ZFS to create backups of your data.


  1. You can backup directly via net-misc/rsync, giving you complete control.
  2. Your backup is a mirror, which can be directly mounted (so you can access them as you would local files).
  3. The history is stored as snapshots, which can be cloned and then mounted (i.e. can be accessed as local files).
  4. Snapshots are instantaneous and cheap -- They only consume space corresponding to the changeset.
  5. Dynamic compression of data and snapshots.
  6. The backups can be read by any machine supporting ZFS (includes Mac Os X, FreeBSD, OpenSolaris based machines)
  7. The adventurous can directly mount /home via ZFS. In this case, making a backup just involves taking a (cheap) snapshot.


  1. ZFS support on Linux is only via fuse
  2. Compression ratio is perhaps not optimal.

Backing up your data


Emerge sys-fs/zfs-fuse.

Preparing the backup device

Using a loop device

If you wish to use a loop device, then you must first create one: This creates a sparse loop device of size 1G:

dd if=/dev/zero of=loop bs=1M count=1 seek=1023

Your loop device can be on a remote file system (e.g. NFS / Samba).

Using a removable drive

If you want to use a removable flash drive, or SD card then insert the appropriate device (and kernel module) so that it becomes accessible. I also recommend you use custom udev rules so that it is mounted by ID in a separate directory. This is so that ZFS doesn't accidentally nuke data on a different flash device you insert.

This example shows you how to do this using an SD card:

File: /etc/udev/rules.d/10-local.rules
KERNEL=="mmcblk[0-9]", SUBSYSTEM=="block", SUBSYSTEMS=="mmc", ATTRS{name}=="SD02G", ATTRS{serial}=="0xe5a0067e", SYMLINK+="zfs/patriot_sdcard"

In this case, every time an SD card with serial ID 0xe5a0067e is inserted, a symlink to the device will be created in /dev/zfs/patriot_sdcard. You can find out more about udev rules at [1].

If your backup device is large enough, you might want to split it into two partitions, and set the partitions to mirror each other. When ZFS has more than one one device storing the same data, it can automatically correct file system errors. When it only has one device, ZFS can only detect, not correct, file system errors.

Creating the ZFS pool

Create a ZFS pool to store your backups:

zpool create backup /dev/zfs/patriot_sdcard

(If you have a second device you would like to use as a mirror, then just add it at the end of the above command).

Now create a filesystem for your backups. I call it the name of my computer (moror in this example).

zfs create -o compression=on backup/mordor

If you have different computers you want to backup on the same device, you can create a filesystem for each of them as above.

Importing / Exporting the pool

Before you can remove your backup device, you should export it:

zpool export backup

This is also recommended after completing your backups, so that you don't accidentally overwrite anything. When you reinsert the device, or need to access a backup, you can import it via

zpool import backup -d /dev/zfs/

Backing up your data

First import your ZFS pool as described above.

To backup your data, just use rsync. No need for any fancy backup software, since ZFS will handle all the snapshots and recovery.

rsync --delete -av /home/ /backup/mordor/home/
rsync --delete -av /etc/ /backup/mordor/etc/

If you don't want all your files backed up, you could use exclude lists.

File: ~/.backup_exclude
- *.o
- .*.swp
+ *

and use

rsync --delete --delete-excluded --exclude-from=$HOME/.backup_exclude \
   -av /home/ /backup/mordor/

In the above example, this will not backup any files with extension .o or swap files (typically created by Vim).

You can use sys-process/vixie-cron to automate the above process. Once you have completed the backup, you should export the ZFS pool so that it is not accidentally overwritten / damaged.

Taking snapshots

The above will only contain a backup of the current state of your files. The next time you backup, these will get overwritten. If you would like to maintain a history (recommended) then you should periodically take a snapshot.

zfs snapshot backup/mordor@2008-week30

Here, backup is the name of your ZFS pool, mordor is the name of your ZFS file system, and 2008-week30 is the name of the snapshot. The snapshot name can be arbitrary, but it is recommended you set it to something reflecting the time the snapshot was made.

You can again use sys-process/vixie-cron to automate this step. When you are done you should export the ZFS pool.

Accessing your backed up data

To access your backed up data, all you have to do is import the ZFS pool. After that the most recent backup should be available as local files in /backup/mordor/.

Unfortunately, zfs-fuse doesn't support browsing snapshots like the ZFS designers intended. So to access an older version of your data, you will have to clone the snapshot:

zfs clone backup/mordor@2008-week12 backup/mordor-2008-week12

After you are done retrieving your files, destroy the clone you created:

zfs destroy backup/mordor-2008-week12


TODO: Upload a few scripts that backup using the above process.


This page was originally written by --gi1242 21:19, 19 July 2008 (UTC)

Retrieved from ""

Last modified: Mon, 21 Jul 2008 02:59:00 +0000 Hits: 2,094