#!/bin/bash -vx # # This script is used to for syncoid replication from a remote host to a local # host with the added feature of destroying existing snapshots. This is needed # if you create snapshots with the same name over and over as snapshots can't # overwritten, they must be destroyed and recreated. We make a specific # named cold_backup when the virtual guests are powered off and it is this # snapshot that we are creating this script for. # https://stackoverflow.com/questions/1602378/how-to-calculate-a-hash-for-a-string-url-in-bash-for-wget-caching # https://stackoverflow.com/questions/21208736/removing-on-linux-md5sum # # Variables # # This is the root of the dataset you are backing up, the source. Note, we are # only backing up the child datasets, not the root. ZFS_SOURCE_DATASET_ROOT="vhsrv02_vg_images" # Name of ZFS snapshot that doesn't change, e.g. cold_backup STATIC_ZFS_SNAPSHOT="cold_backup" # This is the root of the dataset where the backup will go, the destination. # Be sure to create the destination datasets beforehand! ZFS_DESTINATION_DATASET_ROOT="vhsrv05_vg_backups/vhsrv02_vg_images" # The SSH private key (full path). Note, the user on the remote host must have # read access to the datasets you are backing up. SSH_PRIVATE_KEY="/root/.ssh/root@VHSRV05-vastermin@VHSRV02" # The remote user used for backups @ the remote host that will be backed up. REMOTE_USER_AND_HOST="vastermin@172.18.18.172" # End of Variables # # List snapshots of destination and source, remove all paths from and including @ to root of path # Create MD5 of snapshot lists and compare if they are equal, if not then delete the destinstation static zfs snapshot # STATIC_SNAPSHOT_LIST_DESTINATION=`zfs list -r -t snapshot -o name,creation -s creation $ZFS_DESTINATION_DATASET_ROOT | grep ${STATIC_ZFS_SNAPSHOT}` echo Destination list: $STATIC_SNAPSHOT_LIST_DESTINATION STATIC_SNAPSHOT_LIST_DESTINATION="${STATIC_SNAPSHOT_LIST_DESTINATION##*@}" echo Destination short list: $STATIC_SNAPSHOT_LIST_DESTINATION SNAPSHOT_MD5_DESTINATION=`/bin/echo $STATIC_SNAPSHOT_LIST_DESTINATION | /usr/bin/md5sum | cut -d' ' -f1` STATIC_SNAPSHOT_LIST_SOURCE=`ssh -i $SSH_PRIVATE_KEY $REMOTE_USER_AND_HOST "/sbin/zfs list -r -t snapshot -o name,creation -s creation $ZFS_SOURCE_DATASET_ROOT | grep $STATIC_ZFS_SNAPSHOT"` echo Source list: $STATIC_SNAPSHOT_LIST_SOURCE STATIC_SNAPSHOT_LIST_SOURCE="${STATIC_SNAPSHOT_LIST_SOURCE##*@}" echo Source short list: $STATIC_SNAPSHOT_LIST_SOURCE SNAPSHOT_MD5_SOURCE=`/bin/echo $STATIC_SNAPSHOT_LIST_SOURCE | /usr/bin/md5sum | cut -d' ' -f1` if [[ "$SNAPSHOT_MD5_DESTINATION" != "$SNAPSHOT_MD5_SOURCE" ]]; then zfs list -r -H -o name -t snapshot ${ZFS_DESTINATION_DATASET_ROOT} | grep ${STATIC_ZFS_SNAPSHOT} | xargs -n1 zfs destroy echo Destination and Source $STATIC_ZFS_SNAPSHOT snapshots are NOT the same echo MD5 of Destination $SNAPSHOT_MD5_DESTINATION echo MD5 of Source $SNAPSHOT_MD5_SOURCE fi /usr/local/sbin/syncoid --recursive --skip-parent --dumpsnaps --no-privilege-elevation --sshkey=${SSH_PRIVATE_KEY} "${REMOTE_USER_AND_HOST}:${ZFS_SOURCE_DATASET_ROOT}" ${ZFS_DESTINATION_DATASET_ROOT}