UniFi interface.

UniFi Cloud Backup

BrianSnelgrove - February 9, 2020
Posted Under: Administration
Your Ubiquiti UniFi Controller is probably already set to make automatic backups. Why not copy them off the server automatically for safekeeping in the event of a hardware failure?

 

Unless otherwise noted, all directions are for Debian based systems. Most steps will work for other distributions but some commands may need modifications.
Do you have your UniFi Controller backing up automatically, right?

Log into your UniFi Controller -> Settings (lower left corner) -> Backup -> Enable Auto Backup -> On. That was easy. It also gives you warm a warm fuzzy feeling knowing that your controller configuration is backed up in case something goes wrong, right? What if your hard drive crashes? What if the server burns down? Oh my...

Why not automagically copy your backups off-site?

After all, it is the responsible thing to do. You already have 1/2 of your life saved to the cloud somewhere and, if you followed along with Chris Sherwood at Crosstalk Solutions, you have your UniFi controller in the cloud. Lets put one more piece of your valuable data in one more place where you don't have control of it and rely on the "black box" to keep it safe, secure, and backed up...

Now that we have that out of the way let's get started!

Dropbox seems to be a reasonable place to start. It looks like everyone from individuals to large corporations are trusting their data to them, why shouldn't we? Go set up an account, or try to remember what your password is to your current account, I will wait.

Back already? That didn't take long. Now that you have your account information let's install a command-line utility so we can send files to Dropbox. The basic outline for these directions came from AddictiveTips.com. Notice I said basic, they prominently say NOT to run the installer as root or with sudo rights, we promptly ignored those directions and did it anyway! But there is a good reason, the backup files we want need root permissions to access, and we will not be using the dropbox-uploader process under our normal account. Once we know it works, it will be done by a cron job - running as root - automatically. Let's open a terminal and proceed, shall we?

# get root access
sudo -i
# make sure git is installed
apt update && apt install git -y
# change directories and get the repo
cd /usr/local/bin
git clone https://github.com/andreafabrizi/Dropbox-Uploader.git
# make the file executable
chmod +x Dropbox-Uploader/dropbox_uploader.sh
# run the install/configuration
./Dropbox-Uploader/dropbox_uploader.sh

At this point, you should be given some directions on how to set up the Dropbox. Copy/paste the URL into your browser and set up your Dropbox app. Give your app name something creative, but remember it, you will need it later. In this example, I used unifi-uploader - any place you see unifi-uploader you will need to use your app name.

Now we need a script to do the work for us. I considered putting this on GitHub, but it is short enough that I don't think it would be worthwhile. Note: some of the following lines may wrap - when you put them in the script, they should be on a single line. 

nano /usr/local/bin/UniFiBackup.shl

And enter or copy/paste the following into the new file.

#!/bin/bash
# how many files should we leave in DropBox?
FILESTOLEAVE="15";
# the UniFi autobackup directory
UNIFIDIR="/var/lib/unifi/backup/autobackup";
# get the last backup file in the directory
FILE=$(ls -lt "$UNIFIDIR" | grep .unf | head -n 1 | awk '{print $NF}');
# upload the last backup file - if the file exists it will be skipped
/usr/local/bin/Dropbox-Uploader/dropbox_uploader.sh upload "$UNIFIDIR/$FILE" "/$HOSTNAME/$FILE";
# get a list of files that are in the DropBox folder
/usr/local/bin/Dropbox-Uploader/dropbox_uploader.sh list "$HOSTNAME" | grep .unf | awk '{print $NF}' > file.out;
# count the files that are in the DropBox folder
FILECOUNT=$(wc -l < file.out);
# see if we have more files than we want to leave in DropBox
if [ "$FILECOUNT" -gt "$FILESTOLEAVE" ]; then
 # loop through the current DropBox files
 for i in $(cat file.out); do
  # if our count is less than or equal to the number we want to keep break out of the loop
  if (( "$FILECOUNT" <= "$FILESTOLEAVE" )); then
   break;
  fi
  # decrement the number of files in DropBox
  FILECOUNT=$((FILECOUNT - 1));
  # remove the current file
  /usr/local/bin/Dropbox-Uploader/dropbox_uploader.sh delete "/$HOSTNAME/$i";
 done
fi

When you are done CTRL+O to save the file then CTRL+X to exit nano. Now make the file executable.

chmod +x /usr/local/bin/UniFiBackup.shl

We are ready to take the new script for a test drive and see if it works.

/usr/local/bin/UniFiBackup.shl

You should see a few lines about uploading a file to DropBox and the process unceremoniosly ends. Log into DropBox and you should see a folder structure similar to: Apps -> unifi-uploader (or whatever you named your app) -> your.servers.hostname -> autobackup_5.12.35.YYYMMDD_HHMM_somenumbers.unf. Everything worked, lets make the process automagic with cron.daily.

nano /etc/cron.daily/UniFiBackup

Enter the following two lines.

#!/bin/bash
/usr/local/bin/UniFiBackup.shl

Save the file (CTRL+O) and exit (CTRL+X) then make the cron file executable.

chmod +x /etc/cron.daily/UniFiBackup

The script should run every day and try to uplaod the latest automatic UniFi backup. Dropbox limits the amount of space for free accounts, and I have a free account, so we dont want all of our storage eaten up by UniFi Controller backups, we use the FILESTOLEAVE variable to detemrine how many backups to leave. In my case I get weekly backups; if I have to go back in time more than 15 weeks for something I screwed up I have bigger problems...

Resources for this guide:

If you made it this far you already know about Ubiquiti and their UniFi line, there is no need in giving them an additional shout-out. You should also be somewhat familiar with Linux, I use CentOS at work but prefer Ubuntu at home, since that is what you *should* be installing the UniFi Controller on. Surely you have heard of Dropbox, right?


Discussion - all postings are moderated