cybercyber

Copying custom properties from one set of zfs snapshots to another

There was a bug in recent versions of the zfs-for-linux that lead to “zfs send | zfs receive” dropping all your custom properties (such as the ones used by the autosnapshot-tool).

Warning: Since I used it to copy the autosnapshot-labels, I wanted the tool to delete snapshots that were since removed from the source dataset. Be careful!

Use this to copy them over:

#!bash
if [ $# -lt 3 ]; then
        echo Usage: $0 '<PROPERTY>' '<FROM>' '<TO>'
        exit 1
fi

PROPERTY=$1
FROM=$2
TO=$3

zfs list -rH -t snapshot -o $PROPERTY,name -s name $TO | awk '/^-/{print $2;}' | while read snapshot; do
        saveIFS=$IFS
        IFS=@
        set -- $snapshot
        IFS=$saveIFS
        if ! zfs list -H -o name -t snapshot $FROM@$2 > /dev/null 2>&1; then
                echo zfs destroy $snapshot
                zfs destroy $snapshot
        else
                value=$(zfs get -H -o value $PROPERTY $FROM@$2)
                echo zfs set $PROPERTY=$value $snapshot
                zfs set $PROPERTY=$value $snapshot
        fi
done