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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/sh
  2. # zfsync: sync zfs pool(s) between two servers
  3. # in case of failure, unepexpected reboot while syncing, etc.:
  4. # - destroy all snapshots on both local & ${bro}
  5. # - for i in ftp web mail; do zfs send servers/$i/home@newsync | ssh ${bro} zfs receive -vFd servers ;done
  6. # - for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done
  7. # - ssh ${bro} "for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done"
  8. bro="small"
  9. pools="ftp mail web"
  10. SENDNSCA=/usr/local/sbin/send_nsca\ -H\ <nagios_host>\ -d\ ';;'\ -c\ /usr/local/etc/nagios/send_nsca.cfg
  11. HOSTNAME=`/bin/hostname -f | /usr/bin/cut -d '.' -f 1,1`
  12. SERVICENAME=zfsync
  13. LOGFILE=/tmp/zfsync.log
  14. sync_success=1
  15. alert()
  16. {
  17. echo "${HOSTNAME};;${SERVICENAME};;2;CRITICAL - ZFS Sync failed at `date`, please check ${LOGFILE} for complete report." | ${SENDNSCA} >/dev/null
  18. exit $?
  19. }
  20. if [ $1 ]
  21. then
  22. if [ $1 = "init" ]
  23. then
  24. echo "`date`: ZFSync INIT starting."
  25. echo "wiping previous syncs"
  26. for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done
  27. ssh -q ${bro} 'for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done'
  28. echo "making full snapshots"
  29. for jail in $pools; do zfs snapshot servers/$jail/home@newsync;done
  30. echo "sending full snapshots"
  31. for jail in $pools; do zfs send servers/$jail/home@newsync | ssh -q ${bro} 'zfs receive -vFd servers' ;done
  32. echo "renaming them to use them as a base for diff snapshots"
  33. for jail in $pools; do zfs rename servers/$jail/home@newsync servers/$jail/home@prevsync ;done
  34. ssh -q ${bro} 'for jail in ftp mail web; do zfs rename servers/$jail/home@newsync servers/$jail/home@prevsync ;done'
  35. exit $?
  36. fi
  37. fi
  38. echo "`date`: ZFSync starting."
  39. for pool in $pools
  40. do
  41. dataset="servers/${pool}/home"
  42. echo "making new snapshot for ${pool}"
  43. zfs snapshot ${dataset}@newsync || alert
  44. echo "sending snapshot"
  45. zfs send -i prevsync ${dataset}@newsync | ssh -q ${bro} "zfs receive -vFd servers" || alert
  46. echo "cleaning"
  47. zfs destroy ${dataset}@prevsync || alert
  48. zfs rename ${dataset}@newsync ${dataset}@prevsync || alert
  49. ssh -q ${bro} "zfs destroy ${dataset}@prevsync && zfs rename ${dataset}@newsync ${dataset}@prevsync" || alert
  50. done
  51. echo
  52. echo "${HOSTNAME};;${SERVICENAME};;0;OK - ZFS Sync succeeded at `date`" | ${SENDNSCA} >/dev/null