Skip to content

Instantly share code, notes, and snippets.

@pR0Ps
Last active September 24, 2022 19:10
Show Gist options
  • Save pR0Ps/c35e5306a2bf4147c6fbab55b137daaf to your computer and use it in GitHub Desktop.
Save pR0Ps/c35e5306a2bf4147c6fbab55b137daaf to your computer and use it in GitHub Desktop.
Update a UniFi Controller installation on Alpine Linux
#!/bin/sh
####################################
# Update a UniFi Controller installation on Alpine Linux
#
# Assumes that the unifi service can be manipulated with "rc-service unifi <start/stop>"
#
# For initial installation see https://wiki.alpinelinux.org/wiki/UniFi_Controller
# Additional steps:
# - apk add --no-cache libc6-compat
# - ln -s /lib64/ld-linux-x86-64.so.2 /lib/ld-linux-x86-64.so.2
# Configuration options:
unifi_owner="unifi:unifi"
unifi_dir=~unifi
mongod_bin=/usr/bin/mongod
backup_file=~/unifi-data-$(date +%Y-%m-%d_%H%M%S).tar.gz
#####################################
set -eu
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT INT TERM
update_file="$tmp/UniFi.unix.zip"
prompt(){
_prompt="n"
printf "%s [y/n] " "$1"
read -r _prompt
if ! [ "$_prompt" = "y" ]; then
return 1
fi
return 0
}
download(){
curr="$(cat "$unifi_dir/data/db/version")"
echo "Installed version: $curr"
data="$(curl -Ssf 'https://fw-update.ubnt.com/api/firmware?filter=eq~~product~~unifi-controller&filter=eq~~platform~~unix&filter=eq~~channel~~release&sort=-version&limit=1' | jq -r '._embedded.firmware[0]')"
new=$(echo "$data" | jq -r '[.version_major, .version_minor, .version_patch] | join(".")' )
echo "Latest version: $new"
if [ "$curr" = "$new" ]; then
echo "No update available - exiting"
exit 0
fi
if ! prompt "Apply update?"; then
exit 1
fi
echo "Downloading..."
wget "$(echo "$data" | jq -r '._links.data.href')" -O "$update_file"
}
backup(){
echo "Backing up data to $backup_file"
tar czf "$backup_file" -C "$unifi_dir/data" .
}
restore(){
echo "Restoring data from $backup_file"
if [ -e "$unifi_dir/data" ]; then
rm -rf "$unifi_dir/data"
fi
mkdir -p "$unifi_dir/data"
tar xzf "$backup_file" -C "$unifi_dir/data/"
}
update(){
echo "Applying update"
unzip -q "$update_file" -d "$tmp"
rm -rf "$unifi_dir"
mv "$tmp/UniFi" "$unifi_dir"
chown "$unifi_owner" "$unifi_dir"
chmod o-rwx "$unifi_dir"
rm "$unifi_dir/bin/mongod"
ln -s "$mongod_bin" "$unifi_dir/bin/mongod"
}
# Program start
download
rc-service unifi stop
backup
update
restore
rc-service unifi start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment