#!/bin/bash -e # # Improved backup script for Ubiquiti UniFi controller # original source: http://wiki.ubnt.com/UniFi#Automated_Backup # # must contain: # username= # password= source ~/.unifi-backup baseurl=https://localhost:8443 output=/root/backups/unifi filename=`date +%Y%m%d%H%M`.unf keep_days=1 # create output directory mkdir -p $output curl_cmd="curl --cookie /tmp/cookie --cookie-jar /tmp/cookie --insecure --silent --fail" # authenticate against unifi controller if ! $curl_cmd --data '{"username":"'$username'","password":"'$password'"}' $baseurl/api/login > /dev/null; then echo Login failed exit 1 fi # ask controller to do a backup, response contains the path to the backup file path=`$curl_cmd --data 'json={"cmd":"backup","days":"-1"}' $baseurl/api/s/default/cmd/system | sed -n 's/.*\(\/dl.*unf\).*/\1/p'` # download the backup to the destinated output file $curl_cmd $baseurl$path -o $output/$filename # logout $curl_cmd $baseurl/logout # delete outdated backups find $output -ctime +$keep_days -type f -delete echo Backup successful