Script for backup zfs datasets via SSH to any ZFS capable host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.5 KiB

#!/bin/bash
if [[ ! -f "$1" ]]
then
echo "Config file not exists!"
exit 1
fi
if [ -z "$2" ]
then
lock_variable="/var/lock/zfs-backup-mm"
else
lock_variable="$2"
fi
(
flock -n 200 || { echo "locked via flock on file $lock_variable" ; exit 1; }
zfsnap_block () {
IFS='
'
if [ $zfsnap_enable = "y" ]; then
for dataset in $source_datasets;
do
echo "Tuning zfSnap for "$dataset""
for command in $(zfSnap -n -d -zpool28fix -a "$zfsnap_days_keep_snaps"d "$dataset" | grep "$dataset")
do
echo "Doing "$command""
eval "$command";
done
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 -$backup_server_ipv -p $backup_server_port $backup_server zfs list -t snapshot)
for dataset in $source_datasets;
do
dataset_clean=$(echo $dataset | grep -o '[^/]*$')
source_snap=$(echo "$source_zfs_snapshots" | grep "$dataset"@ | grep -E "....-..-.._........--"$zfsnap_days_keep_snaps"d" | tail -n1 | cut -d"@" -f2 | cut -d" " -f1)
source_snaps=$(echo "$source_zfs_snapshots" | grep "$dataset"@ | grep -E "....-..-.._........--"$zfsnap_days_keep_snaps"d" | cut -d"@" -f2 | cut -d" " -f1)
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)
backup_snaps=$(echo "$backups_server_zfs_snapshots" | grep "$backup_server_dataset"/"$dataset_clean"@ | grep -E "....-..-.._........--"$zfsnap_days_keep_snaps"d" | cut -d"@" -f2 | cut -d" " -f1)
source_backups_server_match=$(grep -Fxf <(echo "$source_snaps") <(echo "$backup_snaps"))
source_backups_server_match_tailed=$(echo "$source_backups_server_match" | tail -n1)
dataset_recordsize=$(zfs get recordsize -H -o value "$dataset")
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 "$dataset"@"$source_snap" | lzop | pv -L "$shape_megabytes"m | ssh -$backup_server_ipv "$backup_server" -p "$backup_server_port" "lzop -d | zfs recv -o recordsize="$dataset_recordsize" "$backup_server_dataset"/"$dataset_clean"" && echo "Send OK"
else
echo "Snapshot on backup server found, sending incremental from "$dataset"@"$source_backups_server_match_tailed" to "$dataset"@"$source_snap" to server "$backup_server" "$backup_server_dataset"/"$dataset_clean""
zfs send -I "$dataset"@"$source_backups_server_match_tailed" "$dataset"@"$source_snap" | lzop | pv -L "$shape_megabytes"m | ssh -$backup_server_ipv "$backup_server" -p "$backup_server_port" "lzop -d | zfs recv -o recordsize="$dataset_recordsize" "$backup_server_dataset"/"$dataset_clean" -F" && echo "Send OK"
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"@ | head -n -"$backup_server_number_keep_snaps" | cut -d" " -f1)
for snap in $snaps_to_delete
do
echo "Deleting snapshot "$snap" on "$backup_server""
ssh -$backup_server_ipv "$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>$lock_variable