Update zigbee2mqtt-v5.sh

- Revise the update process so that if the `git pull` fails, it will execute `git stash` followed by `git pull`.
- Keep the update process verbose.
This commit is contained in:
tteckster 2023-02-03 17:39:18 -05:00 committed by GitHub
parent 08efe35850
commit b2da4b0cd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 26 deletions

View File

@ -335,40 +335,57 @@ function update_script() {
clear clear
header_info header_info
cd /opt/zigbee2mqtt cd /opt/zigbee2mqtt
msg_info "Checking for Backup Directory" #!/bin/bash
stop_zigbee2mqtt() {
if which systemctl 2> /dev/null > /dev/null; then
echo "Shutting down Zigbee2MQTT..."
sudo systemctl stop zigbee2mqtt
else
echo "Skipped stopping Zigbee2MQTT, no systemctl found"
fi
}
start_zigbee2mqtt() {
if which systemctl 2> /dev/null > /dev/null; then
echo "Starting Zigbee2MQTT..."
sudo systemctl start zigbee2mqtt
else
echo "Skipped starting Zigbee2MQTT, no systemctl found"
fi
}
set -e
if [ -d data-backup ]; then if [ -d data-backup ]; then
echo "ERROR: Backup directory exists. May be previous restoring was failed?" echo "ERROR: Backup directory exists. May be previous restoring was failed?"
echo "1. Save 'data-backup' and 'data' dirs to safe location to make possibility to restore config later." echo "1. Save 'data-backup' and 'data' dirs to safe location to make possibility to restore config later."
echo "2. Manually delete 'data-backup' dir and try again." echo "2. Manually delete 'data-backup' dir and try again."
exit 1 exit 1
fi fi
msg_ok "No Backup Directory Exists"
msg_info "Stopping Zigbee2MQTT" stop_zigbee2mqtt
systemctl stop zigbee2mqtt
msg_ok "Stopped Zigbee2MQTT"
msg_info "Creating Backup of Configuration" echo "Creating backup of configuration..."
cp -R data data-backup cp -R data data-backup || { echo "Failed to create backup."; exit 1; }
msg_ok "Created Backup of Configuration"
msg_info "Updating Zigbee2MQTT" echo "Initiating update"
git pull &>/dev/null if ! git pull; then
msg_ok "Updated Zigbee2MQTT" echo "Update failed, temporarily storing changes and trying again."
git stash && git pull || (echo "Update failed even after storing changes. Aborting."; exit 1)
fi
msg_info "Installing Dependencies" echo "Acquiring necessary components..."
npm ci &>/dev/null npm ci || { echo "Failed to install necessary components."; exit 1; }
msg_ok "Installed Dependencies"
msg_info "Restoring Configuration" echo "Restoring configuration..."
cp -R data-backup/* data cp -R data-backup/* data || { echo "Failed to restore configuration."; exit 1; }
rm -rf data-backup
msg_ok "Restored Configuration"
msg_info "Starting Zigbee2MQTT" rm -rf data-backup || { echo "Failed to remove backup directory."; exit 1; }
systemctl start zigbee2mqtt
msg_ok "Started Zigbee2MQTT" start_zigbee2mqtt
msg_ok "Update Successful"
echo "Done!"
exit exit
} }
clear clear