Advertisement
Guest User

Untitled

a guest
Feb 10th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.28 KB | None | 0 0
  1. #!/bin/bash
  2. #######################################################################
  3. # this is a helper script that keeps snapraid parity info in sync with
  4. # your data. Here's how it works:
  5. #   1) it first calls diff to figure out if the parity info is out of sync
  6. #   2) if there are changed files (i.e. new, changed, moved or removed),
  7. #         it then checks how many files were removed.
  8. #   3) if the deleted files exceed X (configurable), it triggers an
  9. #         alert email and stops. (in case of accidental deletions)
  10. #   4) otherwise, it will call sync.
  11. #   5) when sync finishes, it sends an email with the output to user.
  12. #
  13. # $Author: sidney
  14. # $Revision: 5
  15. # $Date: 2013-09-16
  16. # $HeadURL: file:///svnrepo/linuxScripts/snapraid_diff_n_sync.sh $
  17. #
  18. # changed for OMV from: Solo0815
  19. #
  20. # ToDo:
  21. # - create HTML-Output
  22. # - switch to automatically do a sync after X days if nothing happens
  23. #
  24. #######################################################################
  25.  
  26. #######################################################################
  27. # Mail settings:
  28. # get basic info
  29. . /etc/default/openmediavault
  30.  
  31. # needed for omv_config_get command
  32. . /usr/share/openmediavault/scripts/helper-functions
  33.  
  34. # get OMV-Mail
  35. # OMV_MAIL_server=$(omv_config_get "//system/email/server")
  36. # echo "OMV_MAIL_server $OMV_MAIL_server"
  37. # OMV_MAIL_port=$(omv_config_get "//system/email/port")
  38. # echo "OMV_MAIL_port $OMV_MAIL_port"
  39. # OMV_MAIL_tls=$(omv_config_get "//system/email/tls")
  40. # echo "OMV_MAIL_tls $OMV_MAIL_tls"
  41. # OMV_MAIL_sender=$(omv_config_get "//system/email/sender")
  42. # echo "OMV_MAIL_sender $OMV_MAIL_sender"
  43. #
  44. # #authentication required
  45. # OMV_MAIL_authrequired=$(omv_config_get "//system/email/authentication/enable")
  46. # echo "OMV_MAIL_authrequired $OMV_MAIL_authrequired"
  47. # OMV_MAIL_username=$(omv_config_get "//system/email/authentication/username")
  48. # echo "OMV_MAIL_username $OMV_MAIL_username"
  49. # OMV_MAIL_password=$(omv_config_get "//system/email/authentication/password")
  50. # echo "OMV_MAIL_password: $OMV_MAIL_password"
  51.  
  52. #primary and secondary recipients
  53. OMV_MAIL_primarymail=$(omv_config_get "//system/email/primaryemail")
  54. #echo "OMV_MAIL_primarymail $OMV_MAIL_primarymail"
  55. OMV_MAIL_secondarymail=$(omv_config_get "//system/email/secondaryemail")
  56. #echo "OMV_MAIL_secondarymail $OMV_MAIL_secondarymail"
  57.  
  58.  
  59. # structure in /etc/openmediavault/config.xml
  60.  
  61. #    <email>
  62. #      <enable>1</enable>
  63. #      <server>mail.ryecoinc.com</server>
  64. #      <port>25</port>
  65. #      <tls>0</tls>
  66. #      <sender>server@ryecoinc.com</sender>
  67. #      <authentication>
  68. #        <enable>0</enable>
  69. #        <username></username>
  70. #        <password></password>
  71. #      </authentication>
  72. #      <primaryemail>aaron@ryecoinc.net</primaryemail>
  73. #      <secondaryemail></secondaryemail>
  74. #    </email>
  75.  
  76. #EMAIL_SUBJECT_PREFIX="[${OMV_MAIL_sender}] SnapRAID"
  77.  
  78. #######################################################################
  79.  
  80. ################################################################
  81. #
  82. #   name:        _send_email
  83. #   parameter :         # $1 = Betreff
  84.                                         # $2 = Datei mit Mailtext
  85.                                         # Optional:
  86.                                         # $3 = Von wem wurde die Nachricht gesendet (Voller Name), z.B. "Server Status-Report"
  87.                                         # $4 = Antwort an Mail-Adresse
  88. #   return:         0 = Alles OK
  89. #                                1 = Fehler passiert
  90. #
  91. _send_email(){
  92.  
  93. ## the below does not work with the internal OMV-Settings
  94. #        mail -s "$1" -a 'Content-Type: text/plain; charset=utf-8' $MAIL -- -F "$3" -f "$4" < $2
  95.  
  96. #_log "INFO: Mail should be sent here - to be fixed"
  97. #echo "Mail should be sent here - to be fixed"
  98.  
  99. #mail [-a header] [-b bcc-addr] [-c cc-addr] [-s subject] to-addr
  100. mail -c "$OMV_MAIL_secondarymail" -s "$1" "$OMV_MAIL_primarymail" < $2
  101.  
  102.         if [ $? -eq 0 ]; then
  103.                 _log "INFO: Status Report an $MAIL gesendet"
  104.                 return 0
  105.         else
  106.                 _log "WARN: Mail konnte nicht gesendet werden. Es ist ein Fehler aufgetreten. Siehe /var/log/mail.log"
  107.                 return 1
  108.         fi
  109. }
  110.  
  111. ################################################################
  112. #
  113. #   name      : _log
  114. #   parameter   : $LOGMESSAGE : logmessage in format "PRIORITY: MESSAGE"
  115. #   return      : none
  116. #
  117. _log()
  118.    {(
  119.  
  120.        [[ "$*" =~ ^([A-Za-z]*):(.*) ]] &&
  121.          {
  122.             PRIORITY=${BASH_REMATCH[1]}
  123.             LOGMESSAGE=${BASH_REMATCH[2]}
  124.             [[ "$(basename "$0")" =~ ^(.*)\. ]] && LOGMESSAGE="${BASH_REMATCH[1]}[$$]: $PRIORITY: '$LOGMESSAGE'";
  125.          }
  126.  
  127.       if $VERBOSE ; then
  128.          # next line only with implementation where logger does not support option '-s'
  129.          # echo "$(date '+%b %e %H:%M:%S'):$LOGMESSAGE"
  130.  
  131.          [ $SYSLOG ] && $LOGGER -s -t "$(date '+%b %e %H:%M:%S'): $USER" -p $FACILITY.$PRIORITY "$LOGMESSAGE"
  132.  
  133.       else
  134.          [ $SYSLOG ] && $LOGGER -p $FACILITY.$PRIORITY "$LOGMESSAGE"
  135.  
  136.       fi   # > if [ "$VERBOSE" = "NO" ]; then
  137.  
  138.    )}
  139.  
  140.  
  141. ################################################################
  142. #####     MAIN BODY of the script
  143. ################################################################
  144.  
  145. SCRIPTVERSION="0.5"
  146.  
  147. # Set path to snapraid.conf
  148. SNAPRAIDCONF="/etc/snapraid.conf"
  149.  
  150. # Define threshold of files deleted to start the sync-process
  151. # default = 0 to make sure no sync process is started while testing
  152. DEL_THRESHOLD=30
  153.  
  154. # read CONTENT and PARITY from $SNAPRAIDCONF
  155. CONTENT_FILE="$(egrep '^content /var/' $SNAPRAIDCONF | awk '{print $2}')"
  156. PARITY_FILE="$(egrep '^parity /' $SNAPRAIDCONF | awk '{print $2}')"
  157.  
  158. ## INTERNAL TEMP VARS ##
  159. TMP_OUTPUT="/tmp/snapraid.out"
  160. # ADD DSpevak
  161. # temp file for script runs counter
  162. COUNTERFILE="/tmp/snapraid_counter"
  163. # ADD DSpevak END
  164. SNAPRAIDBIN="$(which snapraid)"
  165. if [ -z $SNAPRAIDBIN ]; then
  166.         _log "WARN: snapraid executable not found!"
  167. fi
  168. if [ ! -x $SNAPRAIDBIN ]; then
  169.         _log "WARN: snapraid not executable! Please do a 'chmod +x snapraid'"
  170. fi
  171.  
  172. SNAPRAIDVERSION="$(${SNAPRAIDBIN} -V)"
  173.  
  174. ## Functions
  175. # Logging Settings
  176. SYSLOG="true"            # activate write to syslog (default="true")
  177. VERBOSE="false"            # use the verbose mode, with additional info on the command line (default="true")
  178. LOGGER="/usr/bin/logger"   # path and name of logger (default="/usr/bin/logger")
  179. FACILITY="local5"         # facility to log to -> see syslog.conf  and add the line (default="local6")
  180.                                                 # "local6.* %/var/log/autoshutdown.log"
  181. DEBUG="false"
  182.  
  183. if $DEBUG; then
  184.         _log "DEBUG: CONTENT_FILE: $CONTENT_FILE"
  185.         _log "DEBUG: PARITY_FILE: $PARITY_FILE"
  186. fi
  187.  
  188. if [ -f $TMP_OUTPUT ]; then
  189.         rm $TMP_OUTPUT && _log "INFO: '$TMP_OUTPUT' deleted - creating a new one"
  190. fi
  191.  
  192. #sanity check first to make sure we can access the content and parity files
  193. if [ ! -e $CONTENT_FILE ]; then
  194.   _log "WARN: Content file ($CONTENT_FILE) not found!"
  195.   exit 1
  196. fi
  197.  
  198. if [ ! -e $PARITY_FILE ]; then
  199.   _log "WARN: Parity file ($PARITY_FILE) not found!"
  200.   exit 1
  201. fi
  202.  
  203. ## Start of the script - Start
  204.  
  205. _log "INFO: ----------------------------------------"
  206. _log "INFO: SnapRAID Job started - Script version: $SCRIPTVERSION"
  207. echo "SnapRAID Job started - $(date) - Script version: $VERSION" >> $TMP_OUTPUT
  208.  
  209. # run the snapraid DIFF command
  210. _log "INFO: SnapRAID DIFF started"
  211. echo "SnapRAID DIFF started - $(date)" >> $TMP_OUTPUT
  212. $SNAPRAIDBIN diff >> $TMP_OUTPUT
  213.  
  214. # wait for the above cmd to finish
  215. wait
  216.  
  217. _log "INFO: SnapRAID DIFF finished"
  218. _log "INFO: ----------------------------------------"
  219. echo "SnapRAID DIFF finished - $(date)" >> $TMP_OUTPUT
  220. echo "----------------------------------------" >> $TMP_OUTPUT
  221.  
  222. DEL_COUNT=$(egrep '^[Rr]emove' $TMP_OUTPUT | wc -l - | cut -d' ' -f1)
  223. ADD_COUNT=$(egrep '^[Aa]dd' $TMP_OUTPUT | wc -l - | cut -d' ' -f1)
  224. MOVE_COUNT=$(egrep '^[Mm]ove' $TMP_OUTPUT | wc -l - | cut -d' ' -f1)
  225. UPDATE_COUNT=$(egrep '^[Uu]pdate' $TMP_OUTPUT | wc -l - | cut -d' ' -f1)
  226.  
  227. _log "INFO: SUMMARY of changes since last sync - Added: [$ADD_COUNT] - Deleted: [$DEL_COUNT] - Moved: [$MOVE_COUNT] - Updated: [$UPDATE_COUNT]"
  228.  
  229. # check if files have changed
  230. if [ $DEL_COUNT -gt 0 -o $ADD_COUNT -gt 0 -o $MOVE_COUNT -gt 0 -o $UPDATE_COUNT -gt 0 ]; then
  231.     # YES, check if number of deleted files exceed DEL_THRESHOLD
  232.     if [ $DEL_COUNT -gt $DEL_THRESHOLD ]; then
  233.         # YES, lets inform user and not proceed with the sync just in case
  234.         _log "INFO: Number of deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD). NOT proceeding with sync job. Please run sync manually if this is not an error condition."
  235.                 echo "Number of deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD). NOT proceeding with sync job. Please run sync manually if this is not an error condition" >> $TMP_OUTPUT
  236.  
  237.                 _send_email "SnapRAID - WARNING - Number of deleted files (${DEL_COUNT}) exceeded threshold (${DEL_THRESHOLD})" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
  238.     else
  239.         # NO, delete threshold not reached, lets run the sync job
  240.         _log "INFO: Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,U-$UPDATE_COUNT] and deleted files ($DEL_COUNT) is below threshold ($DEL_THRESHOLD). Running SYNC Command."
  241.                 echo "Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,U-$UPDATE_COUNT] and deleted files ($DEL_COUNT) is below threshold ($DEL_THRESHOLD). Running SYNC Command" >> $TMP_OUTPUT
  242.  
  243.         _log "INFO: SnapRAID SYNC Job started"
  244.         _log "INFO: ----------------------------------------"
  245.                 echo "SnapRAID SYNC Job started - $(date)" >> $TMP_OUTPUT
  246.                 echo "----------------------------------------" >> $TMP_OUTPUT
  247.  
  248.         $SNAPRAIDBIN sync >> $TMP_OUTPUT
  249.         #wait for the job to finish
  250.         wait
  251.  
  252.         _log "INFO: ----------------------------------------"
  253.         _log "INFO: SnapRAID SYNC Job finished"
  254.                 echo "SnapRAID SYNC Job finished - $(date)" >> $TMP_OUTPUT
  255.                 echo "----------------------------------------" >> $TMP_OUTPUT
  256.  
  257.     #ADD DSpevak
  258.     #Each 10 runs make a scrub
  259.     if [ -e $COUNTERFILE ]
  260.     then
  261.         COUNTER=`cat $COUNTERFILE`
  262.     else
  263.         COUNTER=0
  264.     fi
  265.  
  266.     let "COUNTER=($COUNTER+1) % 10"
  267.  
  268.     echo $COUNTER > $COUNTERFILE
  269.     if [ "$COUNTER" -eq "0" ]
  270.         then
  271.                 _log "INFO: SnapRAID SCRUB Job started"
  272.                 _log "INFO: ----------------------------------------"
  273.                     echo "SnapRAID SCRUB Job started - $(date)" >> $TMP_OUTPUT
  274.                     echo "----------------------------------------" >> $TMP_OUTPUT
  275.  
  276.                 $SNAPRAIDBIN scrub >> $TMP_OUTPUT
  277.                 #wait for the job to finish
  278.                 wait
  279.  
  280.                 _log "INFO: ----------------------------------------"
  281.                 _log "INFO: SnapRAID SCRUB Job finished"
  282.                     echo "SnapRAID SCRUB Job finished - $(date)" >> $TMP_OUTPUT
  283.                     echo "----------------------------------------" >> $TMP_OUTPUT
  284.  
  285.     fi
  286.     #ADD Dspevak END
  287.         _send_email "$EMAIL_SUBJECT_PREFIX - Sync Job COMPLETED" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
  288.     fi
  289. else
  290.     # NO, so lets log it and exit
  291.     _log "INFO: No change detected. Nothing to do"
  292. fi
  293.  
  294. _log "INFO: SnapRAID Job ended."
  295. # remove $TMP_OUTPUT
  296. rm $TMP_OUTPUT
  297.  
  298. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement