Files
emulator-sync/emulator-sync-scripts.sh

104 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
VERS='1.1.14-05262025'
source /opt/idssys/defaults/colors.inc
source /opt/idssys/defaults/default.inc
source /opt/idssys/emulator-sync/settings.conf
ES_FOLDER=/opt/idssys/emulator-sync
ES_LOGFOLDER="${ES_FOLDER}/logs"
[ ! -d ${ES_LOGFOLDER} ] && mkdir ${ES_LOGFOLDER}
SENDNOTICE(){
[ "${PUSHOVER_APP_TOKEN}" != "" ] && PUSH_TO_MOBILE "${2}
$(date)" "${1}" ${3} &
[ "${EMAIL_NOTICE}" != "" ] && echo -e "${1}\n\n$(date)" | mail -s "${2}" ${EMAIL_NOTICE}
}
RUNSYNC(){
[ -f ${ES_FOLDER}/nc-sync.running ] && [ $(expr $(date +%s) - $(stat -L --format %Y ${ES_FOLDER}/nc-sync.running)) -gt 21600 ] && /bin/rm -f ${ES_FOLDER}/nc-sync.running
if [ ! -f ${ES_FOLDER}/nc-sync.running ]; then
touch ${ES_FOLDER}/nc-sync.running
if [ ! -f ${NC_PATH}/mounted ]; then
/usr/bin/mount -a
sleep 5s
fi
if [ -f ${NC_PATH}/mounted ] || [ "${1}" == "skipmount" ]; then
echo -en "${idsCL[LightCyan]}Sync Started : ${idsCL[Default]}"
[ -f ${ES_FOLDER}/mount.failure ] && /bin/rm -f ${ES_FOLDER}/mount.failure
if [ -f ${ES_LOGFOLDER}/failures.txt ]; then
# echo -e "${idsCL[Red]}Failed${idsCL[Default]}"
if [ -f ${ES_LOGFOLDER}/failures2.txt ]; then
# echo "Nextcloud sync failure, ${ES_LOGFOLDER}/failures2.txt exists."
# SENDNOTICE "Nextcloud Sync Failure" "${ES_LOGFOLDER}/failures2.txt Exists\n\n$(date)" 1
[ $(expr `date +%s` - $(stat -c %Y ${ES_LOGFOLDER}/failures.txt)) -gt 1800 ] && /bin/rm -f ${ES_LOGFOLDER}/failures.txt
else
/bin/mv ${ES_LOGFOLDER}/failures.txt ${ES_LOGFOLDER}/failures2.txt
fi
fi
if [ ! -f ${ES_LOGFOLDER}/failures.txt ]; then
for i in `seq 9 -1 1`; do
mv "${ES_LOGFOLDER}/nextcloudcmd.txt."{$i,$((i+1))} 2>/dev/null
done
mv ${ES_LOGFOLDER}/nextcloudcmd.txt ${ES_LOGFOLDER}/nextcloudcmd.txt.1 2>/dev/null
dbfile=( ${NC_PATH}/.*.db )
/usr/bin/sqlite3 $dbfile 'PRAGMA journal_mode = delete;' 2>/dev/null
${ES_FOLDER}/nextcloud-sync-cron/nextcloud-sync-cron.sh --verbose --logdir ${ES_LOGFOLDER} ${ES_FOLDER}/nextcloud-client.conf
if [ -f ${ES_LOGFOLDER}/failures2.txt ] && [ ! -f ${ES_LOGFOLDER}/failures.txt ]; then
echo -e "${idsCL[Green]}Fixed, the last sync had failed${idsCL[Default]}"
echo "$(date '+%F %T'): Fixed" >> ${ES_LOGFOLDER}/sync.log
SENDNOTICE "Nextcloud Sync Fixed" "Sync is running again"
/bin/rm -f ${ES_LOGFOLDER}/failures*
elif [ -f ${ES_LOGFOLDER}/failures2.txt ] && [ -f ${ES_LOGFOLDER}/failures.txt ]; then
echo -e "${idsCL[Red]}Nextcloud repeat sync failure"
SENDNOTICE "Nextcloud repeat Sync Failure" "${ES_LOGFOLDER}/failures2.txt Exists\n\n$(date)" 1
else
echo -e "${idsCL[Green]}Done${idsCL[Default]}"
fi
/bin/rm -f ${ES_FOLDER}/nc-sync.running
fi
else
if [ -f ${ES_FOLDER}/mount.failure ]; then
SENDNOTICE "Nextcloud Sync Unmounted" "The folder ${NC_PATH} Cannot be mounted" 1
else
/usr/bin/mount -a
touch ${ES_FOLDER}/mount.failure
fi
/bin/rm -f ${ES_FOLDER}/nc-sync.running
exit 1
fi
else
echo "There is a sync in process"
exit 0
fi
}
echo
echo -e "${idsCL[LightGreen]} Emulator-Nextcloud-Sync ${idsCL[Default]} ${idsCL[DarkGray]}(ver-${VERS})${idsCL[Default]}"
DIVIDER . lightGreen
echo
case ${1} in
sync) RUNSYNC ${2};;
esac
exit 1