2014-09-14 00:36:27 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# zfsync: sync zfs pool(s) between two servers
|
|
|
|
|
2022-03-20 23:48:16 +01:00
|
|
|
# in case of failure, unexpected reboot while syncing, etc.:
|
2016-10-08 23:55:56 +02:00
|
|
|
#
|
|
|
|
# launch "zfsync.sh init"
|
|
|
|
#
|
|
|
|
# or manually:
|
|
|
|
#
|
2014-09-14 00:36:27 +02:00
|
|
|
# - destroy all snapshots on both local & ${bro}
|
2014-09-28 21:55:24 +02:00
|
|
|
# - for i in ftp web mail; do zfs snapshot servers/$i/home@newsync ;done
|
2014-09-14 00:36:27 +02:00
|
|
|
# - 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
|
2016-10-08 23:55:56 +02:00
|
|
|
# - ssh ${bro} 'for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done'
|
|
|
|
|
2014-09-14 00:36:27 +02:00
|
|
|
|
|
|
|
bro="small"
|
|
|
|
pools="ftp mail web"
|
|
|
|
|
2017-04-03 22:49:24 +02:00
|
|
|
SENDNSCA=/usr/local/sbin/send_nsca\ -H\ 192.168.X.X\ -d\ ';;'\ -c\ /usr/local/etc/nagios/send_nsca.cfg
|
2014-09-14 00:36:27 +02:00
|
|
|
HOSTNAME=`/bin/hostname -f | /usr/bin/cut -d '.' -f 1,1`
|
|
|
|
SERVICENAME=zfsync
|
|
|
|
LOGFILE=/tmp/zfsync.log
|
2016-10-08 23:55:56 +02:00
|
|
|
LOCKFILE=/tmp/zfsync.lock
|
|
|
|
ZROOT=zdata/servers
|
2014-09-14 00:36:27 +02:00
|
|
|
sync_success=1
|
|
|
|
|
2022-03-20 23:48:16 +01:00
|
|
|
echo "`date`: ZFSync starting."
|
2016-10-08 23:55:56 +02:00
|
|
|
if [ -e $LOCKFILE ]
|
|
|
|
then
|
|
|
|
echo "ERROR: $0 is already running with pid `cat $LOCKFILE`, check $LOCKFILE"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo ${$}>$LOCKFILE
|
|
|
|
|
2014-09-14 00:36:27 +02:00
|
|
|
alert()
|
|
|
|
{
|
|
|
|
echo "${HOSTNAME};;${SERVICENAME};;2;CRITICAL - ZFS Sync failed at `date`, please check ${LOGFILE} for complete report." | ${SENDNSCA} >/dev/null
|
|
|
|
exit $?
|
|
|
|
}
|
2016-10-08 23:55:56 +02:00
|
|
|
|
2014-09-14 00:36:27 +02:00
|
|
|
if [ $1 ]
|
|
|
|
then
|
|
|
|
if [ $1 = "init" ]
|
|
|
|
then
|
|
|
|
echo "wiping previous syncs"
|
|
|
|
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"
|
2016-10-08 23:55:56 +02:00
|
|
|
for jail in $pools; do zfs snapshot $ZROOT/$jail/home@newsync;done
|
2014-09-14 00:36:27 +02:00
|
|
|
echo "sending full snapshots"
|
2022-03-20 23:48:16 +01:00
|
|
|
for jail in $pools; do zfs send $ZROOT/$jail/home@newsync | ssh -q ${bro} 'zfs receive -vFdu zdata' ;done
|
2014-09-14 00:36:27 +02:00
|
|
|
echo "renaming them to use them as a base for diff snapshots"
|
2016-10-08 23:55:56 +02:00
|
|
|
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'
|
2014-09-14 00:36:27 +02:00
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
for pool in $pools
|
|
|
|
do
|
2016-10-08 23:55:56 +02:00
|
|
|
dataset="$ZROOT/${pool}/home"
|
2014-09-14 00:36:27 +02:00
|
|
|
echo "making new snapshot for ${pool}"
|
|
|
|
zfs snapshot ${dataset}@newsync || alert
|
|
|
|
echo "sending snapshot"
|
2022-03-20 23:48:16 +01:00
|
|
|
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
|
2014-09-14 00:36:27 +02:00
|
|
|
echo "cleaning"
|
|
|
|
zfs destroy ${dataset}@prevsync || alert
|
|
|
|
zfs rename ${dataset}@newsync ${dataset}@prevsync || alert
|
|
|
|
ssh -q ${bro} "zfs destroy ${dataset}@prevsync && zfs rename ${dataset}@newsync ${dataset}@prevsync" || alert
|
|
|
|
done
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "${HOSTNAME};;${SERVICENAME};;0;OK - ZFS Sync succeeded at `date`" | ${SENDNSCA} >/dev/null
|
2016-10-08 23:55:56 +02:00
|
|
|
|
|
|
|
rm -f $LOCKFILE
|
2022-03-20 23:48:16 +01:00
|
|
|
echo "`date`: ZFSync ending."
|