Skip to content

Commit 31994a6

Browse files
authored
Merge pull request #119 from auanasgheps/status-check
use snapraid status to check if previous sync was completed
2 parents 5742343 + e5e3e8d commit 31994a6

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

snapraid-aio-script.sh

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
######################
99
# SCRIPT VARIABLES #
1010
######################
11-
SNAPSCRIPTVERSION="3.4" #DEV
11+
SNAPSCRIPTVERSION="3.4" #DEV2
1212

1313
# Read SnapRAID version
1414
SNAPRAIDVERSION="$(snapraid -V | sed -e 's/snapraid v\(.*\)by.*/\1/')"
@@ -147,6 +147,34 @@ function main(){
147147
# sanity check first to make sure we can access the content and parity files
148148
mklog "INFO: Checking SnapRAID disks"
149149
sanity_check
150+
151+
# Check if previous sync was completed before running a new sync
152+
# If the status is ok (exit code 0) the script will proceed, otherwise will stop
153+
154+
mklog "INFO: Checking SnapRAID Status"
155+
check_snapraid_status
156+
if [ $SNAPRAID_STATUS -eq 1 ]; then
157+
# Stop the script due to warning
158+
echo "Stopping the script because the previous SnapRAID sync did not complete correctly."
159+
SUBJECT="[WARNING] - Previous SnapRAID sync did not complete correctly."
160+
NOTIFY_OUTPUT="$SUBJECT"
161+
notify_warning
162+
if [ "$EMAIL_ADDRESS" ]; then
163+
trim_log < "$TMP_OUTPUT" | send_mail
164+
fi
165+
exit 1;
166+
167+
elif [ $SNAPRAID_STATUS -eq 2 ]; then
168+
# Handle unknown status
169+
echo "Stopping the script due to unknown SnapRAID status. Please run 'snapraid status' on your host for more information."
170+
SUBJECT="[WARNING] - SnapRAID unknown status"
171+
NOTIFY_OUTPUT="$SUBJECT"
172+
notify_warning
173+
if [ "$EMAIL_ADDRESS" ]; then
174+
trim_log < "$TMP_OUTPUT" | send_mail
175+
fi
176+
exit 1;
177+
fi
150178

151179
# pause configured containers
152180
if [ "$MANAGE_SERVICES" -eq 1 ]; then
@@ -1101,6 +1129,28 @@ search_conf_files() {
11011129
fi
11021130
}
11031131

1132+
# Run SnapRAID status to check for the previous sync
1133+
check_snapraid_status() {
1134+
# Run snapraid status command and capture the output
1135+
local snapraid_status_output=$($SNAPRAID_BIN status)
1136+
1137+
# Check for the "No sync is in progress" message
1138+
if echo "$snapraid_status_output" | grep -q "No sync is in progress"; then
1139+
echo "Previous sync completed successfully, proceeding."
1140+
mklog "Previous sync completed successfully, proceeding."
1141+
SNAPRAID_STATUS=0
1142+
1143+
# Check for the "NOT fully synced" warning message
1144+
elif echo "$snapraid_status_output" | grep -q "WARNING! The array is NOT fully synced."; then
1145+
mklog "Warning: The array is NOT fully synced. Stopping the script."
1146+
SNAPRAID_STATUS=1
1147+
else
1148+
# If neither message is found, handle the unknown state
1149+
mklog "Warning: The array status is unknown. Stopping the script."
1150+
SNAPRAID_STATUS=2
1151+
fi
1152+
}
1153+
11041154
# Set TRAP
11051155
trap final_cleanup INT EXIT
11061156

0 commit comments

Comments
 (0)