Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #######################################################################
- # this is a helper script that keeps snapraid parity info in sync with
- # your data. Here's how it works:
- # 1) it first calls diff to figure out if the parity info is out of sync
- # 2) if there are changed files (i.e. new, changed, moved or removed),
- # it then checks how many files were removed.
- # 3) if the deleted files exceed X (configurable), it triggers an
- # alert email and stops. (in case of accidental deletions)
- # 4) otherwise, it will call sync.
- # 5) when sync finishes, it sends an email with the output to user.
- #
- # $Author: sidney
- # $Revision: 5
- # $Date: 2013-09-16
- # $HeadURL: file:///svnrepo/linuxScripts/snapraid_diff_n_sync.sh $
- #
- # changed for OMV from: Solo0815
- #
- # ToDo:
- # - create HTML-Output
- # - switch to automatically do a sync after X days if nothing happens
- #
- #######################################################################
- #######################################################################
- # Mail settings:
- # get basic info
- . /etc/default/openmediavault
- # needed for omv_config_get command
- . /usr/share/openmediavault/scripts/helper-functions
- # get OMV-Mail
- # OMV_MAIL_server=$(omv_config_get "//system/email/server")
- # echo "OMV_MAIL_server $OMV_MAIL_server"
- # OMV_MAIL_port=$(omv_config_get "//system/email/port")
- # echo "OMV_MAIL_port $OMV_MAIL_port"
- # OMV_MAIL_tls=$(omv_config_get "//system/email/tls")
- # echo "OMV_MAIL_tls $OMV_MAIL_tls"
- # OMV_MAIL_sender=$(omv_config_get "//system/email/sender")
- # echo "OMV_MAIL_sender $OMV_MAIL_sender"
- #
- # #authentication required
- # OMV_MAIL_authrequired=$(omv_config_get "//system/email/authentication/enable")
- # echo "OMV_MAIL_authrequired $OMV_MAIL_authrequired"
- # OMV_MAIL_username=$(omv_config_get "//system/email/authentication/username")
- # echo "OMV_MAIL_username $OMV_MAIL_username"
- # OMV_MAIL_password=$(omv_config_get "//system/email/authentication/password")
- # echo "OMV_MAIL_password: $OMV_MAIL_password"
- #primary and secondary recipients
- OMV_MAIL_primarymail=$(omv_config_get "//system/email/primaryemail")
- #echo "OMV_MAIL_primarymail $OMV_MAIL_primarymail"
- OMV_MAIL_secondarymail=$(omv_config_get "//system/email/secondaryemail")
- #echo "OMV_MAIL_secondarymail $OMV_MAIL_secondarymail"
- # structure in /etc/openmediavault/config.xml
- # <email>
- # <enable>1</enable>
- # <server>mail.ryecoinc.com</server>
- # <port>25</port>
- # <tls>0</tls>
- # <sender>server@ryecoinc.com</sender>
- # <authentication>
- # <enable>0</enable>
- # <username></username>
- # <password></password>
- # </authentication>
- # <primaryemail>aaron@ryecoinc.net</primaryemail>
- # <secondaryemail></secondaryemail>
- # </email>
- #EMAIL_SUBJECT_PREFIX="[${OMV_MAIL_sender}] SnapRAID"
- #######################################################################
- ################################################################
- #
- # name: _send_email
- # parameter : # $1 = Betreff
- # $2 = Datei mit Mailtext
- # Optional:
- # $3 = Von wem wurde die Nachricht gesendet (Voller Name), z.B. "Server Status-Report"
- # $4 = Antwort an Mail-Adresse
- # return: 0 = Alles OK
- # 1 = Fehler passiert
- #
- _send_email(){
- ## the below does not work with the internal OMV-Settings
- # mail -s "$1" -a 'Content-Type: text/plain; charset=utf-8' $MAIL -- -F "$3" -f "$4" < $2
- #_log "INFO: Mail should be sent here - to be fixed"
- #echo "Mail should be sent here - to be fixed"
- #mail [-a header] [-b bcc-addr] [-c cc-addr] [-s subject] to-addr
- mail -c "$OMV_MAIL_secondarymail" -s "$1" "$OMV_MAIL_primarymail" < $2
- if [ $? -eq 0 ]; then
- _log "INFO: Status Report an $MAIL gesendet"
- return 0
- else
- _log "WARN: Mail konnte nicht gesendet werden. Es ist ein Fehler aufgetreten. Siehe /var/log/mail.log"
- return 1
- fi
- }
- ################################################################
- #
- # name : _log
- # parameter : $LOGMESSAGE : logmessage in format "PRIORITY: MESSAGE"
- # return : none
- #
- _log()
- {(
- [[ "$*" =~ ^([A-Za-z]*):(.*) ]] &&
- {
- PRIORITY=${BASH_REMATCH[1]}
- LOGMESSAGE=${BASH_REMATCH[2]}
- [[ "$(basename "$0")" =~ ^(.*)\. ]] && LOGMESSAGE="${BASH_REMATCH[1]}[$$]: $PRIORITY: '$LOGMESSAGE'";
- }
- if $VERBOSE ; then
- # next line only with implementation where logger does not support option '-s'
- # echo "$(date '+%b %e %H:%M:%S'):$LOGMESSAGE"
- [ $SYSLOG ] && $LOGGER -s -t "$(date '+%b %e %H:%M:%S'): $USER" -p $FACILITY.$PRIORITY "$LOGMESSAGE"
- else
- [ $SYSLOG ] && $LOGGER -p $FACILITY.$PRIORITY "$LOGMESSAGE"
- fi # > if [ "$VERBOSE" = "NO" ]; then
- )}
- ################################################################
- ##### MAIN BODY of the script
- ################################################################
- SCRIPTVERSION="0.5"
- # Set path to snapraid.conf
- SNAPRAIDCONF="/etc/snapraid.conf"
- # Define threshold of files deleted to start the sync-process
- # default = 0 to make sure no sync process is started while testing
- DEL_THRESHOLD=30
- # read CONTENT and PARITY from $SNAPRAIDCONF
- CONTENT_FILE="$(egrep '^content /var/' $SNAPRAIDCONF | awk '{print $2}')"
- PARITY_FILE="$(egrep '^parity /' $SNAPRAIDCONF | awk '{print $2}')"
- ## INTERNAL TEMP VARS ##
- TMP_OUTPUT="/tmp/snapraid.out"
- # ADD DSpevak
- # temp file for script runs counter
- COUNTERFILE="/tmp/snapraid_counter"
- # ADD DSpevak END
- SNAPRAIDBIN="$(which snapraid)"
- if [ -z $SNAPRAIDBIN ]; then
- _log "WARN: snapraid executable not found!"
- fi
- if [ ! -x $SNAPRAIDBIN ]; then
- _log "WARN: snapraid not executable! Please do a 'chmod +x snapraid'"
- fi
- SNAPRAIDVERSION="$(${SNAPRAIDBIN} -V)"
- ## Functions
- # Logging Settings
- SYSLOG="true" # activate write to syslog (default="true")
- VERBOSE="false" # use the verbose mode, with additional info on the command line (default="true")
- LOGGER="/usr/bin/logger" # path and name of logger (default="/usr/bin/logger")
- FACILITY="local5" # facility to log to -> see syslog.conf and add the line (default="local6")
- # "local6.* %/var/log/autoshutdown.log"
- DEBUG="false"
- if $DEBUG; then
- _log "DEBUG: CONTENT_FILE: $CONTENT_FILE"
- _log "DEBUG: PARITY_FILE: $PARITY_FILE"
- fi
- if [ -f $TMP_OUTPUT ]; then
- rm $TMP_OUTPUT && _log "INFO: '$TMP_OUTPUT' deleted - creating a new one"
- fi
- #sanity check first to make sure we can access the content and parity files
- if [ ! -e $CONTENT_FILE ]; then
- _log "WARN: Content file ($CONTENT_FILE) not found!"
- exit 1
- fi
- if [ ! -e $PARITY_FILE ]; then
- _log "WARN: Parity file ($PARITY_FILE) not found!"
- exit 1
- fi
- ## Start of the script - Start
- _log "INFO: ----------------------------------------"
- _log "INFO: SnapRAID Job started - Script version: $SCRIPTVERSION"
- echo "SnapRAID Job started - $(date) - Script version: $VERSION" >> $TMP_OUTPUT
- # run the snapraid DIFF command
- _log "INFO: SnapRAID DIFF started"
- echo "SnapRAID DIFF started - $(date)" >> $TMP_OUTPUT
- $SNAPRAIDBIN diff >> $TMP_OUTPUT
- # wait for the above cmd to finish
- wait
- _log "INFO: SnapRAID DIFF finished"
- _log "INFO: ----------------------------------------"
- echo "SnapRAID DIFF finished - $(date)" >> $TMP_OUTPUT
- echo "----------------------------------------" >> $TMP_OUTPUT
- DEL_COUNT=$(egrep '^[Rr]emove' $TMP_OUTPUT | wc -l - | cut -d' ' -f1)
- ADD_COUNT=$(egrep '^[Aa]dd' $TMP_OUTPUT | wc -l - | cut -d' ' -f1)
- MOVE_COUNT=$(egrep '^[Mm]ove' $TMP_OUTPUT | wc -l - | cut -d' ' -f1)
- UPDATE_COUNT=$(egrep '^[Uu]pdate' $TMP_OUTPUT | wc -l - | cut -d' ' -f1)
- _log "INFO: SUMMARY of changes since last sync - Added: [$ADD_COUNT] - Deleted: [$DEL_COUNT] - Moved: [$MOVE_COUNT] - Updated: [$UPDATE_COUNT]"
- # check if files have changed
- if [ $DEL_COUNT -gt 0 -o $ADD_COUNT -gt 0 -o $MOVE_COUNT -gt 0 -o $UPDATE_COUNT -gt 0 ]; then
- # YES, check if number of deleted files exceed DEL_THRESHOLD
- if [ $DEL_COUNT -gt $DEL_THRESHOLD ]; then
- # YES, lets inform user and not proceed with the sync just in case
- _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."
- 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
- _send_email "SnapRAID - WARNING - Number of deleted files (${DEL_COUNT}) exceeded threshold (${DEL_THRESHOLD})" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
- else
- # NO, delete threshold not reached, lets run the sync job
- _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."
- 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
- _log "INFO: SnapRAID SYNC Job started"
- _log "INFO: ----------------------------------------"
- echo "SnapRAID SYNC Job started - $(date)" >> $TMP_OUTPUT
- echo "----------------------------------------" >> $TMP_OUTPUT
- $SNAPRAIDBIN sync >> $TMP_OUTPUT
- #wait for the job to finish
- wait
- _log "INFO: ----------------------------------------"
- _log "INFO: SnapRAID SYNC Job finished"
- echo "SnapRAID SYNC Job finished - $(date)" >> $TMP_OUTPUT
- echo "----------------------------------------" >> $TMP_OUTPUT
- #ADD DSpevak
- #Each 10 runs make a scrub
- if [ -e $COUNTERFILE ]
- then
- COUNTER=`cat $COUNTERFILE`
- else
- COUNTER=0
- fi
- let "COUNTER=($COUNTER+1) % 10"
- echo $COUNTER > $COUNTERFILE
- if [ "$COUNTER" -eq "0" ]
- then
- _log "INFO: SnapRAID SCRUB Job started"
- _log "INFO: ----------------------------------------"
- echo "SnapRAID SCRUB Job started - $(date)" >> $TMP_OUTPUT
- echo "----------------------------------------" >> $TMP_OUTPUT
- $SNAPRAIDBIN scrub >> $TMP_OUTPUT
- #wait for the job to finish
- wait
- _log "INFO: ----------------------------------------"
- _log "INFO: SnapRAID SCRUB Job finished"
- echo "SnapRAID SCRUB Job finished - $(date)" >> $TMP_OUTPUT
- echo "----------------------------------------" >> $TMP_OUTPUT
- fi
- #ADD Dspevak END
- _send_email "$EMAIL_SUBJECT_PREFIX - Sync Job COMPLETED" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
- fi
- else
- # NO, so lets log it and exit
- _log "INFO: No change detected. Nothing to do"
- fi
- _log "INFO: SnapRAID Job ended."
- # remove $TMP_OUTPUT
- rm $TMP_OUTPUT
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement