Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement scrub frequency in days #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 43 additions & 31 deletions usr/sbin/omv-snapraid-diff
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,16 @@ else
fi

### Section to support SnapRAID Scrub ###
# if array synced
IS_SYNCED=0
mkdir -p $TMP_SCRUB_DIR

if [ -f $TMP_SCRUB_DIR/snapraid_scrub_counter ]; then
COUNTER=$(cat $TMP_SCRUB_DIR/snapraid_scrub_counter)
if [ -f $TMP_SCRUB_DIR/snapraid_last_scrub_date ]; then
LAST_SCRUB_DATE=$(cat $TMP_SCRUB_DIR/snapraid_last_scrub_date)
else
echo "1" > $TMP_SCRUB_DIR/snapraid_scrub_counter
COUNTER=1
echo "1" > $TMP_SCRUB_DIR/snapraid_last_scrub_date
LAST_SCRUB_DATE=1
fi
# enable only one of the two below!!!
let COUNTER++
# COUNTER=$(($COUNTER+0))

# define Variables, if not set in snapraid-diff
if [ -z ${SCRUB_PERCENT} ]; then
Expand Down Expand Up @@ -440,7 +439,7 @@ if [ $DEL_COUNT -gt 0 ] || [ $ADD_COUNT -gt 0 ] || [ $MOVE_COUNT -gt 0 ] || [ $C

# Proceed with sync if no check errors
if [ $UPD_DEL_CHECK -eq 0 ]; then
# start snapraid SYNC
# start snapraid SYNC
_log "INFO: "
_log "INFO: SnapRAID SYNC Job started"
_log "INFO: ----------------------------------------"
Expand All @@ -453,17 +452,42 @@ if [ $DEL_COUNT -gt 0 ] || [ $ADD_COUNT -gt 0 ] || [ $MOVE_COUNT -gt 0 ] || [ $C
#wait for the job to finish
wait

# We have synced array here
IS_SYNCED=1
_log "INFO: ----------------------------------------"
_log "INFO: SnapRAID SYNC Job finished"
echo "SnapRAID SYNC Job finished - $(date)" >> $TMP_OUTPUT
echo "----------------------------------------" >> $TMP_OUTPUT

# If update threshold exceeded
elif [ $UPD_DEL_CHECK -eq 1 ]; then
_send_email "$EMAIL_SUBJECT_PREFIX - WARNING - Number of updated files (${UPDATE_COUNT}) exceeded threshold (${UPD_THRESHOLD})" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"

# If delete threshold exceeded
elif [ $UPD_DEL_CHECK -eq 2 ]; then
_send_email "$EMAIL_SUBJECT_PREFIX - WARNING - Number of deleted files (${DEL_COUNT}) exceeded threshold (${DEL_THRESHOLD})" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"

# IF both thresholds exceeded (UPD_DEL_CHECK -eq 3)
else
_send_email "$EMAIL_SUBJECT_PREFIX - WARNING - Number of updated (${UPDATE_COUNT}) and deleted (${DEL_COUNT}) files exceeded updated (${UPD_THRESHOLD}) and deleted (${DEL_THRESHOLD}) thresholds" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
fi
else
# No file changed indicates synced array
IS_SYNCED=1
# NO, so lets log it and exit
_log "INFO: No change detected."
fi

# Not hit threashold
if [ $UPD_DEL_CHECK -eq 0 ]; then
# Array synced
if [ $IS_SYNCED -eq 1 ]; then
# start snapraid SCRUB
if [ $RUN_SCRUB = "true" ]; then
if [ $COUNTER -lt $SCRUB_FREQUENCY_IN_DAYS ]; then
_log "INFO: SnapRAID SCRUB-Cycle count ($SCRUB_FREQUENCY_IN_DAYS) not met ($COUNTER). No scrub was run."
echo "SnapRAID SCRUB-Cycle count ($SCRUB_FREQUENCY_IN_DAYS) not met ($COUNTER). No scrub was run. - $(date)" >> $TMP_OUTPUT
echo "$COUNTER" > $TMP_SCRUB_DIR/snapraid_scrub_counter
let DAYS_SINCE_LAST_SCRUB=($(date +%s)-$LAST_SCRUB_DATE)/86400
if [ $DAYS_SINCE_LAST_SCRUB -lt $SCRUB_FREQUENCY_IN_DAYS ]; then
_log "INFO: SnapRAID SCRUB-Cycle count ($SCRUB_FREQUENCY_IN_DAYS) not met ($DAYS_SINCE_LAST_SCRUB). No scrub was run."
echo "SnapRAID SCRUB-Cycle count ($SCRUB_FREQUENCY_IN_DAYS) not met ($DAYS_SINCE_LAST_SCRUB). No scrub was run. - $(date)" >> $TMP_OUTPUT
else
_log "INFO: SnapRAID SCRUB Job started"
_log "INFO: ----------------------------------------"
Expand All @@ -474,38 +498,26 @@ if [ $DEL_COUNT -gt 0 ] || [ $ADD_COUNT -gt 0 ] || [ $MOVE_COUNT -gt 0 ] || [ $C
# wait for the job to finish
wait

# Reset counter
echo "1" > $TMP_SCRUB_DIR/snapraid_scrub_counter
# Set last scrub time
echo $(date -d "00:00:00" +%s) > $TMP_SCRUB_DIR/snapraid_last_scrub_date

_log "INFO: ----------------------------------------"
_log "INFO: SnapRAID SCRUB Job finished"
echo "SnapRAID SCRUB Job finished - $(date)" >> $TMP_OUTPUT
echo "----------------------------------------" >> $TMP_OUTPUT
fi
else
echo "Array scrubbing is not enabled. If you would like to enable it, please set the RUN_SCRUB option to true." >> $TMP_OUTPUT
_log "INFO: Array scrubbing is not enabled. If you would like to enable it, please set the RUN_SCRUB option to true."
echo "Array scrubbing is not enabled. If you would like to enable it, please set the RUN_SCRUB option to true." >> $TMP_OUTPUT
_log "INFO: Array scrubbing is not enabled. If you would like to enable it, please set the RUN_SCRUB option to true."
fi

# Send success email
_send_email "$EMAIL_SUBJECT_PREFIX - Sync/Scrub Job COMPLETED" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"

# If update threshold exceeded
elif [ $UPD_DEL_CHECK -eq 1 ]; then
_send_email "$EMAIL_SUBJECT_PREFIX - WARNING - Number of updated files (${UPDATE_COUNT}) exceeded threshold (${UPD_THRESHOLD})" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"

# If delete threshold exceeded
elif [ $UPD_DEL_CHECK -eq 2 ]; then
_send_email "$EMAIL_SUBJECT_PREFIX - WARNING - Number of deleted files (${DEL_COUNT}) exceeded threshold (${DEL_THRESHOLD})" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"

# IF both thresholds exceeded (UPD_DEL_CHECK -eq 3)
_check_success && _rmtmp
else
_send_email "$EMAIL_SUBJECT_PREFIX - WARNING - Number of updated (${UPDATE_COUNT}) and deleted (${DEL_COUNT}) files exceeded updated (${UPD_THRESHOLD}) and deleted (${DEL_THRESHOLD}) thresholds" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
_log "INFO: SnapRAID array not synced, won't scrub array"
echo "SnapRAID array not synced, won't scrub array" >> $TMP_OUTPUT
fi
_check_success && _rmtmp
else
# NO, so lets log it and exit
_log "INFO: No change detected. Nothing to do"
fi

_log "INFO: SnapRAID Job ended."
Expand Down