====== ZFS Health Scripts ====== ===== Checkpool ===== * Check all pools and email operator if errors detected #!/bin/sh RESULT="`zpool status | grep errors`" ZPOOLSTATUS="`zpool status`" HOSTNAME="`hostname`" TMPFILE="/tmp/zfs_report" #CHANGE ME MAILTO="-@-" #CHANGE ME RESULT_GOOD="errors: No known data errors" if [ "${RESULT}" = "${RESULT_GOOD}" ] then echo "no errors" echo "no ZFS Errors on ${HOSTNAME}" > ${TMPFILE} echo "${ZPOOLSTATUS}" >> ${TMPFILE} #mail -s "ZFS pool Report" ${MAILTO} < ${TMPFILE} else echo "errors detected" echo "no ZFS Errors on ${HOSTNAME}" > ${TMPFILE} echo "${ZPOOLSTATUS}" >> ${TMPFILE} mail -s "ERRORS DETECTED!!! ZFS pool Report" ${MAILTO} < ${TMPFILE} fi ===== ZFS Scrub ===== * Check to see if it's the weekend then scrub main pool #!/bin/sh WEEKDAY="`date | cut -d " " -f1`" POOL="tank" if [ ${WEEKDAY} = "Sat" ] then echo "It's Saturday Running Scrub" zpool scrub ${POOL} else echo "It's not Saturday, exiting" fi ===== ZFS Rep ===== #!/bin/sh pool="tank/vol2" destination="zroot" remote_pool="zroot/vol2" host="10.1.0.61" today=`date +"%Y-%m-%d"` #yesterday=`date -v -1d +"$type%Y-%m-%d"` yesterday="2017-01-07" # create today snapshot snapshot_today="${pool}@${today}" # look for a snapshot with this name if zfs list -H -o name -t snapshot | sort | grep "${snapshot_today}" > /dev/null; then echo " snapshot, ${snapshot_today}, already exists" # exit 1 else echo " taking todays snapshot, ${snapshot_today}" zfs snapshot -r ${snapshot_today} fi # look for yesterday snapshot snapshot_yesterday="$pool@$yesterday" snapshot_yesterday_r="$remote_pool@$yesterday" snapshot_today_r="$remote_pool@${today}" #echo $snapshot_yesterday if ssh root@$host "zfs list -H -o name -t filesystem | sort | grep \"${remote_pool}\"" > /dev/null; then echo " Pool found on remote" else echo " Pool missing on remote, aborting" exit 1; fi if zfs list -H -o name -t snapshot | sort | grep "${snapshot_yesterday}" > /dev/null; then echo " yesterday snapshot, ${snapshot_yesterday}, exists locally" if ssh root@$host "zfs list -H -o name -t snapshot | sort | grep \"${snapshot_yesterday_r}\"" > /dev/null; then echo " yesterday snapshot, ${snapshot_yesterday_r}, exists on destination" if ssh root@$host "zfs list -H -o name -t snapshot | sort | grep \"${snapshot_today_r}\"" > /dev/null; then echo " Remote already has today's snapshot aborting sync" exit 2 else echo " proceeding with incremental sync" zfs send -R -i ${snapshot_yesterday} ${snapshot_today} | ssh root@$host "zfs receive -Fduv ${destination}" echo " sync complete" exit 0; fi else echo " yesterday snapshot, ${snapshot_yesterday_r}, missing on destination" if ssh root@$host "zfs list -H -o name -t snapshot | sort | grep \"${snapshot_today_r}\"" > /dev/null; then echo " Remote already has today's snapshot aborting sync" exit 2 else echo " proceeding with full sync" zfs send ${snapshot_today} | ssh root@$host "zfs receive -Fduv ${destination}" echo " sync complete" exit 0; fi fi else echo " missing yesterday snapshot aborting, ${snapshot_yesterday}" exit 1 fi ===== ZFS Clean Snapshots ===== #!/bin/sh pool="zroot/vol2" # iterate i from 15 to 62 # any snapshot older than 2 weeks (14 days) or (15-62 days) old is deleted for i in 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 do CLEANDATE=`date -v -${i}d +"$type%Y-%m-%d"` CLEAN_SNAP="${pool}@${CLEANDATE}" echo $CLEAN_SNAP if zfs list -H -o name -t snapshot | sort | grep "$CLEAN_SNAP" > /dev/null; then zfs destroy -r $CLEAN_SNAP fi done