various scripts for various tasks.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zfsync.sh 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/sh
  2. # zfsync: sync zfs pool(s) between two servers
  3. # before first use, or in case of failure, unexpected reboot while syncing, etc.:
  4. #
  5. # launch "zfsync.sh init"
  6. #
  7. # or manually:
  8. #
  9. # - destroy all snapshots on both local & ${bro}
  10. # - for i in ftp web mail; do zfs snapshot servers/$i/home@newsync ;done
  11. # - for i in ftp web mail; do zfs send servers/$i/home@newsync | ssh ${bro} zfs receive -vFd servers ;done
  12. # - for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done
  13. # - ssh ${bro} 'for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done'
  14. #
  15. # once init is done, call it using crontab:
  16. # */10 * * * * root nice -n 19 /usr/local/bin/zfsync.sh >/tmp/zfsync.log
  17. bro="small"
  18. pools="ftp mail web"
  19. SENDNSCA=/usr/local/sbin/send_nsca\ -H\ 192.168.X.X\ -d\ ';;'\ -c\ /usr/local/etc/nagios/send_nsca.cfg
  20. HOSTNAME=`/bin/hostname -f | /usr/bin/cut -d '.' -f 1,1`
  21. SERVICENAME=zfsync
  22. LOGFILE=/tmp/zfsync.log
  23. LOCKFILE=/tmp/zfsync.lock
  24. ZROOT=zdata/servers
  25. sync_success=1
  26. if [ -e $LOCKFILE ]
  27. then
  28. echo "ERROR: $0 is already running with pid `cat $LOCKFILE`, check $LOCKFILE"
  29. exit 1
  30. fi
  31. echo ${$}>$LOCKFILE
  32. alert()
  33. {
  34. echo "${HOSTNAME};;${SERVICENAME};;2;CRITICAL - ZFS Sync failed at `date`, please check ${LOGFILE} for complete report." | ${SENDNSCA} >/dev/null
  35. exit $?
  36. }
  37. if [ $1 ]
  38. then
  39. if [ $1 = "init" ]
  40. then
  41. echo "`date`: ZFSync INIT starting."
  42. echo "wiping previous syncs"
  43. for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done
  44. ssh -q ${bro} 'for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done'
  45. echo "making full snapshots"
  46. for jail in $pools; do zfs snapshot $ZROOT/$jail/home@newsync;done
  47. echo "sending full snapshots"
  48. for jail in $pools; do zfs send $ZROOT/$jail/home@newsync | ssh -q ${bro} 'zfs receive -vFd zdata' ;done
  49. echo "renaming them to use them as a base for diff snapshots"
  50. for jail in $pools; do zfs rename $ZROOT/$jail/home@newsync $ZROOT/$jail/home@prevsync ;done
  51. ssh -q ${bro} 'for jail in ftp mail web; do zfs rename zdata/servers/$jail/home@newsync zdata/servers/$jail/home@prevsync ;done'
  52. echo "`date`: ZFSync INIT ending."
  53. exit $?
  54. fi
  55. fi
  56. echo "`date`: ZFSync starting."
  57. for pool in $pools
  58. do
  59. dataset="$ZROOT/${pool}/home"
  60. echo "making new snapshot for ${pool}"
  61. zfs snapshot ${dataset}@newsync || alert
  62. echo "sending snapshot"
  63. zfs send -i prevsync ${dataset}@newsync | ssh -q ${bro} "zfs receive -vFd zdata" || alert
  64. echo "cleaning"
  65. zfs destroy ${dataset}@prevsync || alert
  66. zfs rename ${dataset}@newsync ${dataset}@prevsync || alert
  67. ssh -q ${bro} "zfs destroy ${dataset}@prevsync && zfs rename ${dataset}@newsync ${dataset}@prevsync" || alert
  68. done
  69. echo "`date`: ZFSync ending."
  70. echo
  71. echo "${HOSTNAME};;${SERVICENAME};;0;OK - ZFS Sync succeeded at `date`" | ${SENDNSCA} >/dev/null
  72. rm -f $LOCKFILE