#!/bin/sh # To run from Internet: # sudo sh -c "$(curl -s https://apps.control2net.com/apt/install-dingo-lorawan-server)" DIRECTORYBACKUP="/home/pi/goiot-backups" DIRECTORYBACKUP_OLD="/opt/GoIoT/backup" DIRECTORYTMP="/home/pi/download" FILENAME=$DIRECTORYBACKUP"/lns-db-and-config-"`date +%y%m%d_%H%M%S`.tar.gz FILE="/opt/dingo-lorawan-server/dingo-lorawan-server.js" echo "Check Node.js environment..." nodejs=$(nodejs --version 2>&1 | grep v) if [ -z "$nodejs" ]; then #echo "Install Node.js first. Instructions:" #echo " sudo su" #echo " curl -fsSL https://deb.nodesource.com/setup_16.x | bash -" #echo " apt-get install -y nodejs" echo "Install Node.js 16.x..." sudo curl -fsSL https://deb.nodesource.com/setup_16.x | bash - sudo apt-get install -y nodejs else nv=$(echo $nodejs | tr -d "v" | tr -d ".") if [ "$nv" -lt "16150" ]; then echo "Install Node.js 16.x" sudo curl -fsSL https://deb.nodesource.com/setup_16.x | bash - sudo apt-get install -y nodejs fi fi echo "\e[32mNode.js environment is ready\e[0m" echo # Create working folder if not exist if [ ! -d "$DIRECTORYTMP" ]; then mkdir $DIRECTORYTMP chmod 0755 $DIRECTORYTMP fi if [ -f "$FILE" ]; then echo "DINGO LORAWAN SERVER IS ALREADY INSTALLED." echo "Doing upgrade..." echo "Stop LNS service..." sudo systemctl stop dingo-lorawan-server.service echo "e[32mLNS service stopped successfully\e[0m" # To have the database for sure closed, run this SQL sudo sqlite3 /opt/dingo-lorawan-server/db/dingo-lorawan-server.db 'SELECT * FROM db_revision;' if [ $(ls /opt/dingo-lorawan-server/db/dingo-lorawan-server.db-* 2>/dev/null | wc -l) -gt 0 ]; then echo "\033[0;31mDATABASE IS OPEN!!" echo "Can not take databse backup because" echo "a temporary database file(s) exist." echo "Can not continue with install!!\033[0m" exit fi echo "Uninstall obsolete modules..." cd /opt/dingo-lorawan-server sudo npm uninstall @goiot/goiot echo "\e[32mObsolete modules uninstalled\e[0m" echo echo "Copy database and config file to temp folder $DIRECTORYTMP""..." sudo cp -p /opt/dingo-lorawan-server/server.json $DIRECTORYTMP sudo cp -p /opt/dingo-lorawan-server/db/dingo-lorawan-server.db $DIRECTORYTMP echo "\e[32mDone copying server.json and dingo-lorawan-server.db to folder" echo "$DIRECTORYTMP to be later restored\e[0m" echo echo "Backup to folder $DIRECTORYBACKUP""..." if [ ! -d "$DIRECTORYBACKUP" ]; then mkdir $DIRECTORYBACKUP chmod 0755 $DIRECTORYBACKUP fi #cp -p /opt/dingo-lorawan-server/server.json $DIRECTORYBACKUP/server.json.`date +%Y%m%d%H%M%S` #cp -p /opt/dingo-lorawan-server/db/dingo-lorawan-server.db /opt/GoIoT/backup/dingo-lorawan-server.db.`date +%Y%m%d%H%M%S` # Instead compress to a .tar.gz file sudo tar -czf $FILENAME \ -C /opt/dingo-lorawan-server server.json \ -C /opt/dingo-lorawan-server/db dingo-lorawan-server.db if [ $? -eq 0 ]; then echo "\e[32mBackup successfull to $FILENAME""\e[0m" else echo "\033[0;31mBackup failed, patch upgrade will exit!!\033[0m" exit fi echo echo "Upgrade DINGO LoRaWAN Server..." cd $DIRECTORYTMP wget https://apps.control2net.com/apt/lorawan/dingo-lorawan-server-1.0.0.zip sudo unzip -o -q dingo-lorawan-server-1.0.0.zip -d /opt echo " Return the files that were backed-up..." sudo cp -p "$DIRECTORYTMP/server.json" /opt/dingo-lorawan-server/ sudo cp -p "$DIRECTORYTMP/dingo-lorawan-server.db" /opt/dingo-lorawan-server/db/ echo " Backed-up files restored successfully" # 250607 ÞJ: Removed as now the whole node_module folder is copied from the development machine as the latest valid & tested # echo "Install modules..." # cd /opt/dingo-lorawan-server # npm install node-machine-id # npm install enum # npm install neo-blessed # npm install blessed # npm install blessed-contrib # npm i --save gps-time # npm install json-multi-parse echo " Restart DINGO-Stack will take care of LoRaWAN Server db-updates if any..." sudo systemctl restart dingo.service echo " DINGO-Stack will restarted. Check for any SQL errors in /opt/GoIoT/DinGo/dingo.log" # 221220 TJ, Commented out. LoraWan server has to be manually restarted # This is done to be sure all DB-updates from the command above are finished #echo "Start service..." #sudo systemctl restart dingo-lorawan-server.service else echo "Installing DINGO LoRaWAN Server..." cd $DIRECTORYTMP wget https://apps.control2net.com/apt/lorawan/dingo-lorawan-server-1.0.0.zip sudo unzip -q dingo-lorawan-server-1.0.0.zip -d /opt echo " Setup service..." cd /opt/dingo-lorawan-server/systemd sudo cp -p dingo-lorawan-server.service /lib/systemd/system sudo systemctl daemon-reload echo " Service setup finished" echo "Start service...." sudo systemctl enable dingo-lorawan-server.service sudo systemctl restart dingo-lorawan-server.service echo "Delete obsolete stuff" rm -r /opt/dingo-lorawan-server/node_modules/@goiot/ fi echo " Delete old obfusucated files on wrong location by Danjal..." sudo rm /opt/dingo-lorawan-server/dev-time.js sudo rm /opt/dingo-lorawan-server/dev-status.js sudo rm /opt/dingo-lorawan-server/end-device_trans_params.js sudo rm /opt/dingo-lorawan-server/link-check.js sudo rm /opt/dingo-lorawan-server/link-adr.js sudo rm /opt/dingo-lorawan-server/service_adr.js sudo rm /opt/dingo-lorawan-server/WorkStuff.js sudo rm /opt/dingo-lorawan-server/downlinks.js sudo rm /opt/dingo-lorawan-server/downlink.js sudo rm /opt/dingo-lorawan-server/entity.js sudo rm /opt/dingo-lorawan-server/get_uid.js sudo rm -r /opt/dingo-lorawan-server/node_modules/@goiot/ echo " Old obfusucated files deleted" echo " Clean up..." rm dingo-lorawan-server-1.0.0.zip sudo rm server.json sudo rm dingo-lorawan-server.db echo " Clean up finished" sudo grep -q -F 'LORAWAN_SERVER_DB=' /etc/profile.d/dingo.sh || sudo sed -i -e '$i \# Refers to the DINGO LORAWAN SERVER Database location.\nexport LORAWAN_SERVER_DB="/opt/dingo-lorawan-server/db/"\n' /etc/profile.d/dingo.sh sudo grep -q -F 'LORAWAN_SERVER_HOME=' /etc/profile.d/dingo.sh || sudo sed -i -e '$i \# Refers to the DINGO LORAWAN SERVER home directory.\nexport LORAWAN_SERVER_HOME="/opt/dingo-lorawan-server/"\n' /etc/profile.d/dingo.sh sudo grep -q -F 'LORAWAN_SERVER_SERVICE=' /etc/profile.d/dingo.sh || sudo sed -i -e '$i \# Refers to the service that controls the DINGO LORAWAN SERVER\nexport LORAWAN_SERVER_SERVICE="dingo-lorawan-server.service"\n' /etc/profile.d/dingo.sh echo "\e[32mFINISHED\e[0m" echo echo "Use these commands to control the service:" echo " sudo systemctl status dingo-lorawan-server.service (show service status)" echo " sudo systemctl start dingo-lorawan-server.service (start the service)" echo " sudo systemctl stop dingo-lorawan-server.service (stop the service)" echo " sudo systemctl restart dingo-lorawan-server.service (restart the service)" echo " sudo systemctl enable dingo-lorawan-server.service (service starts after reboot)" echo " sudo systemctl disable dingo-lorawan-server.service (service does not start after reboot)" echo " cd /opt/dingo-lorawan-server && sudo node dingo-lorawan-server.js (start server directly, not as service)" echo " journalctl -f -u dingo-lorawan-server.service (show service log)" echo