Skip to content

Instantly share code, notes, and snippets.

@brianredbeard
Forked from stevejenkins/upgrade_unifi.sh
Last active September 21, 2016 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianredbeard/bc9142f844d4f53a1ff2b1fde19fee32 to your computer and use it in GitHub Desktop.
Save brianredbeard/bc9142f844d4f53a1ff2b1fde19fee32 to your computer and use it in GitHub Desktop.
Easy UniFi Controller Upgrade Script for Unix/Linux Systems
#!/bin/sh
# upgrade_unifi.sh
# Easy UniFi Controller Upgrade Script for Unix/Linux Systems
# by Steve Jenkins (stevejenkins.com)
# Version 2.0
# Last Updated July 2, 2016
# REQUIREMENTS
# 1) Assumes you already have any version of UniFi Controller installed
# and running on your system.
# 2) Assumes a user named "ubnt" owns the /opt/UniFi directory.
# 3) Requires a service start/stop script to properly shut down and
# restart the UniFi controller before and after upgrade. I've written
# compatible startup scrips for SysV and systemd systems at
# http://wp.me/p1iGgP-2wl
# 4) Requires wget command to fetch the software from UBNT's download site.
# USAGE
# Modify the "UNIFI_DOWNLOAD_URL" variable below using the full URL of
# the UniFi Controller zip file on UBNT's download site. Optionally modify
# any of the additional variables below (defaults should work fine),
# then run the script!
# CONFIGURATION OPTIONS
UNIFI_VERSION=${UNIFI_VERSION:-5.0.7}
UNIFI_DOWNLOAD_URL=${UNIFI_DOWNLOAD_URL:-http://dl.ubnt.com/unifi/${UNIFI_VERSION}/UniFi.unix.zip}
UNIFI_ARCHIVE_FILENAME=${UNIFI_ARCHIVE_FILENAME:-UniFi.unix.zip}
UNIFI_OWNER=${UNIFI_OWNER:-ubnt}
UNIFI_PARENT_DIR=${UNIFI_PARENT_DIR:-/opt}
UNIFI_DIR=${UNIFI_DIR:-/opt/UniFi}
UNIFI_BACKUP_DIR=${UNIFI_BACKUP_DIR:-/opt/UniFi_bak}
TEMP_DIR=${TEMP_DIR:-/tmp}
# Create progress dots function
show_dots() {
while ps $1 >/dev/null ; do
printf "."
sleep 1
done
printf "\n"
}
# Let's DO this!
printf "Upgrading UniFi Controller...\n"
# Retrieve the updated zip archive from UBNT (overwriting any previous version)
printf "\nDownloading %s from UBNT..." "$UNIFI_DOWNLOAD_URL"
cd $TEMP_DIR || exit
wget -qq $UNIFI_DOWNLOAD_URL -O $UNIFI_ARCHIVE_FILENAME &
show_dots $!
# Check to make sure we have a downloaded file to work with
if [ -f "$UNIFI_ARCHIVE_FILENAME" ]; then
# Archive file exists, extract and install it
# Stop the local UniFi Controller service
printf "\n"
service UniFi stop
# Remove previous backup directory (if it exists)
if [ -d "$UNIFI_BACKUP_DIR" ]; then
printf "\nRemoving previous backup directory...\n"
rm -rf $UNIFI_BACKUP_DIR
fi
# Move existing UniFi directory to backup location
printf "\nMoving existing UniFi Controller directory to backup location...\n"
mv $UNIFI_DIR $UNIFI_BACKUP_DIR
# Extract new version
printf "\nExtracting downloaded software..."
unzip -qq $TEMP_DIR/$UNIFI_ARCHIVE_FILENAME -d $UNIFI_PARENT_DIR &
show_dots $!
# Jump into the backup directory
cd $UNIFI_BACKUP_DIR || exit
# Create an archive of the existing data directory
printf "\nBacking up existing UniFi Controller data..."
tar zcf $TEMP_DIR/unifi_data_bak.tar.gz data/ &
show_dots $!
# Extract the data into the new directory
printf "\nExtracting UniFi Controller backup data to new directory..."
tar zxf $TEMP_DIR/unifi_data_bak.tar.gz -C $UNIFI_DIR &
show_dots $!
# Enforce proper ownership of UniFi directory
chown -R $UNIFI_OWNER:$UNIFI_OWNER $UNIFI_DIR
# Restart the local UniFi Controller service
printf "\n"
service UniFi start
# All done!
printf "\nUpgrade of UniFi Controller complete!\n"
exit 0
else
# Archive file doesn't exist, warn and exit
printf "\nUniFi Controller software not found! Please check download link.\n"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment