Ok, will try, please advise where can I find your script?
I thought you could merge
@eibgrad script posted by
@chongnt above with what you already have...
but I'll added a check for you that waits a maximum of 30sec for the USB-Drive to be mounted and if it fails it will give you a log message.
Code:
#!/bin/sh
############################################################
# required for serialization when reentry is possible
LOCK="/tmp/$(basename $0).lock"
acquire_lock() { while ! mkdir $LOCK &>/dev/null; do sleep 2; done; }
release_lock() { rmdir $LOCK &>/dev/null; }
exit_0() { release_lock; exit 0; } # exit (any concurrent instance(s) may now run)
############################################################
# one instance at a time
acquire_lock
logger -t $(basename $0) "Started [[email protected]]"
##
## Put existing nat-start directives here
##
## IPSET restore:
IPSET_LIST="unblockip"
MAX_TRIES=30 #Retries every second [MAX_TRIES] amount of times.
TRIES="0"
while [ "$TRIES" -lt "$MAX_TRIES" ]; do
if [ -d "/mnt/sda1/entware/home" ]; then
for IPSET_NAME in $IPSET_LIST; do
if [ "$(ipset list -n "$IPSET_NAME" 2>/dev/null)" != "$IPSET_NAME" ]; then #if ipset does not already exist
if [ -s "/mnt/sda1/entware/home/$IPSET_NAME" ]; then #if a backup file exists
ipset restore -! <"/mnt/sda1/entware/home/$IPSET_NAME" #restore ipset
cru a "$IPSET_NAME" "0 2 * * * ipset save $IPSET_NAME > /mnt/sda1/entware/home/$IPSET_NAME" >/dev/null 2>&1 # create cron job for autosave
logger -t $(basename $0) "IPSET restored: $IPSET_NAME"
fi
fi
done
break #all done
else
sleep 1
TRIES=$((TRIES + 1))
if [ "$TRIES" == "$MAX_TRIES" ]; then
logger -t $(basename $0) "Warning: Failed to detect mounted USB-Drive within $MAX_TRIES seconds! IPSET not restored!"
fi
fi
done
############################################################
logger -t $(basename $0) "Completed [[email protected]]"
exit_0
I have merged the code above and put in
@eibgrad code for serialization and added check for usb-drive. It gets a bit bigger and harder to understand/overview but hopefully now it will give a more predictable outcome.
I never tested the entire script, only different sections, so I hope there is no typo... if anyone finds any please let me know.
Perhaps a good idea to destroy your ipset and test run the script before to see that they get restored properly (and check so that there are no complaints from the shell)