How to setup znapzend with local backup on OpenIndiana
Tags: openindiana, illumos, znapzend, zfs
Published: 2021-05-01 16:11:28

This guide will show you how to set up `znapzend` on OpenIndiana to backup a ZFS fileystem to a different ZFS filesystem. It should also work with other Illumos distributions.

# Assumptions

1. The backup destination ZFS zpool, e.g. `destinationzpool`, has already been created
2. The backup destination ZFS dataset, e.g. `destinationzpool/destinationzfsdataset` has already been created 
3. Both of the above are on the same machine as the source dataset

See the [Oracle Solaris docs](https://docs.oracle.com/en/operating-systems/solaris.html) for instructions on Steps 1 to 3. At that link, pick the latest Solaris release and search the ensuing page for "ZFS."

# Step 1: Set up the pkgsrc repo

As of this writing, `znapzend` does not exist in the OpenIndiana repos, so you'll have to [set up the 3rd party pkgsrc repo that has it](https://pkgsrc.joyent.com/install-on-illumos/). 

# Step 2: Install znapzend

```
# pkgin install znapzend
```

# Step 3: Configure znapzend

The [official documentation](https://github.com/oetiker/znapzend/blob/master/doc/znapzendzetup.pod) is overly complicated for this purpose. A simple example config is:

```
znapzendzetup create --recursive SRC '1h=>15min,1d=>1h,1w=>1d,1m=>1w,1y=>1m' sourcezpool DST '1h=>15min,1d=>1h,1w=>1d,1m=>1w,1y=>1m' destinationzpool/destinationzfsdataset
```

Explaining the above command:

* `znapzendzetup`: Set up `znapzend`
* `create`: Generate a new backup config
* `--recursive`: Backup all child datasets on `sourcezpool`
* `SRC`: Source zpool settings begin here
* `'1h=>15min,1d=>1h,1w=>1d,1m=>1w,1y=>1m'`: For each comma-separated parameter, take a snapshot at interval equal to the value to the right of the arrow, and then destroy that snapshot after the value to the left of the arrow, e.g. `1h=>15min` means take a snapshot every 15 minutes and destroy all such snapshots after they've existed for an hour. See official documentation for more options
* `sourcezpool`: The zpool you want to backup
* `DST`: Destination zpool & ZFS dataset settings begin here
* `'1h=>15min,1d=>1h,1w=>1d,1m=>1w,1y=>1m'`: Same as before
* `destinationzpool/destinationzfsdataset`: The destination zpool and ZFS dataset

# Step 4: Check your znapzend config

```
# znapzend --noaction --debug
```

The output of the above command may appear to freeze and not return you to a prompt. If that happens just hit `CTRL + C` until the prompt shows up again. As long as the output has no errors, your config should be good.

# Step 5: Start znapzend in the background

```
# znapzend --daemonize
```

# Step 6: Wait for the smallest interval set in Step 3

# Step 7: Check that znapzend is creating snapshots as configured

```
# zfs list -t snapshot
```

You should see snapshots with `%Y-%m-%d-%H%M%S` (`znapzend`'s default) timestamps in their names.

# Step 8: Kill znapzend

There are many ways to do this, but I prefer using `htop` (install it from the OI repos if you haven't already):

* `# htop`
* Press `F4` to filter
* Type `znapzend`
* Use the arrow keys to highlight any matching entries
* Press `F9` while each matching entry is highlighted to kill it
* Press `F10` to exit

# Step 9: (Optional) Disable other snapshot utilities

CoW filesystems like ZFS sometimes have difficulty with a large number of snapshots combined with low space. Also, destroying snapshots is computationally expensive, and taking them too frequently can slow down the machine. 

Therefore, it's a good idea to disable other snapshot utilities if they are likely to cause such issues. For example, if you use Time Slider:

* Open Time Slider
* Uncheck `Enable Time Slider`
* Click `Delete Snapshots`
* In the window that follows, click `Select All`
* Click `Delete`

The deletion process may take a very long time (I suggest running it overnight.) If any Time Slider snapshots remain after the bulk deletion, just run it again and that should take care of the rest.

# Step 10: Generate a znapzend service manifest

Wouldn't it be nice if you could start `znapzend` at boot? Unlike Linux and FreeBSD, OI's crontab syntax lacks an `@reboot` option, so anything that starts with OS has to be a service. While `@reboot` makes things simple, service creation has some advantages, such as alerting the user when a service isn't running as expected.

```
sudo svccfg export znapzend
```

Copy the output of that command to a text editor. You could also possibly use `|` or `echo` to combine this and the following manifest file creation steps.

**BONUS:** You can use `svccfg export` combined with the following steps to turn just about any background executable into a service!

# Step 11: Create the znapzend service manifest XML file

Create the following file `/var/svc/manifest/site/znapzend.xml` using your preferred method, then copy the output of Step 10 to it and save the file.

I use:

```
# nano /var/svc/manifest/site/znapzend.xml
```

* Copy the output from Step 10 into `nano`
* Press `CTRL + O` to save the file
* Press `CTRL + X` to exit

# Step 12: Validate the manifest file

```
# svccfg validate /var/svc/manifest/site/znapzend.xml
```

# Step 13: Import the manifest file

```
# svccfg import /var/svc/manifest/site/znapzend.xml
```

# Step 14: Start the znapzend service

```
# svcadm enable znapzend
```

And that's it. Now you have automatic, incremental, rotating, easily restored backups of your OS filesystem.