set zfs dataset read-only during sync (to avoid freezes)

This commit is contained in:
n 2022-03-20 23:48:16 +01:00
parent 5dff343760
commit 9ad905a732
Signed by: n
GPG key ID: 510227DD6C502CE3

View file

@ -2,7 +2,7 @@
# zfsync: sync zfs pool(s) between two servers # zfsync: sync zfs pool(s) between two servers
# before first use, or in case of failure, unexpected reboot while syncing, etc.: # in case of failure, unexpected reboot while syncing, etc.:
# #
# launch "zfsync.sh init" # launch "zfsync.sh init"
# #
@ -13,9 +13,6 @@
# - for i in ftp web mail; do zfs send servers/$i/home@newsync | ssh ${bro} zfs receive -vFd servers ;done # - for i in ftp web mail; do zfs send servers/$i/home@newsync | ssh ${bro} zfs receive -vFd servers ;done
# - for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done # - for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done
# - ssh ${bro} 'for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done' # - ssh ${bro} 'for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done'
#
# once init is done, call it using crontab:
# */10 * * * * root nice -n 19 /usr/local/bin/zfsync.sh >/tmp/zfsync.log
bro="small" bro="small"
@ -29,6 +26,7 @@ LOCKFILE=/tmp/zfsync.lock
ZROOT=zdata/servers ZROOT=zdata/servers
sync_success=1 sync_success=1
echo "`date`: ZFSync starting."
if [ -e $LOCKFILE ] if [ -e $LOCKFILE ]
then then
echo "ERROR: $0 is already running with pid `cat $LOCKFILE`, check $LOCKFILE" echo "ERROR: $0 is already running with pid `cat $LOCKFILE`, check $LOCKFILE"
@ -46,38 +44,38 @@ if [ $1 ]
then then
if [ $1 = "init" ] if [ $1 = "init" ]
then then
echo "`date`: ZFSync INIT starting."
echo "wiping previous syncs" echo "wiping previous syncs"
for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done
ssh -q ${bro} 'for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done' ssh -q ${bro} 'for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done'
echo "making full snapshots" echo "making full snapshots"
for jail in $pools; do zfs snapshot $ZROOT/$jail/home@newsync;done for jail in $pools; do zfs snapshot $ZROOT/$jail/home@newsync;done
echo "sending full snapshots" echo "sending full snapshots"
for jail in $pools; do zfs send $ZROOT/$jail/home@newsync | ssh -q ${bro} 'zfs receive -vFd zdata' ;done for jail in $pools; do zfs send $ZROOT/$jail/home@newsync | ssh -q ${bro} 'zfs receive -vFdu zdata' ;done
echo "renaming them to use them as a base for diff snapshots" echo "renaming them to use them as a base for diff snapshots"
for jail in $pools; do zfs rename $ZROOT/$jail/home@newsync $ZROOT/$jail/home@prevsync ;done for jail in $pools; do zfs rename $ZROOT/$jail/home@newsync $ZROOT/$jail/home@prevsync ;done
ssh -q ${bro} 'for jail in ftp mail web; do zfs rename zdata/servers/$jail/home@newsync zdata/servers/$jail/home@prevsync ;done' ssh -q ${bro} 'for jail in ftp mail web; do zfs rename zdata/servers/$jail/home@newsync zdata/servers/$jail/home@prevsync ;done'
echo "`date`: ZFSync INIT ending."
exit $? exit $?
fi fi
fi fi
echo "`date`: ZFSync starting."
for pool in $pools for pool in $pools
do do
dataset="$ZROOT/${pool}/home" dataset="$ZROOT/${pool}/home"
echo "making new snapshot for ${pool}" echo "making new snapshot for ${pool}"
zfs snapshot ${dataset}@newsync || alert zfs snapshot ${dataset}@newsync || alert
echo "sending snapshot" echo "sending snapshot"
zfs send -i @prevsync ${dataset}@newsync | ssh -q ${bro} "zfs receive -vFd zdata" || alert ssh -q ${bro} "zfs set readonly=on ${dataset}" || alert
sleep 2
zfs send -i @prevsync ${dataset}@newsync | ssh -q ${bro} "zfs receive -vFdu zdata" || alert
ssh -q ${bro} "zfs set readonly=off ${dataset}" || alert
echo "cleaning" echo "cleaning"
zfs destroy ${dataset}@prevsync || alert zfs destroy ${dataset}@prevsync || alert
zfs rename ${dataset}@newsync ${dataset}@prevsync || alert zfs rename ${dataset}@newsync ${dataset}@prevsync || alert
ssh -q ${bro} "zfs destroy ${dataset}@prevsync && zfs rename ${dataset}@newsync ${dataset}@prevsync" || alert ssh -q ${bro} "zfs destroy ${dataset}@prevsync && zfs rename ${dataset}@newsync ${dataset}@prevsync" || alert
done done
echo "`date`: ZFSync ending."
echo echo
echo "${HOSTNAME};;${SERVICENAME};;0;OK - ZFS Sync succeeded at `date`" | ${SENDNSCA} >/dev/null echo "${HOSTNAME};;${SERVICENAME};;0;OK - ZFS Sync succeeded at `date`" | ${SENDNSCA} >/dev/null
rm -f $LOCKFILE rm -f $LOCKFILE
echo "`date`: ZFSync ending."