Skip to content

Commit 9ec2409

Browse files
authored
use snapraid status to check if previous sync was completed
"snapraid status" initial implementation to verify if the previous sync completed correctly before proceeding, otherwise halt the script.
1 parent 7063fec commit 9ec2409

File tree

1 file changed

+65
-15
lines changed

1 file changed

+65
-15
lines changed

snapraid-aio-script.sh

Lines changed: 65 additions & 15 deletions
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/')"
@@ -137,26 +137,54 @@ function main(){
137137
if [ ! -f "$SNAPRAID_CONF" ]; then
138138
# if running on OMV7, try to find the SnapRAID conf file automatically
139139
check_omv_version
140-
if [ "$OMV_VERSION" -ge 7 ]; then
141-
pick_snapraid_conf_file
142-
else
143-
echo "SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file "$SNAPRAID_CONF" does not exist."
144-
mklog "WARN: SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file "$SNAPRAID_CONF" does not exist."
145-
SUBJECT="[WARNING] - SnapRAID configuration file not found!"
146-
FORMATTED_CONF="\`$SNAPRAID_CONF\`"
147-
NOTIFY_OUTPUT="$SUBJECT The script cannot be run! Please check your settings, because the specified file $FORMATTED_CONF does not exist."
148-
notify_warning
149-
if [ "$EMAIL_ADDRESS" ]; then
150-
trim_log < "$TMP_OUTPUT" | send_mail
151-
fi
152-
exit 1;
153-
fi
140+
if [ "$OMV_VERSION" -ge 7 ]; then
141+
pick_snapraid_conf_file
142+
else
143+
echo "SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file "$SNAPRAID_CONF" does not exist."
144+
mklog "WARN: SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file "$SNAPRAID_CONF" does not exist."
145+
SUBJECT="[WARNING] - SnapRAID configuration file not found!"
146+
FORMATTED_CONF="\`$SNAPRAID_CONF\`"
147+
NOTIFY_OUTPUT="$SUBJECT The script cannot be run! Please check your settings, because the specified file $FORMATTED_CONF does not exist."
148+
notify_warning
149+
if [ "$EMAIL_ADDRESS" ]; then
150+
trim_log < "$TMP_OUTPUT" | send_mail
151+
fi
152+
exit 1;
153+
fi
154154
fi
155155

156156

157157
# sanity check first to make sure we can access the content and parity files
158158
mklog "INFO: Checking SnapRAID disks"
159159
sanity_check
160+
161+
# Check if previous sync was completed before running a new sync
162+
# If the status is ok (exit code 0) the script will proceed, otherwise will stop
163+
164+
mklog "INFO: Checking SnapRAID Status"
165+
check_snapraid_status
166+
if [ $SNAPRAID_STATUS -eq 1 ]; then
167+
# Stop the script due to warning
168+
echo "Stopping the script because the previous SnapRAID sync did not complete correctly."
169+
SUBJECT="[WARNING] - Previous SnapRAID sync did not complete correctly."
170+
NOTIFY_OUTPUT="$SUBJECT"
171+
notify_warning
172+
if [ "$EMAIL_ADDRESS" ]; then
173+
trim_log < "$TMP_OUTPUT" | send_mail
174+
fi
175+
exit 1;
176+
177+
elif [ $SNAPRAID_STATUS -eq 2 ]; then
178+
# Handle unknown status
179+
echo "Stopping the script due to unknown SnapRAID status. Please run 'snapraid status' on your host for more information."
180+
SUBJECT="[WARNING] - SnapRAID unknown status"
181+
NOTIFY_OUTPUT="$SUBJECT"
182+
notify_warning
183+
if [ "$EMAIL_ADDRESS" ]; then
184+
trim_log < "$TMP_OUTPUT" | send_mail
185+
fi
186+
exit 1;
187+
fi
160188

161189
# pause configured containers
162190
if [ "$MANAGE_SERVICES" -eq 1 ]; then
@@ -1106,6 +1134,28 @@ search_conf_files() {
11061134
fi
11071135
}
11081136

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

0 commit comments

Comments
 (0)