This commit is contained in:
Milan Meduna 2019-03-30 16:52:46 +01:00
parent a34f8228ce
commit ea834f0b2a
2 changed files with 87 additions and 0 deletions

18
example-config Normal file
View file

@ -0,0 +1,18 @@
#Backup server settings
backup_server="192.168.0.10"
backup_server_port="22"
backup_server_dataset="tank/backups"
backup_server_delete_old_snaps=y
backup_server_number_keep_snaps=31
#Source server settings NOT CHANGE AFTER FIRST FULL BACKUP!
zfsnap_enable=y
zfsnap_days_keep_snaps=7
source_datasets="
tank/dataset1
tank/dataset2
"

69
zfs-backup-mm Normal file
View file

@ -0,0 +1,69 @@
#!/bin/bash
(
flock -n 200 || { echo 'locked via flock on file /var/lock/zfs-backup-mm' ; exit 1; }
zfsnap_block () {
if [ $zfsnap_enable = "y" ]; then
for dataset in $source_datasets;
do
echo "Creating snapshot on "$dataset""
zfSnap -d -a "$zfsnap_days_keep_snaps"d "$dataset"
done
backup_block
else
echo "zfsnap_enable is not set to Y, skipping creating snapshots"
backup_block
fi
}
backup_block () {
source_zfs_snapshots=$(zfs list -t snapshot)
backups_server_zfs_snapshots=$(ssh -p $backup_server_port $backup_server zfs list -t snapshot)
for dataset in $source_datasets;
do
source_snap=$(echo "$source_zfs_snapshots" | grep "$dataset"@ | grep -E "....-..-.._........--"$zfsnap_days_keep_snaps"d" | tail -n1 | cut -d"@" -f2 | cut -d" " -f1)
dataset_clean=$(echo $dataset | grep -o '[^/]*$')
backup_snap=$(echo "$backups_server_zfs_snapshots" | grep "$backup_server_dataset"/"$dataset_clean"@ | grep -E "....-..-.._........--"$zfsnap_days_keep_snaps"d" | tail -n1 | cut -d"@" -f2 | cut -d" " -f1)
if [ -z "$backup_snap" ]
then
echo "Snapshot on backup server not found, sending "$dataset"@"$source_snap" to server "$backup_server" "$backup_server_dataset"/"$dataset_clean""
zfs send -c "$dataset"@"$source_snap" | pv | ssh "$backup_server" -p "$backup_server_port" zfs recv "$backup_server_dataset"/"$dataset_clean"
else
echo "Snapshot on backup server found, sending incremental from "$dataset"@"$backup_snap" to "$dataset"@"$source_snap" to server "$backup_server" "$backup_server_dataset"/"$dataset_clean""
zfs send -ci "$dataset"@"$backup_snap" "$dataset"@"$source_snap" | pv | ssh "$backup_server" -p "$backup_server_port" zfs recv "$backup_server_dataset"/"$dataset_clean" -F
fi
done
del_snaps_block
}
del_snaps_block () {
if [ $backup_server_delete_old_snaps = "y" ]; then
for dataset in $source_datasets;
do
dataset_clean=$(echo $dataset | grep -o '[^/]*$')
snaps_to_delete=$(echo "$backups_server_zfs_snapshots" | grep "$backup_server_dataset"/"$dataset_clean"@ | grep -E "....-..-.._........--"$zfsnap_days_keep_snaps"d" | head -n -"$zfsnap_days_keep_snaps" | cut -d" " -f1)
for snap in $snaps_to_delete
do
echo "Deleting snapshot "$snap" on "$backup_server""
ssh "$backup_server" -p "$backup_server_port" zfs destroy "$snap"
done
done
else
echo "backup_server_delete_old_snaps is not set to Y, skipping deleting snapshots"
fi
exit 0
}
if [[ $# -eq 0 ]] ; then
echo 'Config file is not set'
exit 1
fi
source $1
zfsnap_block
) 200>/var/lock/zfs-backup-mm