FreeNAS ZFS snapshot backup to Amazon S3
Tags: freenas, zfs, amazons3
Published: 2017-07-05 15:16:58

---
title: FreeNAS ZFS snapshot backup to Amazon S3
published: true
description: Backing up your FreeNAS ZFS snapshots to Amazon S3
tags: FreeNAS,ZFS,Amazon S3
canonical_url: https://www.mourtada.se/freenas-zfs-snapshot-backup-amazon-s3/
---
Originally posted at [www.mourtada.se](https://www.mourtada.se/freenas-zfs-snapshot-backup-amazon-s3/)

I've been looking for a way to backup my FreeNAS ZFS snapshots to an offsite location. I didn't find much information how to do this so i had to come up with my own solution.

In this post I'm going to show you how to save your encrypted ZFS snapshots in Amazon S3. We're going to use a FreeBSD jail together with <a href="https://www.gnupg.org/">GnuPG</a> and <a href="https://github.com/s3tools/s3cmd">s3cmd</a>.
<h2>Adding a jail in FreeNAS</h2>
Go to the FreeNAS web ui and click Jails. Click add and choose name. If you click advanced here you can change the ip-adress for the jail(I wanted to use DHCP).


<img class="size-full wp-image-657" src="https://www.mourtada.se/wp-content/uploads/2017/07/Screen-Shot-2017-07-05-at-15.40.09.png" alt="Adding an empty jail in Freenas" width="508" height="887" />

Click ok and FreeNAS will setup a new jail for you which takes a minute or two.

From now on we will have to work in the FreeNAS shell(SSH must be enabled under services in the Web UI).

To list all the jails running on your FreeNAS host we can run:
<pre>$ jls
</pre>
Verify that the jail you created is listed.

To enter the jail run:
<pre>$ jexec your_jail_name
$ # Verify that your in the jail
$ hostname
backup
</pre>
We're going to need to install some packages. First we need GnuPG which we'll use to encrypt our snapshots. Then we will need the s3cmd which is used for uploading our snapshots to Amazon s3.
<pre>$ pkg install security/gnupg
$ pkg install net/py-s3cmd</pre>
I'm going to use symmetric AES256 encryption with a passphrase file because i don't want to store my data in the cloud unencrypted. So generate a random passphrase which you will need to store at multiple locations(not just inside the jail). Because if the passphrase is lost your backups will be worthless. The passphrase file needs to be accessible by the backupscript. I have placed my passphrase file in in the root directory.
<pre>$ echo "mypassphrase" &gt; /root/snapshot-gpg-passphrase
$ chmod 400 /root/snapshot-gpg-passphrase
</pre>
Next we'll create a folder holding our current list of snapshots that we're going to keep synced with S3.
<pre>$ mkdir /root/s3_sync_bucket
$ chmod 600 /root/s3_sync_bucket
</pre>
We also need to configure s3cmd so run this and answer all questions:
<pre>$ s3cmd --configure
</pre>
<h2>The backup script</h2>
This script should be run on the FreeNAS host. What is does:
<ol>
 	<li>Creates a snapshot of the specified dataset</li>
 	<li>Sends it to the backup jail where it's encrypted and saved to file</li>
 	<li>Removes the snapshot on the FreeNAS host</li>
 	<li>Removes all snapshots older than 7 days</li>
 	<li>Syncs the local s3 bucket directory with S3 using s3cmd</li>
</ol>
```sh
#!/bin/sh

# The first argument should be the name of the dataset to backup
if test -z $1; then
  echo "Please specify a valid dataset"
  exit 1
fi

# Passphrase file
passphrase_file=/root/snapshot-gpg-passphrase

# Local directory to syn with Amazon S3 
bucket_dir=/root/s3_sync_bucket

# The S3 bucket url
s3_bucket="s3://your-bucket/"

# Generate a snapshot name
snapshot_name="$1@$(date +%Y-%m-%d)"

# Convert to valid filename
filename=$(echo $snapshot_name | sed "s/\//-/g").gpg

echo "Using bucket_dir: $bucket_dir"
echo "Using s3 bucket: $s3_bucket"
echo "Using snapshot_name: $snapshot_name"
echo "Using filename: $filename"

# Create the snapshot, send it to the jail, encrypt it 
zfs snapshot $snapshot_name
zfs send -v $snapshot_name | jexec backup gpg --batch --symmetric --cipher-algo AES256 --passphrase-file $passphrase_file --output $bucket_dir/$filename
zfs destroy $snapshot_name

# Remove old snapshots
search_word=$(echo $filename | sed "s/@.*$//g")
echo "Searching for \"$search_word\" in $bucket_dir older than 7 days"
jexec backup find $bucket_dir -name $search_word* -mtime +7 -exec rm {} \;

# Sync to s3
jexec backup s3cmd sync --delete-removed $bucket_dir $s3_bucket
```

Create the script and run it manually or with crontab
<pre>$ touch /root/backup_script.sh
$ chmod 700 /root/backup_script.sh
$ /root/backup_script.sh my-pool/my-dataset
</pre>
Edit the script to fit your needs.
<h2>Decrypting a backup</h2>
To decrypt a backup:
<pre>$ gpg --batch --decrypt --passphrase-file /root/pass-gpg &lt; backup_file</pre>