Skip to content

Instantly share code, notes, and snippets.

@mariancerny
Last active March 18, 2023 16:15
Show Gist options
  • Save mariancerny/cbe7751c39910fb4abd0 to your computer and use it in GitHub Desktop.
Save mariancerny/cbe7751c39910fb4abd0 to your computer and use it in GitHub Desktop.
Scripts to perform ZFS hourly, daily and monthly backups using ZFS snapshot feature.
# ZFS Backups
3 0 * * * root sh /opt/sbin/zfs-backup.sh
2 1-23 * * * root sh /opt/sbin/zfs-backup-hourly.sh
33 10 9 * * root sh /opt/sbin/zfs-backup-monthly.sh
set -e
two_days_ago=$( date -v-2d +%A )
pools=`zpool list -H -o name`
datasets=`zfs list -d 1 -H -o name | grep /`
for pool in $pools
do
# Maly HACK ako sa vyhnut hlaske, ze snaphost s danym menom neexistuje.
# Vytvorime si snaphost priamo na hlavny dataset poolu, ktory vzapeti
# hned zmazeme.
zfs snapshot $pool@$two_days_ago
zfs destroy -r $pool@$two_days_ago
done
for dataset in $datasets
do
zfs rename -r $dataset@yesterday $two_days_ago || echo "Failed to zfs rename $dataset@yesterday"
zfs rename -r $dataset@today yesterday || echo "Failed to zfs rename $dataset@today"
zfs snapshot -r $dataset@today
done
set -e
hour=$( date +%H:00 )
pools=`zpool list -H -o name`
# BACKUP nesnapshotujeme kazdu hodinu, iba kazdy den.
datasets=`zfs list -d 1 -H -o name | grep / | grep -v BACKUP`
for pool in $pools
do
# Maly HACK ako sa vyhnut hlaske, ze snaphost s danym menom neexistuje.
# Vytvorime si snaphost priamo na hlavny dataset poolu, ktory vzapeti
# hned zmazeme.
zfs snapshot $pool@$hour
zfs destroy -r $pool@$hour
done
for dataset in $datasets
do
zfs snapshot -r $dataset@$hour
done
set -e
# Tento skript je potreba pustat 9. v mesiaci a premenuje poslednu zalohu (pred 8. dnami) na aktualny mesiac.
# Vynechava BACKUP a /tmp.
month=$( date +%B )
eight_days_ago=$( date -v-8d +%A )
pools=`zpool list -H -o name`
# BACKUP a /tmp nesnapshotujeme kazdy mesiac.
datasets=`zfs list -H -o name | grep / | egrep -v 'BACKUP|/tmp' `
for pool in $pools
do
# Maly HACK ako sa vyhnut hlaske, ze snaphost s danym menom neexistuje.
# Vytvorime si snaphost priamo na hlavny dataset poolu, ktory vzapeti
# hned zmazeme.
zfs snapshot $pool@$month
zfs destroy -r $pool@$month
done
for dataset in $datasets
do
zfs rename $dataset@$eight_days_ago $dataset@$month || echo "Failed to zfs rename $dataset@$eight_days_ago $dataset@$month"
done
@mariancerny
Copy link
Author

This set of scripts provides:

  • rotating daily snapshots: today, yesterday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
  • rotating hourly snapshots: 01:00, 02:00, ... 23:00
  • rotating monthly snapshots: January, February, ... December

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment