# ZFS: Automated Snapshots

Snapshots allow you to copy the state of your zpool at a given instance. This can be useful in many ways. For instance, if you delete a file that you still want and you have a snapshot of a time when you had the file you can go retrieve it. Snapshots are cheap to make, so one is encouraged to make them often. Before setting up automated snapshots I had only ever made two snapshots. You can view your snapshots with zfs list -t snapshot:

In this output fresh was made when I had just made the zpool and home_restored was made just after I had restored my home directory from backup to the zpool.

This is a great start, but we don’t want to just rely on making snapshots by hand. It would be more useful if we have some automated system for taking snapshots. Thankfully, such tools already exists. I am going to install zfs-auto-snapshot-git from the AUR, which is from the folks at zfsonlinux. Upon installation we will see the following output:

This automated system used to use cronjobs to run the included zfs-auto-snapshot script, but it looks like back in September 2014 the cronjobs were swapped out for systemd timers.

If you are unfamiliar with systemd timers (like I was when I came across this) they are simply timers which control a systemd service. You can view active timers with systemctl list-timers. You will notice that when we installed zfs-auto-snapshot-git a set of .service and .timer files were installed in /usr/lib/systemd/system:

Each .service and .timer pair is for a time interval for making snapshots. Let’s take a look at the hourly pair.

### zfs-auto-snapshot-hourly.service

As you can see the files are fairly simple. Persistent=true in the .timer file will run the job if we happened to miss the last time it should have run, say if your computer was off or suspended, like an anacron job. The .service file just runs the the zfs-auto-snapshot script, labels it as hourly and keeps 24 snapshots. The // refers to all storage nodes.

To enable automated snapshots we will first have to set an auto-snapshot parameter as stated in the installation output:

Then we just have to enable whatever timer(s) we want, this will enable an automated backup at that interval:

You can always manually run zfs-auto-snapshot which will produce a single backup. I have setup an hourly, daily, weekly and monthly auto-snapshot which has been running now for two months and everything seems to be going smoothly.