Skip to content

Instantly share code, notes, and snippets.

@Sieboldianus
Last active January 27, 2024 10:21
Show Gist options
  • Save Sieboldianus/1bf84dcdaf1b845bbc8155d757ec6fe5 to your computer and use it in GitHub Desktop.
Save Sieboldianus/1bf84dcdaf1b845bbc8155d757ec6fe5 to your computer and use it in GitHub Desktop.
Syncoid: Script to pull ZFS datasets from a file with a list of key:value, to replace non-working --recursive option in case of sendoptions=R
#!/bin/bash
################################################################################
#
# Shell script to pull zfs snapshots with syncoid using a list of key:value pairs
# Works with sendoptions=R, where syncoid's --recursive flag cannot be used
#
# IMPORTANT: Use bash, not sh! e.g.: > bash pull_snapshots.sh
#
################################################################################
# Exit as soon as a command fails
set -e
# Accessing an empty variable will yield an error
set -u
FILE_SNAPSHOTS=snapshots.txt
pull_syncoid() {
ZFS_SOURCE=$1
ZFS_TARGET=$2
# we use </dev/null to redirect standard input for the syncoid command
# which might otherwise consume the while loop's input
syncoid --no-sync-snap --no-privilege-elevation \
--sshkey /home/recvuser/.ssh/id_ed25519 \
--sendoptions=Rw \
'proxmox':"$ZFS_SOURCE" "$ZFS_TARGET" </dev/null
}
while IFS=: read -r zfs_source zfs_target; do
# skip commented lines
[[ "$zfs_source" =~ ^[[:space:]]*# ]] && continue
# skip empty lines
[ -z "$zfs_source" ] && continue
printf 'Pulling: %s\n' "$zfs_source -> $zfs_target"
pull_syncoid "$zfs_source" "$zfs_target";
done < "$FILE_SNAPSHOTS"
# source:target
# tank_hdd/data:tank/hdd/data
tank_hdd/test:tank/hdd/test
tank_hdd/lxc/dataset1:tank/hdd/lxc/dataset1
tank_ssd/lxc/dataset2:tank/ssd/lxc/dataset2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment