ZFS gives us the ability to move data with ZFS send and receive. This can be used to combine snapshots, create full or incremental backups, or replicate data between servers on your LAN or even over the WAN.
Local Backup with Send
This is very simple, simply perform a send of a snapshot then output it to the file, I have complicated it a bit to include a folder which is named off of the current date (as would be helpful with backups).
# zfs send tank/filesystem@now > /backup/`date +%m%d%Y`/tank-filesystem.zsnd
Keep in mind zfs send only makes the backups. In order to restore them we will need to be familiar with zfs receive as well.
Local Restore with Receive
With the basic restore it is very easy, simply specify the file system to restore to, and then define the file to pull it from.
# zfs receive tank/restoredfilesystem < /backup/03152013/tank-filesystem.zsnd
Notice here we are doing redirection back into the ZFS receive command from the file, also important to note that
Local Copy of a File System – Full Clone
Occasionally you may have the need to perform a full copy clone. With a normal zfs clone you will end up with a zero copy clone (copy-on-write), you will also have additional dependency when using zfs clones, which require a little extra caution. If using zfs send and receive to make a copy, then there is no interdependency.
# zfs send tank/originalfilesystem@now | zfs receive tank/copyfilesystem
Here we are able to structure the command with just a simple pipe to separate the send and receive statements.
Local Compressed Backup with Send
Perhaps you want to store your backups in a compressed form. All we need to do is insert a gzip into the mix.
# zfs send tank/test@now | gzip > /backup/`date +%m%d%Y`/tank-originalfilesystem.zsnd.gz
This method combines a pipe with a redirect, and will give us a compressed file.
Local Compressed Restore with Receive
Of course backups don’t do any good for us if we cannot restore them.
# gunzip -c -d /backup/03152013/tank-originalfilesystem.zsnd.gz | zfs receive tank/copyfilesystem
Here we unzip the file, and pipe that into the zfs receive statement.
Local Encrypted (and Compressed) Backup with Send
Some workloads have serious security requirements. This will encrypt and compress the contents of the file system into a backup file. In this example I am using encrypt/decrypt to perform the encryption with aes, I also was unable to get the piping and redirection working without also injecting gzip as well.
# zfs send tank/test@now | encrypt -a aes | gzip > /backup/`date +%m%d%Y`/tank-originalfilesystem.zsnd.aes.gz Enter passphrase: Re-enter passphrase:
Since we aren’t using any keys with encrypt it will prompt us for a passphrase. Remember when using encryption your data becomes worthless if you do not
Local Encrypted (and Compressed) Restore with Receive
Decrypting and restoring the backup file is pretty straight forward, unzip the file, pipe it through decrypt and pipe it into zfs.
# gunzip -c -d /backup/03152013/tank-originalfilesystem.zsnd.aes.gz | decrypt -a aes | zfs receive tank/copyfilesystem Enter passphrase:
Use the passphrase that you used when encrypting the backup to decrypt it.
Remote Copy of a File System – Full Clone
Sometimes a full clone is needed from one server to another, perhaps for data migration.
# zfs send tank/originalfilesystem@now | ssh email@example.com "zfs receive tank/copyfilesystem"