snapraid-diff-n-sync.sh.j2 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #!/bin/bash
  2. # {{ ansible_managed }}
  3. #######################################################################
  4. # This is a helper script that keeps snapraid parity info in sync with
  5. # your data and optionally verifies the parity info. Here's how it works:
  6. # 1) It first calls diff to figure out if the parity info is out of sync.
  7. # 2) If parity info is out of sync, AND the number of deleted files exceed
  8. # X (configurable), it triggers an alert email and stops. (In case of
  9. # accidental deletions, you have the opportunity to recover them from
  10. # the existing parity info)
  11. # 3) If partiy info is out of sync, AND the number of deleted files exceed X
  12. # AND it has reached/exceeded Y (configurable) number of warnings, force
  13. # a sync. (Useful when you get a false alarm above and you can't be bothered
  14. # to login and do a manual sync. Note the risk is if its not a false alarm
  15. # and you can't access the box before Y number of times the job is run to
  16. # fix the issue... Well I hope you have other backups...)
  17. # 4) If parity info is out of sync BUT the number of deleted files did NOT
  18. # exceed X, it calls sync to update the parity info.
  19. # 5) If the parity info is in sync (either because nothing changed or after it
  20. # has successfully completed the sync job, it runs the scrub command to
  21. # validate the integrity of the data (both the files and the parity info).
  22. # Note that each run of the scrub command will validate only a (configurable)
  23. # portion of parity info to avoid having a long running job and affecting
  24. # the performance of the box.
  25. # 6) Once all jobs are completed, it sends an email with the output to user
  26. # (if configured).
  27. #
  28. #
  29. # CHANGELOG
  30. # ---------
  31. # 23/10/2011 Initial release
  32. # 04/01/2015 Updated script to handle changes in SnapRAID v7.0
  33. # Added scrub job as an optional task (after diff and sync)
  34. # 06/01/2015 Made the script more robust by adding checks to make sure preceding
  35. # jobs completed as expected before continuing with the subsequent jobs.
  36. # Made emailing output to user optional.
  37. # 24/01/2015 Inserted a sed step to clean up crlf (aka dos/unix formatting issue)
  38. # in sync & scrub outputs.
  39. # Detect sync and scrub job failures and highlight to user via warning
  40. # subject line in email to user.
  41. # 25/01/2015 Added option to reduce progress report output in email (default is 2 -
  42. # report only in 10% intervals).
  43. # 26/01/2015 For terse = 2 setting, removed lines for 1-8% from output
  44. # 05/02/2015 Added logic to perform forced sync after X number of warnings
  45. # Cleaned up formatting in script file (changed tabs to spaces)
  46. # Made consistent the use of [ in the test statements
  47. # 08/02/2015 Added warning number to the email subject line so that it is easier to
  48. # tell how many warnings have been issued so far
  49. # 04/03/2015 Corrected Scrub job status check (i.e. added check for text "Nothing
  50. # to do") to avoid sending false warning email
  51. # 27/10/2015 Corrected Sync job status check (i.e. added check for text "Nothing to
  52. # do") to avoid sending false warning email
  53. # 29/10/2015 Fixed a bug with the job status check not detecting the right strings
  54. #
  55. #######################################################################
  56. # Expand PATH for smartctl
  57. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  58. ## USER DEFINED SETTINGS ##
  59. # address where the output of the jobs will be emailed to.
  60. # comment it out to disable email output
  61. EMAIL_ADDRESS="blaine@nas.home.lan"
  62. # Set the threshold of deleted files to stop the sync job from running.
  63. # NOTE that depending on how active your filesystem is being used, a low
  64. # number here may result in your parity info being out of sync often and/or
  65. # you having to do lots of manual sync.
  66. DEL_THRESHOLD=1
  67. # Set number of warnings before we force a sync job.
  68. # This option comes in handy when you cannot be bothered to manually
  69. # start a sync job when DEL_THRESHOLD is breached due to false alarm.
  70. # Set to 0 to ALWAYS force a sync (i.e. ignore the delete threshold above)
  71. # Set to -1 to NEVER force a sync (i.e. need to manual sync if delete threshold is breached)
  72. SYNC_WARN_THRESHOLD=-1
  73. # Set percentage of array to scrub if it is in sync.
  74. # i.e. 0 to disable and 100 to scrub the full array in one go
  75. # WARNING - depending on size of your array, setting to 100 will take a very long time!
  76. SCRUB_PERCENT=15
  77. SCRUB_AGE=20
  78. # Set the option to log SMART info. 1 to enable, any other values to disable
  79. SMART_LOG=1
  80. # this script will log its actions to a file at this location
  81. LOG_FILE="/tmp/snapRAID.log"
  82. # location of the snapraid binary
  83. SNAPRAID_BIN="/usr/bin/snapraid"
  84. # location of the mail program binary
  85. MAIL_BIN="/usr/bin/mail"
  86. # how much progress output do we want to keep in email
  87. # Default is 2 which means report progress in 10% intervals
  88. # Set to 1 to report progress in 1% intervals
  89. # Set to 0 to report everything
  90. TERSE=2
  91. ## INTERNAL TEMP VARS ##
  92. EMAIL_SUBJECT_PREFIX="[`hostname`] SnapRAID -"
  93. TMP_OUTPUT="/tmp/snapRAID.out"
  94. SYNC_WARN_FILE="/tmp/snapRAID.warnCount"
  95. SYNC_WARN_COUNT=""
  96. # auto determine names of content and parity files
  97. #CONTENT_FILE=`cat /etc/snapraid.conf | grep snapraid.content | head -n 1 | cut -d " " -f2`
  98. #PARITY_FILE=`cat /etc/snapraid.conf | grep snapraid.parity | head -n 1 | cut -d " " -f2`
  99. CONTENT_FILE=`grep -v '^$\|^\s*\#' /etc/snapraid.conf | grep snapraid.content | head -n 1 | cut -d " " -f2`
  100. PARITY_FILE=`grep -v '^$\|^\s*\#' /etc/snapraid.conf | grep snapraid.parity | head -n 1 | cut -d " " -f2`
  101. # redirect all stdout to log file (leave stderr alone thou)
  102. exec >> $LOG_FILE
  103. # timestamp the job
  104. echo "[`date`] SnapRAID Job started."
  105. echo "SnapRAID DIFF Job started on `date`" > $TMP_OUTPUT
  106. echo "----------------------------------------" >> $TMP_OUTPUT
  107. #TODO - mount and unmount parity disk on demand!
  108. #sanity check first to make sure we can access the content and parity files
  109. if [ ! -e $CONTENT_FILE ]; then
  110. echo "[`date`] ERROR - Content file ($CONTENT_FILE) not found!"
  111. echo "ERROR - Content file ($CONTENT_FILE) not found!" >> $TMP_OUTPUT
  112. exit 1;
  113. fi
  114. if [ ! -e $PARITY_FILE ]; then
  115. echo "[`date`] ERROR - Parity file ($PARITY_FILE) not found!"
  116. echo "ERROR - Parity file ($PARITY_FILE) not found!" >> $TMP_OUTPUT
  117. exit 1;
  118. fi
  119. # run the snapraid DIFF command
  120. echo "[`date`] Running DIFF Command."
  121. $SNAPRAID_BIN diff >> $TMP_OUTPUT
  122. # wait for the above cmd to finish
  123. wait
  124. echo "----------------------------------------" >> $TMP_OUTPUT
  125. echo "SnapRAID DIFF Job finished on `date`" >> $TMP_OUTPUT
  126. JOBS_DONE="DIFF"
  127. DEL_COUNT=$(grep -w '^ \{1,\}[0-9]* removed$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
  128. ADD_COUNT=$(grep -w '^ \{1,\}[0-9]* added$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
  129. MOVE_COUNT=$(grep -w '^ \{1,\}[0-9]* moved$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
  130. COPY_COUNT=$(grep -w '^ \{1,\}[0-9]* copied$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
  131. UPDATE_COUNT=$(grep -w '^ \{1,\}[0-9]* updated$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
  132. # sanity check to make sure that we were able to get our counts from the output of the DIFF job
  133. if [ -z "$DEL_COUNT" -o -z "$ADD_COUNT" -o -z "$MOVE_COUNT" -o -z "$COPY_COUNT" -o -z "$UPDATE_COUNT" ]; then
  134. # failed to get one or more of the count values, lets report to user and exit with error code
  135. echo "[`date`] ERROR - failed to get one or more count values. Unable to proceed. Exiting script."
  136. if [ $EMAIL_ADDRESS ]; then
  137. # $MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX WARNING - Unable to proceed with SYNC/SCRUB job(s). Check DIFF job output inside." "$EMAIL_ADDRESS" < $TMP_OUTPUT
  138. curl \
  139. -H "Title: $EMAIL_SUBJECT_PREFIX WARNING" \
  140. -H "Priority: urgent" \
  141. -H "Tags: warning" \
  142. -d "Unable to proceed with SYNC/SCRUB job(s). Check DIFF job output inside
  143. $(cat $TMP_OUTPUT)" \
  144. https://ntfy.warchildstory.com/{{ server_notifications_topic }}
  145. fi
  146. exit 1;
  147. fi
  148. echo "SUMMARY of changes - Added [$ADD_COUNT] - Deleted [$DEL_COUNT] - Moved [$MOVE_COUNT] - Copied [$COPY_COUNT] - Updated [$UPDATE_COUNT]" >> $TMP_OUTPUT
  149. # check if the conditions to run SYNC are met
  150. # CHK 1 - if files have changed
  151. if [ $DEL_COUNT -gt 0 -o $ADD_COUNT -gt 0 -o $MOVE_COUNT -gt 0 -o $COPY_COUNT -gt 0 -o $UPDATE_COUNT -gt 0 ]; then
  152. # CHK 1 - YES, files have changed
  153. # CHK 2 - if number of deleted files exceed DEL_THRESHOLD
  154. if [ $DEL_COUNT -lt $DEL_THRESHOLD ]; then
  155. # CHK 2 - NO, delete threshold not reached, lets run the sync job
  156. echo "Deleted files ($DEL_COUNT) did not exceed threshold ($DEL_THRESHOLD), proceeding with sync job." >> $TMP_OUTPUT
  157. echo "[`date`] Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] and deleted files ($DEL_COUNT) is below threshold ($DEL_THRESHOLD). Running SYNC Command."
  158. DO_SYNC=1
  159. else
  160. #CHK 2 - YES, delete threshold breached! print warning message to both outputs
  161. echo "Number of deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD)." >> $TMP_OUTPUT
  162. echo "[`date`] WARNING - Deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD). Check $TMP_OUTPUT for details."
  163. # CHK 3 - if forced sync is set
  164. if [ $SYNC_WARN_THRESHOLD -gt -1 ]; then
  165. # CHK 3 - YES
  166. echo "Forced sync is enabled." >> $TMP_OUTPUT
  167. echo "[`date`] Forced sync is enabled."
  168. # CHK 4 - if number of warnings has exceeded threshold
  169. SYNC_WARN_COUNT=$(sed 'q;/^[0-9][0-9]*$/!d' $SYNC_WARN_FILE 2>/dev/null)
  170. SYNC_WARN_COUNT=${SYNC_WARN_COUNT:-0} #value is zero if file does not exist or does not contain what we are expecting
  171. if [ $SYNC_WARN_COUNT -ge $SYNC_WARN_THRESHOLD ]; then
  172. # CHK 5 - YES, lets force a sync job. Do not need to remove warning marker here as it is automatically removed when the sync job is run by this script
  173. echo "Number of warning(s) ($SYNC_WARN_COUNT) has reached/exceeded threshold ($SYNC_WARN_THRESHOLD). Forcing a sync job to run." >> $TMP_OUTPUT
  174. echo "[`date`] Number of warning(s) ($SYNC_WARN_COUNT) has reached/exceeded threshold ($SYNC_WARN_THRESHOLD). Forcing a sync job to run."
  175. DO_SYNC=1
  176. else
  177. # CHK 4 - NO, so let's increment the warning count and skip the sync job
  178. ((SYNC_WARN_COUNT += 1))
  179. echo $SYNC_WARN_COUNT > $SYNC_WARN_FILE
  180. echo "$((SYNC_WARN_THRESHOLD - SYNC_WARN_COUNT)) warning(s) till forced sync. NOT proceeding with sync job." >> $TMP_OUTPUT
  181. echo "[`date`] $((SYNC_WARN_THRESHOLD - SYNC_WARN_COUNT)) warning(s) till forced sync. NOT proceeding with sync job."
  182. DO_SYNC=0
  183. fi
  184. else
  185. # CHK 3 - NO, so let's skip SYNC
  186. echo "Forced sync is not enabled. NOT proceeding with sync job. Please run sync manually if this is not an error condition." >> $TMP_OUTPUT
  187. echo "[`date`] Forced sync is not enabled. Check $TMP_OUTPUT for details. NOT proceeding with sync job."
  188. DO_SYNC=0
  189. fi
  190. fi
  191. else
  192. # CHK 1 - NO, so let's skip SYNC
  193. echo "[`date`] No change detected. Not running SYNC job."
  194. DO_SYNC=0
  195. fi
  196. # Now run sync if conditions are met
  197. if [ $DO_SYNC -eq 1 ]; then
  198. echo "SnapRAID SYNC Job started on `date`" >> $TMP_OUTPUT
  199. echo "----------------------------------------" >> $TMP_OUTPUT
  200. $SNAPRAID_BIN sync | sed -e 's/\r/\n/g' >> $TMP_OUTPUT
  201. #wait for the job to finish
  202. wait
  203. echo "----------------------------------------" >> $TMP_OUTPUT
  204. echo "SnapRAID SYNC Job finished on `date`" >> $TMP_OUTPUT
  205. JOBS_DONE="$JOBS_DONE + SYNC"
  206. # insert SYNC marker to 'Everything OK' or 'Nothing to do' string to differentiate it from SCRUB job later
  207. sed -i 's/^Everything OK/SYNC_JOB--Everything OK/g;s/^Nothing to do/SYNC_JOB--Nothing to do/g' $TMP_OUTPUT
  208. # Remove any warning flags if set previously. This is done in this step to take care of scenarios when user has manually synced or restored deleted files and we will have missed it in the checks above.
  209. if [ -e $SYNC_WARN_FILE ]; then
  210. rm $SYNC_WARN_FILE
  211. fi
  212. $SNAPRAID_BIN scrub -p new
  213. fi
  214. # Moving onto scrub now. Check if user has enabled scrub
  215. if [ $SCRUB_PERCENT -gt 0 ]; then
  216. # YES, first let's check if delete threshold has been breached and we have not forced a sync.
  217. if [ $DEL_COUNT -gt $DEL_THRESHOLD -a $DO_SYNC -eq 0 ]; then
  218. # YES, parity is out of sync so let's not run scrub job
  219. echo "[`date`] Scrub job cancelled as parity info is out of sync (deleted files threshold has been breached)."
  220. else
  221. # NO, delete threshold has not been breached OR we forced a sync, but we have one last test -
  222. # let's make sure if sync ran, it completed successfully (by checking for our marker text "SYNC_JOB--" in the output).
  223. if [ $DO_SYNC -eq 1 -a -z "$(grep -w "SYNC_JOB-" $TMP_OUTPUT)" ]; then
  224. # Sync ran but did not complete successfully so lets not run scrub to be safe
  225. echo "[`date`] WARNING - check output of SYNC job. Could not detect marker <SYNC_JOB-->. Not proceeding with SCRUB job."
  226. echo "WARNING - check output of SYNC job. Could not detect marker <SYNC_JOB-->. Not proceeding with SCRUB job." >> $TMP_OUTPUT
  227. else
  228. # Everything ok - let's run the scrub job!
  229. echo "[`date`] Running SCRUB Command."
  230. echo "SnapRAID SCRUB Job started on `date`" >> $TMP_OUTPUT
  231. echo "----------------------------------------" >> $TMP_OUTPUT
  232. $SNAPRAID_BIN scrub -p $SCRUB_PERCENT -o $SCRUB_AGE | sed -e 's/\r/\n/g' >> $TMP_OUTPUT
  233. #wait for the job to finish
  234. wait
  235. echo "----------------------------------------" >> $TMP_OUTPUT
  236. echo "SnapRAID SCRUB Job finished on `date`" >> $TMP_OUTPUT
  237. JOBS_DONE="$JOBS_DONE + SCRUB"
  238. # insert SCRUB marker to 'Everything OK' or 'Nothing to do' string to differentiate it from SYNC job above
  239. sed -i 's/^Everything OK/SCRUB_JOB--Everything OK/g;s/^Nothing to do/SCRUB_JOB--Nothing to do/g' $TMP_OUTPUT
  240. fi
  241. fi
  242. else
  243. echo "[`date`] Scrub job is not scheduled. Not running SCRUB job."
  244. fi
  245. # Moving onto logging SMART info if enabled
  246. if [ $SMART_LOG -eq 1 ]; then
  247. $SNAPRAID_BIN smart >> $TMP_OUTPUT
  248. wait
  249. fi
  250. # Commenting out since it could potentially cause drives to fall out
  251. #echo "Spinning down disks..." >> $TMP_OUTPUT
  252. #$SNAPRAID_BIN down
  253. # check if deleted count exceeded threshold
  254. if [ $DEL_COUNT -gt $DEL_THRESHOLD -a $DO_SYNC -eq 0 ]; then
  255. # YES, lets inform user with an appropriate subject line
  256. #$MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX WARNING $SYNC_WARN_COUNT - Number of deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD)" "$EMAIL_ADDRESS" < $TMP_OUTPUT
  257. curl \
  258. -H "Title: $EMAIL_SUBJECT_PREFIX WARNING" \
  259. -H "Priority: urgent" \
  260. -H "Tags: warning" \
  261. -d "$SYNC_WARN_COUNT - Number of deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD)
  262. $(cat $TMP_OUTPUT)" \
  263. https://ntfy.warchildstory.com/{{ server_notifications_topic }}
  264. elif [ -z "${JOBS_DONE##*"SYNC"*}" -a -z "$(grep -w "SYNC_JOB-" $TMP_OUTPUT)" ]; then
  265. # Sync ran but did not complete successfully so lets warn the user
  266. #$MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX WARNING - SYNC job ran but did not complete successfully" "$EMAIL_ADDRESS" < $TMP_OUTPUT
  267. curl \
  268. -H "Title: $EMAIL_SUBJECT_PREFIX WARNING" \
  269. -H "Priority: urgent" \
  270. -H "Tags: warning" \
  271. -d "SYNC job ran but did not complete successfully
  272. $(cat $TMP_OUTPUT)" \
  273. https://ntfy.warchildstory.com/{{ server_notifications_topic }}
  274. elif [ -z "${JOBS_DONE##*"SCRUB"*}" -a -z "$(grep -w "SCRUB_JOB-" $TMP_OUTPUT)" ]; then
  275. # Scrub ran but did not complete successfully so lets warn the user
  276. #$MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX WARNING - SCRUB job ran but did not complete successfully" "$EMAIL_ADDRESS" < $TMP_OUTPUT
  277. curl \
  278. -H "Title: $EMAIL_SUBJECT_PREFIX WARNING" \
  279. -H "Priority: urgent" \
  280. -H "Tags: warning" \
  281. -d "SCRUB job ran but did not complete successfully
  282. $(cat $TMP_OUTPUT)" \
  283. https://ntfy.warchildstory.com/{{ server_notifications_topic }}
  284. else
  285. # OPTIONALLY, let's reduce the amount of status lines in output.
  286. if [ $TERSE -gt 1 ]; then
  287. # Report progress in interval of tens %
  288. sed -i '$!N; /^\([0-9]\).*\n\1.*$/!P; D' $TMP_OUTPUT
  289. sed -i '/^[1-8]%.*$/d' $TMP_OUTPUT
  290. elif [ $TERSE -gt 0 ]; then
  291. # Report progress in interval of ones %
  292. sed -i '$!N; /^\([0-9]*\)%.*\n\1.*$/!P; D' $TMP_OUTPUT
  293. fi
  294. #$MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX INFO - $JOBS_DONE Jobs COMPLETED" "$EMAIL_ADDRESS" < $TMP_OUTPUT
  295. curl \
  296. -H "Title: $EMAIL_SUBJECT_PREFIX INFO" \
  297. -H "Priority: default" \
  298. -H "Tags: white_check_mark" \
  299. -d "$JOBS_DONE Jobs COMPLETED
  300. $(cat $TMP_OUTPUT)" \
  301. https://ntfy.warchildstory.com/{{ server_notifications_topic }}
  302. fi
  303. echo "[`date`] All jobs ended."
  304. exit 0;