#!/usr/bin/env bash # Media/VPN Manager Scripts action="$1" VERBOSE=true [ "${2}" != "q" ] && [ "${2}" != "qq" ] && source /opt/idssys/defaults/colors.inc source /opt/idssys/defaults/default.inc source /opt/idssys/mediamanager/settings.conf source /opt/idssys/mediamanager/defaults.inc [ -f ${TMPFOLDER}/realwanip ] && source ${TMPFOLDER}/realwanip [ -f ${TMPFOLDER}/vers.info ] && source ${TMPFOLDER}/vers.info [ "${GITBRANCH}" != "" ] && GBRANCH="${GITBRANCH}" || GBRANCH="master" [ -f ${TMPFOLDER}/vpninfo ] && source ${TMPFOLDER}/vpninfo if [[ "${noheader}" != *" ${1} "* ]] && [[ "${noheader}" != *" ${2} "* ]] || ([ "${1}" == "update" ] && [ ! -z ${2+x} ]); then if [ -f ${TMPFOLDER}/update.available ]; then dispup="${idsCL[Yellow]} - Update Available! (run '${idsCL[LightCyan]}mm update${idsCL[Yellow]}')${idsCL[Default]}" else dispup="" fi echo echo -e "${idsCL[LightGreen]} Media/VPN Manager ${idsCL[Default]} ${idsCL[DarkGray]}(ver-${VERS})${dispup}${idsCL[Default]}" DIVIDER . lightGreen fi cw_spc=40 MMCRONCHECK(){ if [ "$(/bin/ip route show | grep default | cut -d' ' -f 3)" == "" ]; then OUT=$(/bin/ip link | grep "${VPN_INTERFACE}") [ ${#OUT} = 0 ] && /bin/ip route add default via ${DEFAULT_GW} fi if [ "${SERVICESCHECK}" != "" ]; then if [ $(date +%u) -eq 6 ] && [ $(date +%H)$(date +%M) -ge 0400 ] && [ $(date +%H)$(date +%M) -le 0405 ] ; then if [ $(expr `date +%s` - $(stat -c %Y ${TMPFOLDER}/vers.info)) -gt 600 ]; then UPDATESERVICE service all q >/dev/null 2>&1 echo "$(date) - Updated All Services" >> ${MM_LOGFILE} fi elif [ $(date +%H)$(date +%u) -eq 6 ] && [ $(date +%H)$(date +%M) -ge 0300 ] && [ $(date +%H)$(date +%M) -lt 0305 ] ; then if [ $(expr `date +%s` - $(stat -c %Y ${MMFOLDER}/.git/ORIG_HEAD)) -gt 600 ] && [ "`git -C /opt/idssys/nodemgmt log --pretty=%H ...refs/heads/${GBRANCH}^ | head -n 1`" != "`git -C /opt/idssys/nodemgmt ls-remote origin -h refs/heads/${GBRANCH} |cut -f1`" ]; then echo "updating mm executing" /usr/local/bin/mm update q >/dev/null 2>&1 source /opt/idssys/mediamanager/defaults.inc echo "$(date) - Updated MM Scripts to v${VERS}" >> ${MM_LOGFILE} fi fi fi if [ -f ${TMPFOLDER}/mm.stop ]; then /usr/local/bin/mm check q >/dev/null 2>&1 else echo -e "$(date) - Starting log #############################" >> ${MM_OUTPUT_LOGFILE} /usr/local/bin/mm check qq >> ${MM_OUTPUT_LOGFILE} echo -e "$(date) - Finished log #############################" >> ${MM_OUTPUT_LOGFILE} fi exit 0 } MMSTART(){ [ $VERBOSE = true ] && chktmstart=`date +%s` ([ "${1}" == "start" ] || [ "${1}" == "check" ]) && touch ${TMPFOLDER}/mm.stop.checking ([ "${2}" == "vpn" ] || [ -f ${MMFOLDER}/mm.noservices ]) && touch ${TMPFOLDER}/mm.noservices if [ "${1}" = "start" ] || [ "${1}" = "restart" ] || [ "${1}" = "" ]; then GOFORSTART=true; elif [ "${1}" == "check" ] && [ "$(/bin/ip link | grep "${VPN_INTERFACE}")" == "" ] && [ ! -f ${TMPFOLDER}/mm.vpn* ] && [ ! -f ${TMPFOLDER}/vpninfo.* ]; then GOFORSTART=true; [ ! -f ${MMFOLDER}/mm.noservices ] && SENDNOTICE "Starting MM/VPN Systems" "Fresh Startup" echo "$(date) - Starting MM/VPN system - (fresh start)" >> ${MM_LOGFILE} echo -e "${idsCL[LightCyan]}Fresh Startup${idsCL[Default]}" elif [ "${1}" = "check" ] && [ "$(/bin/ip link | grep "${VPN_INTERFACE}")" = "" ] && [ -f ${TMPFOLDER}/mm.vpn* ]; then GOFORSTART=true; echo "$(date) - Attempting to restart systems after VPN failure..." >> ${MM_LOGFILE} echo -e "${idsCL[LightCyan]}Attempting to restart systems after VPN failure...${idsCL[Default]}" SENDNOTICE "Attempting to start systems after VPN failure..." else GOFORSTART=false; fi if [ ${GOFORSTART} = true ]; then mmsv='Starting' echo OUT=$(/bin/ip link | grep "${VPN_INTERFACE}") if [ ${#OUT} = 0 ]; then echo "$(date) - ${mmsv} VPN..." >> ${MM_LOGFILE} msg="Setting up the VPN Tunnel config" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" VPNCONFIG_TORGUARD echo -e "${idsCL[Green]}Done${idsCL[Default]}" msg="Starting the VPN Tunnel" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" REALWANIP=$(curl -s http://whatismyip.akamai.com) echo "REALWANIP=\"${REALWANIP}\"" > ${TMPFOLDER}/realwanip "${VPN_START[@]}" >/dev/null 2>&1 sleep 5s else msg="The VPN Tunnel is already running" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" fi else msg="Verifying the VPN Tunnel" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" mmsv='Verifying' # [ $VERBOSE = true ] && echo "$(date) - ${mmsv} VPN..." >> ${MM_LOGFILE} fi OUT=$(/bin/ip link | grep "$VPN_INTERFACE") if [ ${#OUT} = 0 ]; then if [ -f ${TMPFOLDER}/mm.vpn.fail ]; then echo -e "${idsCL[Red]} failed, restarting system${idsCL[Default]}" echo "$(date) - VPN Failure #2 - restarting system" >> ${MM_LOGFILE} SENDNOTICE "VPN Failure" "2nd Failure, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail2 rm -f ${TMPFOLDER}/mm.vpn.fail MMSTOP # sleep 2m # "${VPN_START[@]}" >/dev/null 2>&1 # sleep 10s rm -f ${TMPFOLDER}/mm.stop* # rm -f ${NM_FOLDER}/.tmp/mm.stop* [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 elif [ -f ${TMPFOLDER}/mm.vpn.fail2 ]; then echo -e "${idsCL[Red]} failed, shutting down system${idsCL[Default]}" echo "$(date) - VPN Failure #3 - Shutting down MM/VPN systems" >> ${MM_LOGFILE} SENDNOTICE "VPN Failure" "Final Failure, shutting down system" 2 MMSTOP [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 else echo -e "${idsCL[Red]} failed, restarting system${idsCL[Default]}" echo "$(date) - VPN Failure #1 - restarting system" >> ${MM_LOGFILE} SENDNOTICE "VPN Failure" "1st Failure, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail MMSTOP # sleep 10s # "${VPN_START[@]}" >/dev/null 2>&1 # sleep 10s rm -f ${TMPFOLDER}/mm.stop* # rm -f ${NM_FOLDER}/.tmp/mm.stop* [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 fi fi [ "${1}" == "start" ] && rm -f mm.vpn.fail* OUT=$(/bin/ip link | grep "$VPN_INTERFACE") if [ ${#OUT} != 0 ]; then if [ ${GOFORSTART} = true ] || [ "${1}" == "check" ]; then echo -e "${idsCL[Green]}Done${idsCL[Default]}" fi vpnuptime=$(expr `date +%s` - $(stat -c %Y ${TMPFOLDER}/vpninfo)) if [ ${vpnuptime} -ge ${MM_VPN_RESTART} ]; then if [ ${MM_VPN_RESTART} -eq 86400 ]; then echo "$(date) - 24hr session limit reached, restarting vpn & services" >> ${MM_LOGFILE} echo -en "${idsCL[LightCyan]}24hr session limit reached, restarting vpn & services" else sessiontimeout=$(date -d@${MM_VPN_RESTART} -u "+%-H-%-M-%-S") IFS='-'; read -a session_timeout <<< "${sessiontimeout}"; unset IFS sessiontimeout="${session_timeout[0]} hr" [ ${session_timeout[1]} -gt 0 ] && sessiontimeout="${sessiontimeout}, ${session_timeout[1]} min" [ ${session_timeout[2]} -gt 0 ] && sessiontimeout="${sessiontimeout}, ${session_timeout[2]} sec" echo "$(date) - Your '${sessiontimeout}' session limit reached, restarting vpn & services" echo "$(date) - Your '${sessiontimeout}' session limit reached, restarting vpn & services" >> ${MM_LOGFILE} echo -en "${idsCL[LightCyan]}Your '${idsCL[White]}${sessiontimeout}${idsCL[LightCyan]}' session limit reached, restarting vpn & services${idsCL[Default]}" fi ([ -f ${TMPFOLDER}/mm.noservices ] || [ -f ${MMFOLDER}/mm.noservices ]) && rv="vpn" || rv="" MMSTOP >/dev/null 2>&1 echo -en "." read -et 10 echo -en "." read -et 10 echo -en "." read -et 10 echo -en "." MMSTART start ${rv} >/dev/null 2>&1 echo -e " ${idsCL[Green]}Done${idsCL[Default]}" exit 0 fi zerogw=$(/bin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) defaultgw=$(/bin/ip route show | grep default | cut -d' ' -f 3) # if [ "${zerogw}" != "${defaultgw}" ]; then # if [ "${defaultgw}" != "${VPN_INTERFACE}" ]; then if [ "${zerogw}" != "${defaultgw}" ] || [ "${defaultgw}" != "${VPN_INTERFACE}" ]; then echo "$(date) - Adding VPN IP Routes" >> ${MM_LOGFILE} msg="Adding VPN IP Routes" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" /bin/ip route del default /bin/ip route add default dev ${VPN_INTERFACE} /bin/ip route add 0.0.0.0/1 dev ${VPN_INTERFACE} /bin/ip route add 128.0.0.0/1 dev ${VPN_INTERFACE} /bin/ip route add ${vpn_server_address}/32 via ${DEFAULT_GW} dev eth0 if [ "${ADDROUTES}" != "" ]; then for route in "${ADD_ROUTES[@]}"; do /bin/ip route add ${route} via ${DEFAULT_GW} dev eth0 done fi echo -e "${idsCL[Green]}Done${idsCL[Default]}" zerogw=$(/bin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) defaultgw=$(/bin/ip route show | grep default | cut -d' ' -f 3) fi msg="Verifying default network routes" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" checked=false cc=0 until [ "${checked}" = "" ]; do zerogw=$(/bin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) defaultgw=$(/bin/ip route show | grep default | cut -d' ' -f 3) if [ "${zerogw}" == "${defaultgw}" ] && [ "${defaultgw}" == "${VPN_INTERFACE}" ]; then checked="" echo -e "${idsCL[Green]}Done${idsCL[Default]}" elif [ ${cc} -eq 1 ] && [ "${zerogw}" == "${VPN_INTERFACE}" ] && [ "${defaultgw}" != "${VPN_INTERFACE}" ]; then echo -en "Fixing... " echo -e "$(date) - DEFAULT ROUTES FAILURE {zerogw=${zerogw}, defaultgw=${defaultgw}}\n-------------------------------------------------------------------" >> ${LOGFOLDER}/iproutes.log /bin/ip r >> ${LOGFOLDER}/iproutes.log echo -e "\n" >> ${LOGFOLDER}/iproutes.log /bin/ip route del default /bin/ip route add default dev ${VPN_INTERFACE} SENDNOTICE "MM FIX" "Default route failure, default route re-added" elif [ ${cc} -eq 10 ]; then echo -e "${idsCL[Red]}Could not confirm default routes, restarting system!${idsCL[Default]}" echo "$(date) - Could not confirm default routes, restarting system" >> ${MM_LOGFILE} SENDNOTICE "Network Failure" "Default route failure, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail echo -e "$(date) - DEFAULT ROUTES FAILURE\n-------------------------------------------------------------------" >> ${LOGFOLDER}/iproutes.log /bin/ip r >> ${LOGFOLDER}/iproutes.log echo -e "\n" >> ${LOGFOLDER}/iproutes.log MMSTOP sleep 10s # "${VPN_START[@]}" >/dev/null 2>&1 # sleep 10s rm -f ${TMPFOLDER}/mm.stop* # rm -f ${NM_FOLDER}/.tmp/mm.stop* [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 fi ((cc=${cc}+1)) sleep 1s done msg="Verifying traffic routing through VPN" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" mmip="" cc=0 until [ "${REALWANIP}" != "" ] && [ "${mmip}" != "" ] && [[ "${REALWANIP}" != *"connection timed out"* ]] && [[ "${mmip}" != *"connection timed out"* ]]; do # mmip=$(/usr/bin/dig +short +time=2 myip.opendns.com @208.67.222.222) mmip=$(curl -s http://whatismyip.akamai.com) if [ ${cc} -eq 10 ]; then echo echo -e "${idsCL[Red]}Cannot get an WAN address, restarting system!${idsCL[Default]}" echo "$(date) - Cannot get an WAN address, restarting system! wanip(${REALWANIP}) vpnip(${mmip})" >> ${MM_LOGFILE} [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> ${MM_LOGFILE} SENDNOTICE "Network Failure" "Cannot get an WAN address, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail MMSTOP >/dev/null 2>&1 sleep 10s # "${VPN_START[@]}" >/dev/null 2>&1 # sleep 10s rm -f ${TMPFOLDER}/mm.stop* # rm -f ${NM_FOLDER}/.tmp/mm.stop* [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 fi ((cc=${cc}+1)) sleep 1s done if [ "${REALWANIP}" != "${mmip}" ]; then echo -e "${idsCL[Green]}Done${idsCL[Default]}" # [ $VERBOSE = true ] && echo "$(date) - WAN Address: ${mmip} - GW Address: ${defaultgw}" >> ${MM_LOGFILE} else echo echo -e "${idsCL[Red]}Traffic is not routing through VPN, restarting system!${idsCL[Default]}" echo "$(date) - Traffic is not routing through VPN, restarting system! wanip(${REALWANIP}) vpnip(${mmip})" >> ${MM_LOGFILE} [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> ${MM_LOGFILE} SENDNOTICE "Network Failure" "Traffic route failure, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail MMSTOP sleep 10s # "${VPN_START[@]}" >/dev/null 2>&1 # sleep 10s rm -f ${TMPFOLDER}/mm.stop* # rm -f ${NM_FOLDER}/.tmp/mm.stop* [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 fi [ "${1}" == "start" ] && [ "${2}" != "vpn" ] && rm -f ${TMPFOLDER}/mm.noservices if [ "${SERVICESCHECK}" != "" ]; then msg="Verifying Plex Media is Mounted" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" ### CHECK FOR PLEX MOUNT if [ ! -f ${PLEX_MOUNT}/mounted ] && [ "${2}" != "vpn" ] && [ ! -f ${TMPFOLDER}/mm.noservices ] && [ ! -f ${MMFOLDER}/mm.noservices ]; then echo "$(date) - Plex Media Not Mounted, will try and mount" >> ${MM_LOGFILE} umount ${PLEX_MOUNT} >/dev/null 2>&1 sleep 2s mount -a >/dev/null 2>&1 sleep 2s if [ ! -f ${PLEX_MOUNT}/mounted ]; then if [ -f "${TMPFOLDER}/mm.plexmount.fail2" ]; then fd=$(( `date +%s` - `stat -L --format %Y "${TMPFOLDER}/mm.plexmount.fail2"` )) [ ${fd} -gt 100 ] && PMFS=true || PMFS=false elif [ -f ${TMPFOLDER}/mm.plexmount.fail ]; then fd=$(( `date +%s` - `stat -L --format %Y ${TMPFOLDER}/mm.plexmount.fail` )) [ ${fd} -gt 100 ] && PMFS=true || PMFS=false else PMFS=true fi if [ ${PMFS} = true ]; then [ -f "${TMPFOLDER}/mm.plexmount.fail2" ] && touch ${TMPFOLDER}/mm.plexmount.fail2 || touch ${TMPFOLDER}/mm.plexmount.fail SENDNOTICE "Plex Media Not Mounted" "Could not mount the Plex Media Folder" 1 echo "$(date) - Plex Media could not be mounted" >> ${MM_LOGFILE} fi if [ ! -f ${TMPFOLDER}/mm.plexmount.fail2 ]; then echo -e "${idsCL[Red]}Not Mounted${idsCL[Default]}" for srvc in "${SERVICES_CHECK[@]}"; do /bin/systemctl stop ${srvc} >/dev/null 2>&1 & done else echo -e "${idsCL[Red]}Not Mounted${idsCL[LightCyan]}, running services as chosen previously though${idsCL[Default]}" fi else echo -e "${idsCL[Green]}Mounted, fixed${idsCL[Default]}" SENDNOTICE "Plex Media Mounted" "The Plex Media Folder was successfully re-mounted again!" echo "$(date) - Plex Media now mounted, restarting services" >> ${MM_LOGFILE} for srvc in "${SERVICES_CHECK[@]}"; do /bin/systemctl restart ${srvc} >/dev/null 2>&1 & done rm -f ${TMPFOLDER}/mm.plexmount.fail* fi elif [ "${2}" == "vpn" ] || [ -f ${TMPFOLDER}/mm.noservices ] || [ -f ${MMFOLDER}/mm.noservices ]; then echo -e "${idsCL[LightCyan]}Skipping, started wtih VPN only${idsCL[Default]}" else echo -e "${idsCL[Green]}Mounted${idsCL[Default]}" if [ -f ${TMPFOLDER}/mm.plexmount.fail ]; then echo -e "${idsCL[Green]}Mounted, fixed${idsCL[Default]}" SENDNOTICE "Plex Media Mounted" "The Plex Media Folder was successfully re-mounted again!" echo "$(date) - Plex Media now mounted, restarting services" >> ${MM_LOGFILE} for srvc in "${SERVICES_CHECK[@]}"; do /bin/systemctl restart ${srvc} >/dev/null 2>&1 & done rm -f ${TMPFOLDER}/mm.plexmount.fail* fi fi else echo -e "${idsCL[LightCyan]}Skipping, started wtih VPN only${idsCL[Default]}" fi if [ -f ${TMPFOLDER}/mm.plexmount.fail ] && [ ! -f ${TMPFOLDER}/mm.noservices ] && [ ! -f ${MMFOLDER}/mm.noservices ] && [ "${1}" == "start" ] && [ "${2}" != "vpn" ]; then echo -en "${idsCL[LightCyan]}Would you like to start services without Plex Media being mounted? ${idsCL[LightYellow]}(y/N)${idsCL[LightCyan]}? ${idsCL[Default]}" read -n 1 choice case "$choice" in [Yy]) rm -f ${TMPFOLDER}/mm.plexmount.fail* touch ${TMPFOLDER}/mm.plexmount.fail2 echo;echo ;; * ) echo;echo ;; esac fi msg="${mmsv} Services"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" if [ "${2}" != "vpn" ] && [ ! -f ${TMPFOLDER}/mm.noservices ] && [ ! -f ${MMFOLDER}/mm.noservices ] && [ ! -f ${TMPFOLDER}/mm.plexmount.fail ]; then for srvc in "${SERVICES_CHECK[@]}"; do [ "$(systemctl is-active ${srvc})" != "active" ] && /bin/systemctl restart ${srvc} done echo -e "${idsCL[Green]}Done${idsCL[Default]}" msg="Waiting for Services to Start" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" for srvc in "${SERVICES_CHECK[@]}"; do if [ "${MM_SERVICE_PORTS[${srvc}]}" != "" ]; then checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t) tryupdate=false cc=0 until [ "${checkport}" != "" ]; do if [ ${cc} -eq 10 ]; then if [ -f ${TMPFOLDER}/mm.${srvc}.starting ]; then msg="${srvc^} Service is not starting properly, will try and update" echo -e "${idsCL[Red]}${msg}${idsCL[Default]}" echo "$(date) - ${msg}" >> ${MM_LOGFILE} SENDNOTICE "${srvc^} Service Issue" "${msg}" 1 tryupdate="true" msg="Updating ${srvc^} Service" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" rm -f ${TMPFOLDER}/mm.${srvc}.starting else [ "${MM_UPDATE_REPO[$srvc]}" != "" ] && touch ${TMPFOLDER}/mm.${srvc}.starting fi checkport="go" fi if [ "${checkport}" != "go" ]; then checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t) ((cc=${cc}+1)) sleep 1s fi done if ([ "${checkport}" == "" ] || [ "${tryupdate}" == "true" ]) && [ "${MM_UPDATE_REPO[${srvc}]}" != "" ]; then UPDATESERVICE service ${srvc} -f >/dev/null 2>&1 echo -e "${idsCL[Green]}Done${idsCL[Default]}" elif [ "${tryupdate}" == "true" ]; then echo -e "${idsCL[Green]}Done${idsCL[Default]}" fi [ "${checkport}" != "" ] && rm -f ${TMPFOLDER}/mm.${srvc}.starting >/dev/null 2>&1 fi done echo -e "${idsCL[Green]}Done${idsCL[Default]}" elif [ -f ${TMPFOLDER}/mm.noservices ] || [ -f ${MMFOLDER}/mm.noservices ]; then echo -e "${idsCL[LightCyan]}Skipping, started wtih VPN only${idsCL[Default]}" elif [ -f ${TMPFOLDER}/mm.plexmount.fail ]; then echo -e "${idsCL[LightCyan]}Skipping, plex media is not mounted${idsCL[Default]}" else echo -e "${idsCL[LightYellow]}Skipping${idsCL[Default]}" fi [ "${2}" != "q" ] && [ "${2}" != "qq" ] && echo if [ "${1}" == "start" ] || [ "${1}" == "restart" ]; then #echo -e "${idsCL[LightCyan]}It may take a couple of minutes before all services fully start${idsCL[Default]}" echo "$(date) - MM/VPN systems STARTED" >> ${MM_LOGFILE} MMSTATUS fi if [ -f ${TMPFOLDER}/mm.vpn.fail* ]; then echo "$(date) - VPN issue has been fixed" >> ${MM_LOGFILE} SENDNOTICE "Services Restored" "VPN issue has been fixed" rm -f ${TMPFOLDER}/mm.vpn.fail* fi rm -f ${TMPFOLDER}/mm.stop* # rm -f ${NM_FOLDER}/.tmp/mm.stop* echo -e "${idsCL[Green]}All systems are go!${idsCL[Default]}" fi [ "${2}" != "q" ] && [ "${2}" != "qq" ] && echo [ $VERBOSE = true ] && DEBUGLOGSTOP #exit 0 } MMSTOP(){ if [ -f ${TMPFOLDER}/mm.stop ]; then echo -e "${idsCL[Yellow]}The system is already stopped, will verify anyway...${idsCL[Default]}" else echo -e "${idsCL[Green]}Stopping System...${idsCL[Default]}" echo "$(date) - Stopping VPN..." >> ${MM_LOGFILE} fi echo touch ${TMPFOLDER}/mm.stop # touch ${NM_FOLDER}/.tmp/mm.stop # echo -en "${idsCL[LightCyan]}Stopping background MM scripts... ${idsCL[Default]}" # getMMbash=$(pgrep -f '/mm') # for i in $getMMbash; do # /bin/kill $i # done # echo -e "${idsCL[Green]}Done${idsCL[Default]}" if [ "${SERVICESCHECK}" != "" ]; then msg="Stopping Services" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" for srvc in "${!MM_SERVICES[@]}"; do if [[ ${srvc} != *"qbittorrent"* ]] && [[ ${srvc} != *"vpn"* ]] && [[ ${srvc} != *"wireguard"* ]]; then /bin/systemctl stop ${srvc} >/dev/null 2>&1 & fi done sleep 2s /bin/systemctl stop qbittorrent echo -e "${idsCL[Green]}Done${idsCL[Default]}" msg="Waiting for Services to stop" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" for srvc in "${!MM_SERVICES[@]}"; do if [ "MM_SERVICE_PORTS[${srvc}]" != "" ] && [[ ${srvc} != *"vpn"* ]] && [[ ${srvc} != *"wireguard"* ]]; then checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t) # echo "$srvc - ${MM_SERVICE_PORTS[${srvc}]} - $checkport" until [ "${checkport}" == "" ]; do sleep 1s checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t) done fi done echo -e "${idsCL[Green]}Done${idsCL[Default]}" fi msg="Shutting Down VPN Tunnel" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" "${VPN_STOP[@]}" >/dev/null 2>&1 checked=false until [ "${checked}" = "" ]; do OUT=$(/bin/ip link | grep "$VPN_INTERFACE") #OUT=`ip a show $VPN_INTERFACE up` >/dev/null 2>&1 if [ ${#OUT} = 0 ]; then checked="" fi done echo -e "${idsCL[Green]}Done${idsCL[Default]}" msg="Re-Adding Default Network Routes" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[LightCyan]}${msg}${spc} ${idsCL[White]}: ${idsCL[Default]}" [ "$(/bin/ip route show | grep default | cut -d' ' -f 3)" != "${DEFAULT_GW}" ] && /bin/ip route add default via ${DEFAULT_GW} [ "${vpn_server_address}" != "" ] && /bin/ip route del ${vpn_server_address} if [ "${ADDROUTES}" != "" ]; then for route in "${ADD_ROUTES[@]}"; do /bin/ip route del ${route} via ${DEFAULT_GW} dev eth0 done fi [ $VERBOSE = true ] && echo "$(date) - Added Default IP Routes back in" >> ${MM_LOGFILE} echo -e "${idsCL[Green]}Done${idsCL[Default]}" rm -f ${TMPFOLDER}/vpninfo* rm -f ${TMPFOLDER}/realwanip [ ! -f ${MMFOLDER}/mm.noservices ] && rm -f ${TMPFOLDER}/mm.noservices* rm -f ${TMPFOLDER}/mm.plexmount.fail* rm -f ${TMPFOLDER}/mm.stop.checking echo "$(date) - MM/VPN systems STOPPED" >> ${MM_LOGFILE} if [ "${SERVICESCHECK}" != "" ]; then echo echo -e "${idsCL[Red]}All services have been stopped${idsCL[Default]}" fi echo touch ${TMPFOLDER}/mm.stop # touch ${NM_FOLDER}/.tmp/mm.stop } FULLSTOP(){ echo -en "${idsCL[LightCyan]}Stopping any background MM scripts still running... ${idsCL[Default]}" getMMbash=$(pgrep -f '/mm') >/dev/null 2>&1 for i in ${getMMbash}; do [ ${i} -ne ${BASHPID} ] && /bin/kill -s PIPE ${i} >/dev/null 1>&2 done echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo exit 0 } MMSTATUS(){ statusArray=(wireguard) statusArray+=(${SERVICES_CHECK[@]}) echo for srvc in "${statusArray[@]}"; do c=0; spc='' spc1=$((${cw_spc}-${#MM_SERVICES[${srvc}]})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done dispname="${MM_SERVICES[${srvc}]/(/${idsCL[LightCyan]}(}" echo -en " ${idsCL[White]}${dispname}${spc}${idsCL[White]}: ${idsCL[Default]}" if [ "$(systemctl is-active ${srvc})" != "active" ] && [ "${srvc}" != "wireguard" ]; then if [ -f ${TMPFOLDER}/mm.noservices ] || [ -f ${MMFOLDER}/mm.noservices ]; then echo -e "${idsCL[LightCyan]}Skipping, started wtih VPN only${idsCL[Default]}" elif [ -f ${TMPFOLDER}/mm.plexmount.fail ]; then echo -e "${idsCL[LightCyan]}Skipping, Plex Media not mounted${idsCL[Default]}" else echo -e "${idsCL[Red]}Not Running${idsCL[Default]}" fi elif [ "${srvc}" == "wireguard" ] && [ "$(/bin/ip link | grep $VPN_INTERFACE)" == "" ]; then echo -e "${idsCL[Red]}Not Running${idsCL[Default]}" else [ "${MM_SERVICE_PORTS[${srvc}]}" != "" ] && checkport=$(lsof -Pi :${MM_SERVICE_PORTS[${srvc}]} -sTCP:LISTEN -t) if ([ "${MM_SERVICE_PORTS[${srvc}]}" != "" ] && [ "${checkport}" != "" ]) || [ "${MM_SERVICE_PORTS[${srvc}]}" == "" ]; then [[ ${srvc} == *"wireguard"* ]] && echo -e "${idsCL[Green]}Running${idsCL[Default]}" || echo -e "${idsCL[Green]}Running ${idsCL[Cyan]}(Port: ${MM_SERVICE_PORTS[${srvc}]})${idsCL[Default]}" elif [ "${checkport}" == "" ]; then echo -e "${idsCL[LightMagenta]}Service Running, Web Port Not Accesible${idsCL[Default]}" fi if [[ ${srvc} == *"vpn"* ]] || [[ ${srvc} == *"wireguard"* ]]; then zerogw=$(/bin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) defaultgw=$(/bin/ip route show | grep default | cut -d' ' -f 3) vpnuptime=$(expr `date +%s` - $(stat -c %Y ${TMPFOLDER}/vpninfo)) msg=" |- ${idsCL[Default]}VPN/Services Uptime" c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Green]}$(SHOW_TIME ${vpnuptime})${idsCL[Default]}" msg=" |- ${idsCL[Default]}0.0.0.0 Gateway" c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}" echo -e "${idsCL[Green]}$zerogw${idsCL[Default]}" msg=" |- ${idsCL[Default]}DEFAULT Gateway" c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}" if [ "${zerogw}" == "${defaultgw}" ] && [ "${zerogw}" == "${VPN_INTERFACE}" ]; then echo -e "${idsCL[Green]}$defaultgw${idsCL[Default]}" else echo -e "${idsCL[Red]}$defaultgw - Incorrect Gateways${idsCL[Default]}" if ([ ! -f ${TMPFOLDER}/mm.stop* ] && [ "${1}" == "fix" ]) || ([ ! -f ${TMPFOLDER}/mm.stop ] && [ -f ${TMPFOLDER}/mm.stop.checking ] && [ "${1}" == "fix" ]); then echo echo -e "${idsCL[LightCyan]}Restarting system...${idsCL[Default]}" ([ -f ${TMPFOLDER}/mm.noservices ] || [ -f ${MMFOLDER}/mm.noservices ]) && rv="vpn" || rv="" MMSTOP echo -en "${idsCL[LightCyan]}Pausing for 10secs... ${idsCL[Default]}" sleep 10s echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo echo -e "${idsCL[LightCyan]}Starting system back up...${idsCL[Default]}" echo MMSTART start ${rv} msg=" ${MM_SERVICES[wireguard]}" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]}: ${idsCL[Default]}" echo -e "${idsCL[Green]}Running${idsCL[Default]}" vpnuptime=$(expr `date +%s` - $(stat -c %Y ${TMPFOLDER}/vpninfo)) msg=" |- ${idsCL[Default]}VPN/Services Uptime" c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Green]}$(SHOW_TIME ${vpnuptime})${idsCL[Default]}" zerogw=$(/bin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) defaultgw=$(/bin/ip route show | grep default | cut -d' ' -f 3) msg=" |- ${idsCL[Default]}0.0.0.0 Gateway" c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}" echo -e "${idsCL[Green]}$zerogw${idsCL[Default]}" msg=" |- ${idsCL[Default]}DEFAULT Gateway" c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}" if [ "${zerogw}" == "${defaultgw}" ] && [ "${zerogw}" == "${VPN_INTERFACE}" ]; then echo -e "${idsCL[Green]}$defaultgw${idsCL[Default]}" else echo -e "${idsCL[Red]}$defaultgw - Incorrect Gateways${idsCL[Default]}" echo if [ ! -f ${TMPFOLDER}/mm.stop.checking ]; then echo -e "${idsCL[LightCyan]}Shutting down system... fix yo shit${idsCL[Default]}" MMSTOP fi fi fi fi msg=" |- ${idsCL[Default]}Verify routing through VPN" c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}" # mmip=$(/usr/bin/dig +short myip.opendns.com @resolver1.opendns.com) # mmip=$(/usr/bin/dig +short +time=2 myip.opendns.com @208.67.222.222) mmip=$(curl -s http://whatismyip.akamai.com) if [ "${REALWANIP}" != "${mmip}" ] && [[ "${mmip}" != *"timed out"* ]]; then echo -e "${idsCL[Green]}Good${idsCL[Default]}" else echo -e "${idsCL[Red]}Not Routing${idsCL[Default]}" if ([ ! -f ${TMPFOLDER}/mm.stop* ] && ([ "${1}" == "fix" ] || [ "${1}" == "q" ])) || ([ ! -f ${TMPFOLDER}/mm.stop ] && [ -f ${TMPFOLDER}/mm.stop.checking ] && ([ "${1}" == "fix" ] || [ "${1}" == "q" ])); then echo echo -e "${idsCL[LightCyan]}Restarting system...${idsCL[Default]}" ([ -f ${TMPFOLDER}/mm.noservices ] || [ -f ${MMFOLDER}/mm.noservices ]) && rv="vpn" || rv="" MMSTOP echo -en "${idsCL[LightCyan]}Pausing for 10secs... ${idsCL[Default]}" sleep 10s echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo echo -e "${idsCL[LightCyan]}Starting system back up...${idsCL[Default]}" echo MMSTART start ${rv} fi fi msg=" |- ${idsCL[Default]}Real WAN Address" c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Green]}${REALWANIP}${idsCL[Default]}" msg=" |- ${idsCL[Default]}VPN WAN Address" c=0; spc=''; spc1=$((${cw_spc}-${#msg}+6)) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Green]}${mmip}${idsCL[Default]}" if [ ! -f ${MMFOLDER}/mm.noservices ]; then ### CHECK FOR PLEX MOUNT echo msg=" Verifying Plex Media Mount" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc}${idsCL[White]} : ${idsCL[Default]}" if [ -f ${TMPFOLDER}/mm.plexmount.fail2 ]; then echo -e "${idsCL[Red]}Not Mounted${idsCL[LightCyan]}, running services as chosen previously${idsCL[Default]}" elif [ ! -f ${PLEX_MOUNT}/mounted ]; then echo -e "${idsCL[Red]}Not Mounted${idsCL[Default]}" else echo -e "${idsCL[Green]}Mounted${idsCL[Default]}" rm -f ${TMPFOLDER}/mm.plexmount.fail* fi echo fi fi fi done echo echo } GETVPNINFO(){ echo -e "${idsCL[Green]}VPN Connection Info${idsCL[Default]}" echo msg="Server Network"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[LightCyan]}${msg}${spc}${idsCL[White]}: ${idsCL[Green]}${VPN_SERVER}${idsCL[Default]}" msg="Server Address"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[LightCyan]}${msg}${spc}${idsCL[White]}: ${idsCL[Green]}${vpn_server_address}${idsCL[Default]}" msg="Server Port"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[LightCyan]}${msg}${spc}${idsCL[White]}: ${idsCL[Green]}${vpn_server_port}${idsCL[Default]}" msg="Server Public Key"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[LightCyan]}${msg}${spc}${idsCL[White]}: ${idsCL[Green]}${vpn_publickey}${idsCL[Default]}" echo msg="VPN Traffic Routing"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[LightCyan]}${msg}${spc}${idsCL[White]}: ${idsCL[Green]}${vpn_routes}${idsCL[Default]}" msg="VPN Server/Network"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[LightCyan]}${msg}${spc}${idsCL[White]}: ${idsCL[Green]}${vpn_server_ipv4}${idsCL[Default]}" msg="VPN Client Address"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e "${idsCL[LightCyan]}${msg}${spc}${idsCL[White]}: ${idsCL[Green]}${vpn_client_ipv4}${idsCL[Default]}" echo } VPNCONFIG_TORGUARD(){ privkey=$(/usr/bin/wg genkey) reqpublickey=$(echo "${privkey}" | /usr/bin/wg pubkey) reqpublickey="$(php -r 'echo rawurlencode($argv[1]);' -- "$reqpublickey")" rm -f ${TMPFOLDER}/vpninfo JSON=$(curl https://${VPN_SERVER}:1443/api/v1/setup?public-key=${reqpublickey} --user "${VPN_USER}:${VPN_PASS}" -sk) echo -e "vpn_publickey=\"$(echo ${JSON} | jq -r '.server_public_key')\" vpn_server_ipv4=\"$(echo ${JSON} | jq -r '.server_ipv4')\" vpn_client_ipv4=\"$(echo ${JSON} | jq -r '.client_ipv4')\" vpn_routes=\"$(echo ${JSON} | jq -r '.routes')\" vpn_server_address=\"$(echo ${JSON} | jq -r '.vpn_server_address')\" vpn_server_port=\"$(echo ${JSON} | jq -r '.vpn_server_port')\" " > ${TMPFOLDER}/vpninfo source ${TMPFOLDER}/vpninfo rm -f /etc/wireguard/${VPN_INTERFACE}.conf echo -e "[Interface] PrivateKey = ${privkey} ListenPort = 55009 MTU = 1390 DNS = 1.1.1.1 Address = ${vpn_client_ipv4} [Peer] PublicKey = ${vpn_publickey} AllowedIPs = 0.0.0.0/0 Endpoint = ${vpn_server_address}:${vpn_server_port} PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf } VPNSPEEDTEST(){ echo -e "${idsCL[Green]}VPN Speedtest Comparison${idsCL[Default]}" DIVIDER . lightGreen echo if [ ! -f ${TMPFOLDER}/mm.stop ]; then echo -en "${idsCL[LightCyan]}This requires all MM/VPN services to be stopped, ok (Y/n)? ${idsCL[Default]}" read -n 1 choice case "$choice" in [Nn]) exit 0;; esac echo echo -en "${idsCL[LightCyan]}Stopping MM/VPN Services ... ${idsCL[Default]}" else echo -en "${idsCL[LightCyan]}Verifying MM/VPN Services are stopped ... ${idsCL[Default]}" fi MMSTOP >/dev/null 2>&1 echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo echo -e "${idsCL[Green]}Running test without VPN${idsCL[Default]}" DIVIDER . green # speedtest -s 17462 speedtest -s 47744 echo echo -en "${idsCL[LightCyan]}Starting vpn ... ${idsCL[Default]}" # VPNCONFIG_TORGUARD # "${VPN_START[@]}" >/dev/null 2>&1 mm start vpn >/dev/null 2>&1 echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo sleep 5s echo -e "${idsCL[Green]}Running test with VPN${idsCL[Default]}" DIVIDER . green # speedtest -s 17462 speedtest -s 47744 echo echo -en "${idsCL[LightCyan]}Stopping vpn ... ${idsCL[Default]}" # "${VPN_STOP[@]}" >/dev/null 2>&1 ([ -f ${TMPFOLDER}/mm.noservices ] || [ -f ${MMFOLDER}/mm.noservices ]) && rv="vpn" || rv="" MMSTOP >/dev/null 2>&1 echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo echo -en "${idsCL[LightCyan]}Would you like to start MM/VPN services (y/N)? ${idsCL[Default]}" read -n 1 choice echo case "$choice" in [Yy]) MMSTART start ${rv};; * ) echo -e "${idsCL[Cyan]}Not starting. To start, run the command '${idsCL[Green]}mm start${idsCL[LightCyan]}'${idsCL[Default]}";echo;echo;; esac } UPDATESERVICE(){ if [ "${1}" == "service" ]; then touch ${LOGFOLDER}/update.lastupdate if [ "${2}" == "all" ]; then for usrvc in "${UPDATE_SERVICES[@]}"; do [ "${MM_UPDATE_REPO[${usrvc}]}" != "" ] && ([[ " ${SERVICES_CHECK[*]} " =~ " ${usrvc} " ]] || [ "${usrvc}" == "organizr" ]) && UPDATESERVICE service ${usrvc} ${3} done elif [ "${2}" != "" ]; then usrvc=${2} echo "${usrvc} Update" > ${TMPFOLDER}/mm.update.stop echo "$(date) - Updating Service (${usrvc}) : Starting" >> ${MM_LOGFILE} echo echo -e "${idsCL[Green]}Updating ${usrvc^} Service${idsCL[Default]}" DIVIDER . lightGreen if [ "${usrvc}" == "organizr" ]; then msg="Checking for update"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" cd /var/www/media.scity.us/html >/dev/null 2>&1 if [ "`git log --pretty=%H ...refs/heads/v2-master^ | head -n 1`" != "`git ls-remote origin -h refs/heads/v2-master |cut -f1`" ] || [ "${3}" == "-f" ]; then [ "${3}" == "-f" ] && echo -e "${idsCL[LightCyan]}Update Not Needed, forcing anyway${idsCL[Default]}" || echo -e "${idsCL[Green]}Update Available${idsCL[Default]}" msg="Updating service"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" /usr/bin/git stash >/dev/null 2>&1 /usr/bin/git pull >/dev/null 2>&1 /bin/chown -Rf www-data:www-data /var/www/media.scity.us/html echo -e "${idsCL[Green]}Done${idsCL[Default]}" msg="Restarting NGINX"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" /bin/systemctl restart nginx >/dev/null 2>&1 echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo "$(date) - Updating Service (${usrvc}) : Completed" >> ${MM_LOGFILE} else echo -e "${idsCL[LightCyan]}Update Not Available${idsCL[Default]}" echo "$(date) - Updating Service (${usrvc}) : Update Not Needed" >> ${MM_LOGFILE} fi else if [[ " ${UPDATE_SERVICES[*]} " =~ " ${usrvc} " ]]; then if [ "${MM_UPDATE_REPO[${usrvc}]}" != "" ] && [ "${MM_UPDATE_REPO[${usrvc}]}" != "systemd" ]; then msg="Checking for update"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" if [[ $BETA_SERVICES == *${usrvc}* ]]; then latest_release_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases | jq -r '.[0].assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'` echo "$(date) - Updating Service (${usrvc}) : BETA UPDATEURL = ${latest_release_url}" >> ${MM_LOGFILE} beta="BETA " else latest_release_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases/latest | jq -r '.assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'` echo "$(date) - Updating Service (${usrvc}) : UPDATEURL = ${latest_release_url}" >> ${MM_LOGFILE} beta="" fi latest_ver=`echo ${latest_release_url} | cut -d/ -f8` cver=${usrvc}_VER [ ! -d /opt/${usrvc^} ] && [[ " ${SERVICES_CHECK[*]} " =~ " ${usrvc} " ]] && INSTALLSRVC=true if [ "${INSTALLSRVC}" == "true" ] || [ "${!cver}" != "${latest_ver}" ] || [ "${3}" == "-f" ]; then [ "${3}" == "-f" ] && echo -e "${idsCL[LightCyan]}Update Not Needed, forcing anyway${idsCL[Default]}" || echo -e "${idsCL[Green]}Update Available (${latest_ver} > ${!cver})${idsCL[Default]}" # rm -f ${LOGFOLDER}/update.${usrvc} touch ${LOGFOLDER}/update.${usrvc} if [ "${INSTALLSRVC}" != "true" ]; then # SERVICEBACKUP ${usrvc} msg="Updating Service" else msg="Installing Service" fi c=0; spc=''; spc1=$((${cw_spc}-${#msg})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" if curl --output /dev/null --silent --head --fail $latest_release_url; then tmpfile=/tmp/${usrvc}-update.tar.gz [[ -f ${tmpfile} ]] && rm -f "${tmpfile}" &>/dev/null wget -qO ${tmpfile} $latest_release_url >> ${LOGFOLDER}/update.${usrvc} if [[ ( $? -eq 0 ) && ( -f ${tmpfile} ) ]]; then if [ "${usrvc}" != "bazarr" ]; then rm -Rf /opt/${usrvc^} >> ${LOGFOLDER}/update.${usrvc} mkdir /opt/${usrvc^} >> ${LOGFOLDER}/update.${usrvc} fi if [ "${usrvc}" == "ombi" ]; then /bin/tar xfP ${tmpfile} -C /opt/${usrvc^} >> ${LOGFOLDER}/update.${usrvc} elif [ "${usrvc}" == "bazarr" ]; then /usr/bin/unzip -oqq ${tmpfile} -d /opt/${usrvc^} else /bin/tar xfP ${tmpfile} -C /opt >> ${LOGFOLDER}/update.${usrvc} fi chown -R user:users /opt/${usrvc^} >> ${LOGFOLDER}/update.${usrvc} fi fi rm -f ${tmpfile} &>/dev/null echo -e "${idsCL[Green]}Done${idsCL[Default]}" [ ! -f ${TMPFOLDER}/vers.info ] && touch ${TMPFOLDER}/vers.info [ "${usrvc}" == "radarr" ] && /bin/sed -i /${usrvc}3d/d ${TMPFOLDER}/vers.info /bin/sed -i /${usrvc}/d ${TMPFOLDER}/vers.info echo "${usrvc}_VER=\"${latest_ver}\"" >> ${TMPFOLDER}/vers.info [ "${usrvc}" == "radarr" ] && echo "${usrvc}3d_VER=\"${latest_ver}\"" >> ${TMPFOLDER}/vers.info if [ "${INSTALLSRVC}" != "true" ]; then echo "$(date) - Updating Service (${usrvc}) : Completed" >> ${MM_LOGFILE} # echo -e "${idsCL[Green]}${usrvc^} Service has been updated${idsCL[Default]}" else sudo -iu user mkdir -p /home/user/.config/${usrvc^} >> ${LOGFOLDER}/update.${usrvc} if [ "${ausrvc}" == "lidarr" ] || [ "${ausrvc}" == "radarr" ] || [ "${ausrvc}" == "radarr3d" ] || [ "${ausrvc}" == "prowlarr" ] || [ "${ausrvc}" == "readarr" ]; then rm -f /etc/systemd/system/${usrvc}.service >> ${LOGFOLDER}/update.${usrvc} systemctl daemon-reload >> ${LOGFOLDER}/update.${usrvc} CREATE_SYSTEMD_SERVICE ${usrvc} >> ${LOGFOLDER}/update.${usrvc} systemctl daemon-reload >> ${LOGFOLDER}/update.${usrvc} fi echo "$(date) - Installing Service (${usrvc}) : Completed" >> ${MM_LOGFILE} echo -e "${idsCL[Green]}${usrvc^} Service has been Installed${idsCL[Default]}" fi SERVICEBACKUPSTART ${usrvc} else echo -e "${idsCL[LightCyan]}Update Not Available${idsCL[Default]}" echo "$(date) - Updating Service (${usrvc}) : Update Not Needed" >> ${MM_LOGFILE} fi elif [ "${MM_UPDATE_REPO[${usrvc}]}" == "systemd" ]; then msg="Checking for update"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" [ "${usrvc}" == "qbittorrent" ] && ausrvc="qbittorrent-nox" || ausrvc=${usrvc} if [ "$(apt-get -s upgrade | grep ${ausrvc})" != "" ] || [ "${3}" == "-f" ]; then [ "${3}" == "-f" ] && [ "$(apt-get -s upgrade | grep ${ausrvc})" == "" ] && echo -e "${idsCL[LightCyan]}Update not needed, forcing anyway${idsCL[Default]}" || echo -e "${idsCL[Green]}Update Available${idsCL[Default]}" SERVICEBACKUP ${usrvc} msg="Updating service"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" [ "${3}" == "-f" ] && [ "$(apt-get -s upgrade | grep ${ausrvc})" == "" ] && /usr/bin/apt install --reinstall ${ausrvc} -y >/dev/null 2>&1 || /usr/bin/apt install ${ausrvc} -y >/dev/null 2>&1 systemctl disable ${ausrvc} >/dev/null 2>&1 if [ "${ausrvc}" == "sonarr" ]; then rm -f /lib/systemd/system/${ausrvc}.service >> ${LOGFOLDER}/update.${ausrvc} rm -f /etc/systemd/system/${ausrvc}.service >> ${LOGFOLDER}/update.${ausrvc} CREATE_SYSTEMD_SERVICE ${ausrvc} >> ${LOGFOLDER}/update.${ausrvc} fi echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo "$(date) - Updating Service (${usrvc}) : Completed" >> ${MM_LOGFILE} SERVICEBACKUPSTART ${usrvc} else echo -e "${idsCL[LightCyan]}Update Not Available${idsCL[Default]}" echo "$(date) - Updating Service (${usrvc}) : Update not needed" >> ${MM_LOGFILE} fi fi else echo -e "${idsCL[Red]}Not currently available to update${idsCL[Default]}" fi fi else echo -e "${idsCL[Red]}No service selected for update${idsCL[Default]}" fi rm -f ${TMPFOLDER}/mm.update.stop elif [ "${1}" == "list" ]; then SHOWSRVCUPDATES fi } CREATE_SYSTEMD_SERVICE(){ srvc=${1} [ "${srvc}" == "sonarr" ] && SEXEC="/usr/bin/mono --debug /usr/lib/sonarr/bin/Sonarr.exe" || SEXEC="opt/${srvc^}/${srvc^}" echo -e "[Unit] Description=${srvc^} Daemon After=syslog.target network.target [Service] User=user Group=users Type=simple ExecStart=${SEXEC} -nobrowser -data=/home/user/.config/${srvc^}/ TimeoutStopSec=20 KillMode=process Restart=on-failure [Install] WantedBy=multi-user.target " > /etc/systemd/system/${srvc}.service } SERVICEBACKUP(){ usrvc=${1} if [ "${usrvc}" == "list" ]; then SERVICERESTORE . list elif [ "${usrvc}" == "" ] || [ "${usrvc}" == "true" ]; then echo echo -e "[${idsCL[LightCyan]} 0${idsCL[Default]}]${idsCL[White]} All Services${idsCL[Default]}" b=1; declare -a bsrvcs for bsrvc in "${UPDATE_SERVICES[@]}"; do if [ "${MM_UPDATE_REPO[${bsrvc}]}" != "" ] && [[ " ${SERVICES_CHECK[*]} " =~ " ${bsrvc} " ]]; then bsrvcs[${b}]=${bsrvc} bsrvcd=`echo -n "${bsrvc:0:1}" | tr "[:lower:]" "[:upper:]"`; bsrvcd="${bsrvcd}${bsrvc:1}" bsrvcd=${bsrvcd/Qbittorrent/qBittorent}; bsrvcd=${bsrvcd/3d/3D} [ $b -lt 10 ] && db=" $b" || db=$b echo -e "[${idsCL[LightCyan]}${db}${idsCL[Default]}]${idsCL[White]} ${bsrvcd}${idsCL[Default]}" b=$((b+1)) fi done echo echo -en "${idsCL[LightCyan]}Select a service to backup: ${idsCL[White]}" read selsrvc echo [ ${selsrvc} -eq 0 ] && usrvc=all || usrvc=${bsrvcs[${selsrvc}]} fi if [ "${usrvc}" == "all" ]; then for uasrvc in "${UPDATE_SERVICES[@]}"; do if [ "${MM_UPDATE_REPO[${uasrvc}]}" != "" ] && [[ " ${SERVICES_CHECK[*]} " =~ " ${uasrvc} " ]]; then SERVICEBACKUP ${uasrvc} ${2} ([ "${3}" == "true" ] || [ "${2}" == "true" ]) && SERVICEBACKUPSTART ${uasrvc} fi done elif [ "${usrvc}" != "" ]; then if [ "${3}" == "true" ] || [ "${2}" == "true" ] || [ "${1}" == "true" ]; then echo echo -e "${idsCL[Green]}Backing up ${usrvc^} Service${idsCL[Default]}" DIVIDER . lightGreen fi msg="Stopping Service "; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" /bin/systemctl stop ${usrvc} >/dev/null 2>&1 [ "${usrvc}" == "radarr" ] && /bin/systemctl stop radarr3d >/dev/null 2>&1 echo -e "${idsCL[Green]}Done${idsCL[Default]}" msg="Backing Up Service"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" BACKUPDIR=${MM_BACKUP_FOLDER}/${usrvc} [ ! -d ${BACKUPDIR} ] && mkdir -p ${BACKUPDIR} [ "${usrvc}" == "radarr" ] && [ ! -d ${BACKUPDIR}3d ] && mkdir -p ${BACKUPDIR}3d [ "${MM_SERVICE_APP[$usrvc]}" != "" ] && APPBAKFOLDER=${MM_SERVICE_APP[$usrvc]} || APPBAKFOLDER=/opt/${usrvc^} if [ "${2}" == "-f" ] || ([ ! -f ${BACKUPDIR}/cfg-full.tgz ] && [ "${MM_SERVICE_CFG[${usrvc}]}" != "" ] && [ -d ${MM_SERVICE_CFG[${usrvc}]} ]) || ([ ! -f ${BACKUPDIR}/app-full.tgz ] && [ "${MM_SERVICE_APP[$usrvc]}" != "none" ]) || ([ $(expr `date +%s` - $(stat -c %Y ${LOGFOLDER}/update.${usrvc})) -gt 604800 ] && [ $(expr `date +%s` - $(stat -c %Y ${BACKUPDIR}/fullbackup)) -gt 7776000 ]); then if [ "${MM_SERVICE_APP[$usrvc]}" != "none" ]; then rm -f ${BACKUPDIR}/*app*.* tar -cPpg ${BACKUPDIR}/snapshot-apt.snar -f ${BACKUPDIR}/app-full.tgz ${APPBAKFOLDER} fi if [ "${MM_SERVICE_CFG[${usrvc}]}" != "" ] && [ -d ${MM_SERVICE_CFG[${usrvc}]} ]; then rm -f ${BACKUPDIR}/*cfg*.* tar -cPpg ${BACKUPDIR}/snapshot-cfg.snar -f ${BACKUPDIR}/cfg-full.tgz ${MM_SERVICE_CFG[${usrvc}]} fi touch ${BACKUPDIR}/fullbackup else BACKUPTIME=$(date +%y-%m-%d-%H-%M-%S) if [ "${MM_SERVICE_APP[$usrvc]}" != "none" ]; then tar -cPpg ${BACKUPDIR}/snapshot-apt.snar -f ${BACKUPDIR}/app.${BACKUPTIME}.tgz ${APPBAKFOLDER} fi if [ "${MM_SERVICE_CFG[${usrvc}]}" != "" ] && [ -d ${MM_SERVICE_CFG[${usrvc}]} ]; then tar -cPpg ${BACKUPDIR}/snapshot-cfg.snar -f ${BACKUPDIR}/cfg.${BACKUPTIME}.tgz ${MM_SERVICE_CFG[${usrvc}]} fi fi for BAKFILE in ${BACKUPDIR}/app.*.tgz; do [ ${BACKUPDIR}/app-full.tgz -nt ${BAKFILE} ] && rm -f ${BAKFILE} done for BAKFILE in ${BACKUPDIR}/cfg.*.tgz; do [ ${BACKUPDIR}/cfg-full.tgz -nt ${BAKFILE} ] && rm -f ${BAKFILE} done echo -e "${idsCL[Green]}Done${idsCL[Default]}" ([ "${3}" == "true" ] || [ "${2}" == "true" ] || [ "${1}" == "true" ]) && SERVICEBACKUPSTART ${usrvc} [ "${usrvc}" == "radarr" ] && echo -e "${idsCL[Green]}Radarr3D Backup${idsCL[Default]}" && SERVICEBACKUP ${usrvc}3d ${2} ${3} fi } SERVICEBACKUPSTART(){ usrvc=${1} msg="Starting Service"; c=0; spc=''; spc1=$((${cw_spc}-${#msg})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" OUT=$(ip link | grep "$VPN_INTERFACE") >/dev/null 2>&1 if [ ! -f ${TMPFOLDER}/mm.stop ] && [[ " ${SERVICES_CHECK[*]} " =~ " ${usrvc} " ]] && [ ! -f ${TMPFOLDER}/mm.noservices ] && [ ! -f ${MMFOLDER}/mm.noservices ] && [ ! `find /opt/idssys/mediamanager/ -name 'mm.plexmount.fail*'` ]; then /bin/systemctl disable ${usrvc} >> ${LOGFOLDER}/update.${usrvc} /bin/systemctl start ${usrvc} >> ${LOGFOLDER}/update.${usrvc} [ "${usrvc}" == "radarr" ] && [[ " ${SERVICES_CHECK[*]} " =~ " radarr3d " ]] && /bin/systemctl start radarr3d >> ${LOGFOLDER}/update.${usrvc} echo -e "${idsCL[Green]}Done${idsCL[Default]}" else echo -e "${idsCL[LightCyan]}Skipping${idsCL[Default]}" fi } SERVICERESTORE(){ srvc=${1} if [ "${srvc}" == "" ] || [ "${2}" == "list" ]; then echo b=1; declare -a bsrvcs for bsrvc in ${MM_BACKUP_FOLDER}/*/ ; do bsrvc=${bsrvc%/*}; bsrvc=${bsrvc##*/} bsrvcs[${b}]=${bsrvc} bsrvcd=`echo -n "${bsrvc:0:1}" | tr "[:lower:]" "[:upper:]"`; bsrvcd="${bsrvcd}${bsrvc:1}" bsrvcd=${bsrvcd/Qbittorrent/qBittorent}; bsrvcd=${bsrvcd/3d/3D} [ $b -lt 10 ] && db=" $b" || db=$b echo -e "[${idsCL[LightCyan]}${db}${idsCL[Default]}]${idsCL[White]} ${bsrvcd}${idsCL[Default]}" b=$((b+1)) done echo [ "${2}" == "list" ] && msg='list backups for' || msg='restore' echo -en "${idsCL[LightCyan]}Select a service to ${msg}: ${idsCL[White]}" read selsrvc echo srvc=${bsrvcs[${selsrvc}]} fi if [ "${srvc}" != "" ]; then [ "${2}" != "list" ] && echo -e "${idsCL[LightGreen]}Restoring ${srvc}${idsCL[Default]}\n" BACKUPDIR=${MM_BACKUP_FOLDER}/${srvc} [ -f ${BACKUPDIR}/app-full.tgz ] && fcheck=app || fcheck=cfg fulldate=$(date -r ${BACKUPDIR}/${fcheck}-full.tgz "+%y-%m-%d-%H-%M-%S") IFS='-'; read -a full_date <<< "${fulldate}"; unset IFS if [ ${full_date[3]} -gt 12 ]; then hr=`expr ${full_date[3]} - 12`; ma=pm; else hr=${full_date[3]}; ma=am; fi; [ ${hr} -lt 10 ] && hr=" ${hr}" fulldate="${full_date[1]}/${full_date[2]}/${full_date[0]} @ ${hr}:${full_date[4]}${ma}" echo -e "[${idsCL[LightCyan]} 1${idsCL[Default]}]${idsCL[White]} ${fulldate} - Full Backup${idsCL[Default]}" f=2; declare -a rdates # for bakfile in ${BACKUPDIR}/${fcheck}.*.tgz; do for bakfile in `find ${BACKUPDIR} -name "${fcheck}.*.tgz" -type f -printf "%f\n" | sort -n`; do fname=${bakfile##*/} fdate=${fname#*.}; fdate=${fdate%.*} rdates[${f}]="${fdate}" IFS='-'; read -a f_date <<< "${fdate}"; unset IFS if [ ${f_date[3]} -gt 12 ]; then hr=`expr ${f_date[3]} - 12`; ma=pm; else hr=${f_date[3]}; ma=am; fi; [ ${hr} -lt 10 ] && hr=" ${hr}" fdate="${f_date[1]}/${f_date[2]}/${f_date[0]} @ ${hr}:${f_date[4]}${ma}" [ $f -lt 10 ] && df=" $f" || df=$f echo -e "[${idsCL[LightCyan]}${df}${idsCL[Default]}]${idsCL[White]} ${fdate} - Incremental Backup${idsCL[Default]}" f=$((f+1)) done if [ "${2}" == "list" ]; then echo exit 0; fi echo -en "\n${idsCL[LightCyan]}Select a backup to restore: ${idsCL[White]}" read restore echo if [ "${rdates[${restore}]}" != "" ]; then echo -en "${idsCL[LightGreen]}Restoring Full Backup first ${idsCL[White]}.." tar -cPpf /tmp/cfg-full.tgz ${MM_SERVICE_CFG['readarr']} --checkpoint=.100 echo -e "${idsCL[Green]} Done${idsCL[Default]}\n" for r in ${!rdates[@]}; do IFS='-'; read -a ridate <<< "${rdates[${r}]}"; unset IFS if [ ${ridate[3]} -gt 12 ]; then hr=`expr ${ridate[3]} - 12`; ma=pm; else hr=${ridate[3]}; ma=am; fi; [ ${hr} -lt 10 ] && hr=" ${hr}" rdate="${ridate[1]}/${ridate[2]}/${ridate[0]} @ ${hr}:${ridate[4]}${ma}" echo -en "${idsCL[LightGreen]}Restoring Incremental Backup from ${rdate} ${idsCL[White]}.." tar -cPpf /tmp/cfg-full.tgz ${MM_SERVICE_CFG['readarr']} --checkpoint=.100 echo -e "${idsCL[Green]} Done${idsCL[Default]}\n" [ ${r} -eq ${restore} ] && break done echo -e "\n${idsCL[Green]}Restore to '${idsCL[White]}${rdate}${idsCL[Green]}' complete!${idsCL[Default]}\n" echo "THIS DID NOT RESTORE ANYTHING, ITS STILL IN BETA BITCH!!" fi fi } SHOWSRVCUPDATES(){ echo -e "${idsCL[White]}${idsST[Bold]}Latest Available Service Releases${idsST[Reset]}${idsCL[Default]}" DIVIDER . lightGreen for usrvc in "${UPDATE_SERVICES[@]}"; do if [ "${usrvc}" != "organizr" ]; then if [ "${MM_UPDATE_REPO[${usrvc}]}" == "systemd" ]; then [ "${usrvc}" == "qbittorrent" ] && ausrvc="qbittorrent-nox" || ausrvc=${usrvc} repo_ver=`dpkg -s ${ausrvc} | grep Version` echo -e "${idsCL[LightCyan]}${MM_SERVICES[${usrvc}]}:${idsCL[Default]} Apt Repo Controller" echo -e "${idsCL[LightGreen]}Repo Release : ${repo_ver/Version: /v}${idsCL[Default]}" else beta_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases | jq -r '.[0].assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'` #beta_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases | jq -r '.[0].assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://' | cut -d/ -f8` beta_ver=`echo ${beta_url} | cut -d/ -f8` latest_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases/latest | jq -r '.assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'` latest_ver=`echo ${latest_url} | cut -d/ -f8` [[ $BETA_SERVICES == *${usrvc}* ]] && cur='Beta' || cur='Stable' echo -e "${idsCL[LightCyan]}${MM_SERVICES[${usrvc}]}:${idsCL[Default]} ${cur} Selected" echo -e "${idsCL[LightGreen]}Stable Release : ${latest_ver}${idsCL[Default]}" echo -e "${idsCL[LightGreen]}Beta Release : ${beta_ver}${idsCL[Default]}" fi cver=${usrvc}_VER echo -e "${idsCL[LightCyan]}Current vers. : ${!cver/Version: /v}${idsCL[Default]}" # echo # echo ${latest_url} # echo ${beta_url} echo echo fi done } MMSETUP(){ echo -e "${idsCL[Green]}MM/VPN Manager Setup Scripts${idsCL[Default]}" echo MMSTOP echo -en "${idsCL[LightCyan]}Would you like to configure the hostname and network settings (y/N)? ${idsCL[Default]}" read choice case "$choice" in [Yy]) echo echo -en "${idsCL[LightYellow]}Hostname [Default='${idsCL[White]}Media-Manager${idsCL[LightYellow]}']: ${idsCL[Default]}" read -e HOSTNAME [ "${HOSTNAME}" == "" ] && HOSTNAME="Media-Manager" echo REPIP=`hostname -I` REPIP=${REPIP/ /} echo -en "${idsCL[LightYellow]}IP Address [Current='${idsCL[White]}${REPIP}${idsCL[LightYellow]}']: ${idsCL[Default]}" read -e IPADDRESS [ "${IPADDRESS}" == "" ] && IPADDRESS=${REPIP} echo REPSUB=`/sbin/ifconfig eth0 | awk '/netmask/{ print $4;} '` echo -en "${idsCL[LightYellow]}Subnet Mask [Current='${idsCL[White]}${REPSUB}${idsCL[LightYellow]}']: ${idsCL[Default]}" read -e SUBNET [ "${SUBNET}" == "" ] && SUBNET=${REPSUB} echo REPGW=`route -n | grep 'UG[ \t]' | awk '{print $2}'` echo -en "${idsCL[LightYellow]}Gateway IP [Current='${idsCL[White]}${REPGW}${idsCL[LightYellow]}']: ${idsCL[Default]}" read -e GATEWAY [ "${GATEWAY}" == "" ] && GATEWAY=${REPGW} echo REPPRIDNS=`resolvectl -i eth0 dns | awk '/eth0/{ print $4;} '` echo -en "${idsCL[LightYellow]}Primary DNS [Current='${idsCL[White]}${REPPRIDNS}${idsCL[LightYellow]}']: ${idsCL[Default]}" read -e PRIDNS [ "${PRIDNS}" == "" ] && PRIDNS=${REPPRIDNS} echo REPSECDNS=`resolvectl -i eth0 dns | awk '/eth0/{ print $5;} '` echo -en "${idsCL[LightYellow]}Secondary DNS (optional) [Current='${idsCL[White]}${REPSECDNS}${idsCL[LightYellow]}']: ${idsCL[Default]}" read -e SECDNS [ "${SECDNS}" == "" ] && SECDNS=${REPSECDNS} echo echo echo "Confirm settings:" echo -e "${idsCL[LightCyan]}Hostname : ${idsCL[Green]}${HOSTNAME}${idsCL[Default]}" echo -e "${idsCL[LightCyan]}IP Address : ${idsCL[Green]}${IPADDRESS}${idsCL[Default]}" echo -e "${idsCL[LightCyan]}Subnet Mask : ${idsCL[Green]}${SUBNET}${idsCL[Default]}" echo -e "${idsCL[LightCyan]}Gateway IP : ${idsCL[Green]}${GATEWAY}${idsCL[Default]}" echo -e "${idsCL[LightCyan]}Primary DNS : ${idsCL[Green]}${PRIDNS}${idsCL[Default]}" echo -e "${idsCL[LightCyan]}Secondary DNS : ${idsCL[Green]}${SECDNS}${idsCL[Default]}" echo echo -en "${idsCL[LightCyan]}Is this correct (Y/n)? ${idsCL[Default]}" read -n 1 choice case "$choice" in [Nn]) MMSETUP; exit 0;; esac echo -en "${idsCL[LightCyan]}Making configuration changes ... ${idsCL[Default]}" sudo hostnamectl set-hostname ${HOSTNAME} >/dev/null 2>&1 sudo sed -i '/127.0.1.1/d' /etc/hosts >/dev/null 2>&1 echo "127.0.1.1 ${HOSTNAME} mm" >> /etc/hosts NEWSUB=`IPprefix_by_netmask ${SUBNET}` REPSUB=`IPprefix_by_netmask ${REPSUB}` sed -i "s/${REPIP}\/${REPSUB}/${IPADDRESS}\/${NEWSUB}/g" /etc/netplan/eth0.yaml >/dev/null 2>&1 sed -i "s/${REPIP}\/${REPSUB}/${IPADDRESS}\/${NEWSUB}/g" /etc/netplan/eth0.yaml >/dev/null 2>&1 [ "${SECDNS}" != "" ] && DNS="${PRIDNS},${SECDNS}" || DNS="${PRIDNS}" [ "${REPSECDNS}" != "" ] && REPDNS="${REPPRIDNS},${REPSECDNS}" || REPDNS="${REPPRIDNS}" sed -i "s/${REPDNS}/${DNS}/g" /etc/netplan/eth0.yaml >/dev/null 2>&1 sed -i "s/${REPGW}/${GATEWAY}/g" /etc/netplan/eth0.yaml >/dev/null 2>&1 echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo if [ "$(hostname)" != "${HOSTNAME}" ] || [ "$REPIP" != "$IPADDRESS" ] || [ "$REPSUB" != "$NEWSUB" ] || [ "$REPGW" != "$GATEWAY" ] || [ "$REPDNS" != "$DNS" ]; then NETCHNG=true else NETCHNG=false fi ;; esac echo echo -en "${idsCL[LightCyan]}Do you wish to change enabled services (y/N)? ${idsCL[Default]}" read choice case "$choice" in [Yy]) echo # echo -en "${idsCL[LightCyan]}Resetting config ... ${idsCL[Default]}" # # #sed -i '/^$/d' ${MMFOLDER}/settings.conf # echo -e "${idsCL[Green]}Done${idsCL[Default]}" # if [ "${SERVICESCHECK}" != "" ]; then # echo # srvcs2=${SERVICESCHECK/snap.tautulli.tautulli/tautulli} # srvcs2=${srvcs2/snap.overseerr.daemon/overseerr} # echo -e "${idsCL[LightCyan]}Run previous MM/VPN Services${idsCL[Default]}" # echo -en "${idsCL[LightYellow]}['${idsCL[White]}${srvcs2}${idsCL[LightYellow]}']${idsCL[LightCyan]} (Y/n)? ${idsCL[Default]}" # read -n 1 choice # case "$choice" in # [Nn]) SERVICESCHECK="";; # * ) srvcs=${srvcs2};;esac # fi # if [ "${SERVICESCHECK}" == "" ] && [ "${srvcs}" == "" ]; then export NEWT_COLORS='' srvcs=$(whiptail --title "MM/VPN Manager Service Selector" --checklist --separate-output \ "\nPlease select which services to enable for this system:" 0 70 16 \ "bazarr" "Bazarr (Subtitle Manager)" ON \ "jackett" "Jackett (Torrent Indexing Manager) " OFF \ "lidarr" "Lidarr (Music Manager)" OFF \ "ombi" "Ombi (Download Requester)" OFF \ "overseerr" "Overseerr (Download Requester)" ON \ "prowlarr" "Prowlarr (Torrent Index Manager)" ON \ "qbittorrent" "qBittorrent (Torrent Downloader)" ON \ "radarr" "Radarr (Movie Manager)" ON \ "radarr3d" "Radarr3D (3D Movie Manager)" OFF \ "readarr" "Readarr (Book Manager)" OFF \ "sonarr" "Sonarr (TV Show Manager)" ON \ "tautulli" "Tautulli (Plex Stats)" ON 3>&1 1>&2 2>&3) exitstatus=$?; srvcs="$(join_by , $srvcs)"; # fi echo echo -e "${idsCL[LightCyan]}Selected services: ${idsCL[Green]}${srvcs}${idsCL[Default]}" if [ "${srvcs}" != "" ]; then echo echo -en "${idsCL[LightCyan]}Saving new settings ... ${idsCL[Default]}" `\cp ${MMFOLDER}/settings.conf ${MMFOLDER}/settings.conf.bak` >/dev/null 2>&1 sed -i '/SERVICESCHECK/d' ${MMFOLDER}/settings.conf >/dev/null 2>&1 srvcs=${srvcs/tautulli/snap.tautulli.tautulli} srvcs=${srvcs/overseerr/snap.overseerr.daemon} echo -e "\nSERVICESCHECK=${srvcs}" >> ${MMFOLDER}/settings.conf else echo -e "\n# SERVICESCHECK=" >> ${MMFOLDER}/settings.conf fi echo source ${MMFOLDER}/settings.conf IFS=,; SERVICES_CHECK=(${SERVICESCHECK}); unset IFS echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo CNFG=true ;; esac echo echo -en "${idsCL[LightCyan]}Would you like to configure notications (y/N)? ${idsCL[Default]}" read choice case "$choice" in [Yy]) echo echo echo -e "${idsCL[Cyan]}You may choose to setup notifications by either SMTP and/or Pushover:${idsCL[Default]}" echo if [ "${EMAIL_NOTICE}" != "" ]; then echo -en "${idsCL[LightYellow]}Use previous email address [${idsCL[White]}${EMAIL_NOTICE}${idsCL[LightYellow]}] (Y/n)? ${idsCL[Default]}" read -n 1 choice case "$choice" in [Nn]) EMAIL_NOTICE="";; esac echo fi if [ "${EMAIL_NOTICE}" == "" ]; then echo -e -n "${idsCL[LightYellow]}(optional) Email notification address: ${idsCL[Default]}" read -e EMAIL_NOTICE fi echo if [ "${PUSHOVER_APP_TOKEN}" != "" ]; then echo -en "${idsCL[LightYellow]}Use previous Pushover App Token [${idsCL[White]}${PUSHOVER_APP_TOKEN}${idsCL[LightYellow]}] (Y/n)? ${idsCL[Default]}" read -n 1 choice case "$choice" in [Nn]) PUSHOVER_APP_TOKEN="";; esac echo fi if [ "${PUSHOVER_APP_TOKEN}" == "" ]; then echo -e -n "${idsCL[LightYellow]}(optional) Pushover App Token: ${idsCL[Default]}" read -e PUSHOVER_APP_TOKEN fi echo if [ "${PUSHOVER_APP_TOKEN}" != "" ]; then if [ "${PUSHOVER_USER_TOKEN}" != "" ]; then echo -en "${idsCL[LightYellow]}Use previous Pushover User Token [${idsCL[White]}${PUSHOVER_USER_TOKEN}${idsCL[LightYellow]}] (Y/n)? ${idsCL[Default]}" read -n 1 choice case "$choice" in [Nn]) PUSHOVER_USER_TOKEN="";; esac echo fi if [ "${PUSHOVER_USER_TOKEN}" == "" ]; then echo -e -n "${idsCL[LightYellow]}(optional) Pushover User Token: ${idsCL[Default]}" read -e PUSHOVER_USER_TOKEN fi echo else echo "# PUSHOVER_USER_TOKEN=\"\"" >> ${MMFOLDER}/settings.conf fi echo -en "${idsCL[LightCyan]}Saving new settings ... ${idsCL[Default]}" sed -i '/PUSHOVER_/d' ${MMFOLDER}/settings.conf >/dev/null 2>&1 sed -i '/EMAIL_NOTICE/d' ${MMFOLDER}/settings.conf >/dev/null 2>&1 sed -i '/OPTIONAL/d' ${MMFOLDER}/settings.conf >/dev/null 2>&1 [ "${EMAIL_NOTICE}" != "" ] && echo "EMAIL_NOTICE=${EMAIL_NOTICE}" >> ${MMFOLDER}/settings.conf || echo "# EMAIL_NOTICE=email@address.com" >> ${MMFOLDER}/settings.conf [ "${PUSHOVER_APP_TOKEN}" != "" ] && echo "PUSHOVER_APP_TOKEN=\"${PUSHOVER_APP_TOKEN}\"" >> ${MMFOLDER}/settings.conf || echo "# PUSHOVER_APP_TOKEN=\"\"" >> ${MMFOLDER}/settings.conf [ "${PUSHOVER_USER_TOKEN}" != "" ] && echo "PUSHOVER_USER_TOKEN=\"${PUSHOVER_USER_TOKEN}\"" >> ${MMFOLDER}/settings.conf || echo "# PUSHOVER_USER_TOKEN=\"\"" >> ${MMFOLDER}/settings.conf source ${MMFOLDER}/settings.conf >/dev/null 2>&1 IFS=,; SERVICES_CHECK=(${SERVICESCHECK}); unset IFS echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo CNFG=true ;; esac echo echo -en "${idsCL[LightCyan]}Would you like to setup an SSH mount for Plex Media (y/N)? ${idsCL[Default]}" read choice case "$choice" in [Yy]) if [ "$(dpkg-query -W --showformat='${Status}\n' grepcidr | grep "install ok installed")" == "" ]; then echo echo -en "${idsCL[LightCyan]}Installing requirements ... ${idsCL[Default]}" sudo apt -y install sshfs >/dev/null 2>&1 apt install sshfs -y echo -e "${idsCL[Green]}Done${idsCL[Default]}" fi echo echo -en "${idsCL[LightYellow]}Plex Server IP/Hostname: ${idsCL[Default]}" read -e PLEXSERVER echo echo -en "${idsCL[LightYellow]}File Access Username: ${idsCL[Default]}" read -e PLEXUSER echo echo -en "${idsCL[LightYellow]}File Access Password: ${idsCL[Default]}" read -e PLEXPASSWORD echo echo -en "${idsCL[LightYellow]}Plex Media Folder location [ ex. /srv/media-data ] : ${idsCL[Default]}" read -e PLEXFOLDER echo echo -en "${idsCL[LightCyan]}Verifying local SSH key, otherwise generating one ... ${idsCL[Default]}" if [ ! -f /root/.ssh/id_rsa.pub ]; then echo echo -e "${idsCL[LightYellow]}Generating new key, enter no password, and keep defaults: ${idsCL[Default]}" echo sudo ssh-keygen -t rsa fi echo -e "${idsCL[Green]}Done${idsCL[Default]}" sudo ssh-copy-id ${PLEXUSER}@${PLEXSERVER} echo echo -e "${idsCL[LightCyan]}Configuring and mounting ... ${idsCL[Default]}" if [ "${PLEXSERVER}" != "" ] && [ "${PLEXUSER}" != "" ] && [ "${PLEXPASSWORD}" != "" ] && [ "${PLEXFOLDER}" != "" ]; then sudo sed -i '/\/mnt\/plex-media/d' /etc/fstab sudo echo "sshfs#${PLEXUSER}@${PLEXSERVER}:${PLEXFOLDER} /mnt/plex-media fuse ,_netdev,reconnect,allow_other,umask=002,gid=100,uid=1000,IdentityFile=/root/.ssh/id_rsa 0 0" >> /etc/fstab umount /mnt/plex-media >/dev/null 2>&1 mount -a >/dev/null 2>&1 sleep 2s echo -e "${idsCL[Green]}Done${idsCL[Default]}" echo echo -e "${idsCL[LightCyan]}Verify the following folder structure looks correct:${idsCL[Default]}" ls -l /mnt/plex-media echo -en "${idsCL[LightCyan]}Is the above folder your plex media (Y/n)? ${idsCL[Default]}" read -n 1 choice case "$choice" in [Nn]) echo -e "${idsCL[Magenta]}Missing info, configure mounting to '${idsCL[White]}/mnt/plex-media${idsCL[Magenta]}' manually${idsCL[Default]}";; * ) echo echo -en "${idsCL[LightCyan]}Adding the file 'mounted', to the plex media folder, this file must exist for the system to know its mounted properly ... ${idsCL[Default]}" touch /mnt/plex-media/mounted echo -e "${idsCL[Green]}Done${idsCL[Default]}" ;; esac echo -e "${idsCL[Green]}Your Plex Media is now mounted, and will automatically remount at startup.${idsCL[Default]}" else echo echo -e "${idsCL[Magenta]}Missing info, configure mounting to '${idsCL[White]}/mnt/plex-media${idsCL[Magenta]}' manually${idsCL[Default]}" fi ;; * ) echo -e "${idsCL[Magenta]}Make sure to manually configure mounting to '${idsCL[White]}/mnt/plex-media${idsCL[Magenta]}' ${idsCL[Default]}" ;; esac if [ "${NETCHNG}" == "true" ] || [ "${CNFG}" == "true" ]; then if [ "${NETCHNG}" == "true" ]; then echo echo -en "${idsCL[LightCyan]}A reboot is recommended, would you like to reboot now (Y/n)? ${idsCL[Default]}" read -n 1 choice case "$choice" in [Nn]) echo;; * ) reboot;; esac else echo echo -en "${idsCL[LightCyan]}Would you like to update MM/VPN Services (y/N)? ${idsCL[Default]}" read -n 1 choice case "$choice" in [Yy]) for usrvc in "${UPDATE_SERVICES[@]}"; do [ "${MM_UPDATE_REPO[${usrvc}]}" != "" ] && UPDATESERVICE service ${usrvc} done;; * ) tmp='';; esac echo echo -en "${idsCL[LightCyan]}Would you like to start MM/VPN Services (y/N)? ${idsCL[Default]}" read -n 1 choice echo case "$choice" in [Yy]) MMSTART start;; * ) echo -e "${idsCL[Cyan]}Not starting. To start, run the command '${idsCL[Green]}mm start${idsCL[LightCyan]}'${idsCL[Default]}";echo;echo;; esac fi fi echo echo -e "${idsCL[LightCyan]}The following are MM/VPN Manager (mm) usage commands:${idsCL[Default]}" DIVIDER . lightGreen USAGE # cat ${MMFOLDER}/settings.conf } USAGE(){ echo -e "${idsCL[White]} Usage: ${idsCL[LightCyan]}mm {option}" echo echo -e " ${idsCL[Yellow]}start ${idsCL[Default]}-> Start services" echo -e " ${idsCL[Yellow]}stop ${idsCL[Default]}-> Stop services" echo -e " ${idsCL[Yellow]}restart ${idsCL[Default]}-> Restart services" echo echo -e " ${idsCL[Yellow]}status ${idsCL[Default]}-> Check Status on VPN and Media Services" echo -e " ${idsCL[Yellow]}vpninfo ${idsCL[Default]}-> Show VPN Info" echo -e " ${idsCL[Yellow]}speedtest ${idsCL[Default]}-> Run Speedtest Comparison (with and w/o VPN)" echo echo -e " ${idsCL[Yellow]}backup or all ${idsCL[Default]}-> Backs up specified service, or 'all' will of course backup all services" echo -e " ${idsCL[Yellow]}update {option} ${idsCL[Default]}-> Ran by itself will check for mediamanager updates, or;" echo -e " ${idsCL[LightYellow]} list ${idsCL[Default]}-> List available service releases" echo -e " ${idsCL[LightYellow]} service {${idsCL[Cyan]}${idsCL[LightYellow]} or all} ${idsCL[Default]}-> Update specified service, or all ( -f = force update)" echo echo -e " ${idsCL[Yellow]}cron ${idsCL[Default]}-> Crontab job for maintaining system (run */1 min)" # echo -e " ${idsCL[Yellow]}clear ${idsCL[Default]}-> Clear all tmp MM system files, not normally needed" echo -e " ${idsCL[Yellow]}setup ${idsCL[Default]}-> Setup/Reconfigure (Network/Services/SSHFS Mount)" echo echo if [ "${SERVICESCHECK}" != "" ]; then srvcscheck=${SERVICESCHECK//,/ }; srvcscheck=${srvcscheck/snap.tautulli.tautulli/tautulli}; srvcscheck=${srvcscheck/snap.overseerr.daemon/overseerr} srvcscheck2="" for i in $srvcscheck; do csrv=`echo -n "${i:0:1}" | tr "[:lower:]" "[:upper:]"`; [ "${srvcscheck2}" != "" ] && srvcscheck2="${srvcscheck2}, ${csrv}${i:1}" || srvcscheck2="${csrv}${i:1}"; done srvcscheck2=${srvcscheck2/Qbittorrent/qBittorent}; srvcscheck2=${srvcscheck2/3d/3D} else srvcscheck2="None" fi echo -e "${idsCL[White]} Services Available: ${idsCL[Default]}" echo for srvc in "${AVAIL_SERVICES[@]}"; do if [[ ${srvc} != *"vpn"* ]] && [[ ${srvc} != *"wireguard"* ]]; then sdsp=${srvc/snap.tautulli.tautulli/tautulli} sdsp=${sdsp/snap.overseerr.daemon/overseerr} c=0; spc=''; spc1=$((36-${#sdsp})); until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -e " ${idsCL[Cyan]}${sdsp}${spc}${idsCL[Default]}-> ${MM_SERVICES[${srvc}]}" fi done echo echo echo -e "${idsCL[White]} Services currently enabled on this system: ${idsCL[Default]}(run 'mm setup' to change enabled services)" echo echo -e " ${idsCL[Cyan]}${srvcscheck2}${idsCL[Default]}" echo } TEST(){ /usr/bin/vnstat -t /usr/bin/vnstat -d -b $(date --date="-7 day" +%Y-%m-%d) -e $(date +%Y-%m-%d) /usr/bin/vnstat -d /usr/bin/vnstat -hg } if [ ! -f ${TMPFOLDER}/mm.update.stop ]; then if [ -f ${TMPFOLDER}/mm.stop ]; then stoppedsince=$(expr `date +%s` - $(stat -c %Y ${TMPFOLDER}/mm.stop)) stopmsg="${idsCL[Yellow]}The system has currently been stopped for '$(SHOW_TIME ${stoppedsince})'.\n${idsCL[LightCyan]}(You'll need to run \"mm start\" to start services again)${idsCL[Default]}" fi case $action in clear) echo -e "${idsCL[LightCyan]}Clearing all mm.* files ${idsCL[Default]}"; echo;; cron) MMCRONCHECK ${2};; setup) MMSETUP;; vpninfo) GETVPNINFO;; speedtest) VPNSPEEDTEST;; test) TEST;; stop) MMSTOP FULLSTOP ;; start | check) if [ -f ${TMPFOLDER}/mm.stop ] && [ "${action}" == "check" ]; then if [ "${2}" != "q" ]; then echo -e "${stopmsg}" # [ $VERBOSE = true ] && echo "$(date) - Not checking, system has been stopped" >> ${MM_LOGFILE} echo fi exit 1 elif [ "${VPN_USER}" == "" ] || [ "${VPN_PASS}" == "" ] || [ "${VPN_SERVER}" == "" ]; then echo "$(date) - VPN User Info Missing" >> ${MM_LOGFILE} echo -e "${idsCL[Red]}VPN User Info Missing${idsCL[Default]}" echo exit 1 elif [ ! -f ${TMPFOLDER}/mm.stop ] && [ -f ${TMPFOLDER}/mm.stop.checking ]; then checkstopped=`expr $(date +%s) - $(stat -c %Y ${TMPFOLDER}/mm.stop.checking)` if [ $checkstopped -gt 900 ]; then rm -f ${TMPFOLDER}/mm.stop* # rm -f ${NM_FOLDER}/.tmp/mm.stop* echo "$(date) - Bash script stuck in stop mode after doing a check, will restart" >> ${MM_LOGFILE} SENDNOTICE "MM Script Error" "Bash script stuck in check mode, restarting system" MMSTOP rm -f ${TMPFOLDER}/mm.stop* # rm -f ${NM_FOLDER}/.tmp/mm.stop* getStuckMM=$(pgrep -f '/mm') for i in $getStuckMM; do /bin/kill $i done else exit 1 fi fi [ ! -f ${TMPFOLDER}/mm.stop ] && MMSTART ${action} ${2} [ "${action}" != "status" ] && rm -f ${TMPFOLDER}/mm.stop.checking ;; restart) if [ -f ${TMPFOLDER}/mm.stop ]; then echo -e "${stopmsg}" echo exit 1 fi ([ -f ${TMPFOLDER}/mm.noservices ] || [ -f ${MMFOLDER}/mm.noservices ]) && rv="vpn" || rv="" MMSTOP echo -en "${idsCL[LightCyan]}Pausing for 30s to ensure a new VPN WAN IP, or press any key to continue with potentially getting a new address ... ${idsCL[Default]}" read -et 30 -n 1 echo -e "${idsCL[Green]}Done${idsCL[Default]}" MMSTART ${action} ${rv} ;; status) if [ -f ${TMPFOLDER}/mm.stop ]; then echo -e "${stopmsg}" echo fi MMSTATUS ${2} ;; update) UPDATESERVICE ${2} ${3} ${4};; backup) SERVICEBACKUP ${2} ${3} true;; restore) SERVICERESTORE ${2} ${3};; *) USAGE;; esac else echo -e "${idsCL[Yellow]}Part of the system is currently updating, please try again shortly.\nUpdates usually only take 1-3mins.${idsCL[Default]}" echo fi exit 0