Automatic backups with ZFS + cron
In this guide we set up automatic ZFS snapshotsthat are created on their own at fixed intervals.
The snapshot command
Code: Select all
sudo zfs snapshot mypool/data@$(date +%Y-%m-%d-%H%M)
⏰ Adding it to cron
Open cron:
crontab -e
Add this line:
0 * * * * sudo zfs snapshot mypool/data@$(date +\%Y-\%m-\%d-\%H\%M)
👉 This makes a backup every hour
🗑️ Deleting old snapshots (very important)
sudo zfs list -t snapshot
Example of deleting one:
sudo zfs destroy mypool/data@snapshot-name
🔥 Automatic clean-up (optional)
You can add a second cron job:
0 3 * * * sudo zfs destroy $(zfs list -H -t snapshot -o name | head -n -10)
👉 It keeps the last 10 snapshots
✅ Advantages
fully automatic backups
no external tools
very fast recovery
ideal for VPS and storage servers