***Guide***How do I set up a cron job to automate ZFS snapshots on Unraid

To set up a cron job for automating ZFS snapshots on Unraid, follow these detailed steps:

Prerequisites

  1. Unraid Server: Ensure your Unraid server is up and running.
  2. ZFS Plugin: Install the ZFS plugin from the Unraid Community Applications.
  3. Snapshot Script: Obtain a snapshot script, such as zfs-auto-snapshot.sh, which can be found in various repositories like SpaceInvaderOne’s GitHub.

Step 1: Create the Snapshot Script

  1. Download the Script: Get the zfs-auto-snapshot.sh script from a reliable source (e.g., SpaceInvaderOne’s GitHub).
  2. Place the Script: Move it to a persistent directory, such as /mnt/user/scripts/.
mv /path/to/zfs-auto-snapshot.sh /mnt/user/scripts/
chmod +x /mnt/user/scripts/zfs-auto-snapshot.sh

Step 2: Create a Cron Job

Unraid does not retain crontab settings after a reboot, so you will need to create a custom cron job setup.

  1. Create a Cron Jobs Directory:
mkdir -p /boot/config/plugins/cronjobs
  1. Create a Cron File:

Create a file named zfs_snap.cron in the cron jobs directory:

nano /boot/config/plugins/cronjobs/zfs_snap.cron
  1. Add Cron Job Entry:

Add the following line to schedule your snapshot creation. This example runs the snapshot script at 11:55 PM and 11:55 AM daily:

55 11,23 * * * /mnt/user/scripts/zfs-auto-snapshot.sh // -k 28 --label=01

This command will keep the latest 28 snapshots.

Step 3: Load Cron Jobs on Boot

To ensure your cron jobs are loaded after each reboot, add the following line to your go file located at /boot/config/go:

crontab /boot/config/plugins/cronjobs/zfs_snap.cron

Step 4: Verify Cron Jobs

After setting everything up, you can verify that your cron job is running by checking the logs or manually executing the script to see if it creates snapshots as expected.

Additional Configuration (Optional)

  • User Scripts Plugin: Consider installing the User Scripts plugin from Community Applications for easier management of scripts and scheduling.
  • Retention Policy: Modify your snapshot script to include logic for deleting old snapshots if necessary.

Conclusion

By following these steps, you will have successfully set up a cron job on Unraid to automate ZFS snapshots, ensuring regular backups of your datasets without manual intervention. This setup enhances data safety and recovery options for your Unraid server.