Update zigbee2mqtt.sh

This commit is contained in:
CanbiZ 2025-01-04 10:07:52 +01:00 committed by GitHub
parent 11a3d9a850
commit 90ee41307b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/develop/misc/build.func)
# Copyright (c) 2021-2024 tteck
# Author: tteck (tteckster)
# Copyright (c) 2021-2025 tteck
# Author: tteck (tteckster) | Co-Author: MickLesk
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
# Source: https://www.zigbee2mqtt.io/
@ -28,95 +28,44 @@ function update_script() {
header_info
check_container_storage
check_container_resources
if [[ ! -d /opt/zigbee2mqtt ]]; then
msg_error "No ${APP} Installation Found!"
exit
fi
if [[ "$(node -v | cut -d 'v' -f 2)" == "18."* ]]; then
if ! command -v npm >/dev/null 2>&1; then
if ! command -v pnpm >/dev/null 2>&1; then
echo "Installing NPM..."
apt-get install -y npm >/dev/null 2>&1
echo "Installed NPM..."
fi
fi
cd /opt/zigbee2mqtt
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
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 "2. Manually delete 'data-backup' dir and try again."
exit 1
if [[ ! -d /opt/zigbee2mqtt ]]; then
msg_error "No ${APP} Installation Found!"
exit
fi
RELEASE=$(curl -s https://api.github.com/repos/Koenkk/zigbee2mqtt/releases/latest | grep "tag_name" | awk '{print substr($2, 2, length($2)-3) }')
if [[ ! -f /opt/${APP}_version.txt ]] || [[ "${RELEASE}" != "$(cat /opt/${APP}_version.txt)" ]]; then
msg_info "Stopping Service"
systemctl stop zigbee2mqtt
msg_ok "Stopped Service"
stop_zigbee2mqtt
msg_info "Creating Backup"
mkdir -p /opt/z2m_backup
tar -czf /opt/z2m_backup/${APP}_backup_$(date +%Y%m%d%H%M%S).tar.gz -C /opt zigbee2mqtt &>/dev/null
mv /opt/zigbee2mqtt/data /opt/z2m_backup
msg_ok "Backup Created"
echo "Generating a backup of the configuration..."
cp -R data data-backup || {
echo "Failed to create backup."
exit 1
}
msg_info "Updating ${APP} to v${RELEASE}"
cd /opt
wget -q "https://github.com/Koenkk/zigbee2mqtt/archive/refs/tags/${RELEASE}.zip"
unzip -q ${RELEASE}.zip
mv zigbee2mqtt-${RELEASE} /opt/zigbee2mqtt
rm -rf /opt/zigbee2mqtt/data
mv /opt/z2m_backup/data /opt/zigbee2mqtt
echo "Checking if any changes were made to package-lock.json..."
git checkout package-lock.json || {
echo "Failed to check package-lock.json."
exit 1
}
echo "Initiating update..."
if ! git pull; then
echo "Update failed, temporarily storing changes and trying again."
git stash && git pull || (
echo "Update failed even after storing changes. Aborting."
exit 1
)
msg_info "Starting Service"
systemctl start zigbee2mqtt
msg_ok "Started Service"
echo "${RELEASE}" >/opt/${APP}_version.txt
else
msg_ok "No update required. ${APP} is already at v${RELEASE}."
fi
echo "Acquiring necessary components..."
npm ci || {
echo "Failed to install necessary components."
exit 1
}
echo "Building..."
npm run build || {
echo "Failed to build new version."
exit 1
}
echo "Restoring configuration..."
cp -R data-backup/* data || {
echo "Failed to restore configuration."
exit 1
}
rm -rf data-backup || {
echo "Failed to remove backup directory."
exit 1
}
start_zigbee2mqtt
echo "Done!"
exit
}
@ -127,4 +76,4 @@ description
msg_ok "Completed Successfully!\n"
echo -e "${CREATING}${GN}${APP} setup has been successfully initialized!${CL}"
echo -e "${INFO}${YW} Access it using the following URL:${CL}"
echo -e "${TAB}${GATEWAY}${BGN}http://${IP}:9442${CL}"
echo -e "${TAB}${GATEWAY}${BGN}http://${IP}:9442${CL}"