-
-
Save corny/3cf7af0f6cb7a00adeb3 to your computer and use it in GitHub Desktop.
#!/bin/bash -e | |
# | |
# Improved backup script for Ubiquiti UniFi controller | |
# original source: http://wiki.ubnt.com/UniFi#Automated_Backup | |
# | |
# must contain: | |
# username=<username> | |
# password=<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 |
Nice - thanks for sharing, definitely fills a need.
So the UniFi controllers are Unix or Linux based devices? (I'm trying to help my organization figure out an automated backup system for theirs)
@Dan-Stafford run on Linux and Windows https://www.ubnt.com/download/unifi/
Great script. Workes awesome.
But since at least a month the curl command that triggers the backup doesnt return a path anymore.
So it later just downloads an empty file instead of the backup.
Has anyone the same problem?
This is for Controller Version 5.12.35.0
So apparently the endpoints changed.
The endpoint to trigger a backup is now cmd/backup
This (LN29):
path=`$curl_cmd --data 'json={"cmd":"backup","days":"-1"}' $baseurl/api/s/default/cmd/system | sed -n 's/.*\(\/dl.*unf\).*/\1/p'`
should be changed to this:
path=`$curl_cmd --data '{"cmd":"backup","days":"-1"}' $baseurl/api/s/default/cmd/backup | sed -n 's/.*\(\/dl.*unf\).*/\1/p'`
I also had to remove the json=
from the POST data
This is for Controller Version
5.12.35.0
So apparently the endpoints changed.
The endpoint to trigger a backup is now
cmd/backup
This (LN29):
path=`$curl_cmd --data 'json={"cmd":"backup","days":"-1"}' $baseurl/api/s/default/cmd/system | sed -n 's/.*\(\/dl.*unf\).*/\1/p'`
should be changed to this:
path=`$curl_cmd --data '{"cmd":"backup","days":"-1"}' $baseurl/api/s/default/cmd/backup | sed -n 's/.*\(\/dl.*unf\).*/\1/p'`
I also had to remove the
json=
from the POST data
Thanks for your comment.
I have errors with a powershell scripts and with Chrome I noticed that the endpoint trigger changed.
But the script I'm using is still not working.. If you can take a look on this snippet please :
# login $Response = Invoke-RestMethod ` -Method Post ` -Uri "$BaseURI/api/login" ` -Body $( @{ username=$Username; password=$Password } | ConvertTo-Json ) ` -SessionVariable 'UniFiSession' If ( $Response.meta.rc -ne 'ok' ) { Write-Error "LOGIN ERROR" -ErrorAction Stop } # generate the backup file $Response = Invoke-RestMethod ` -Method Post ` -WebSession $UniFiSession ` #-Uri "$BaseURI/api/s/default/cmd/system" ` -Uri "$BaseURI/api/s/default/cmd/backup" ` -Body $( @{ cmd='backup'; days=$BackupDays } | ConvertTo-Json ) # -Body $( @{ cmd='async-backup'; days=$BackupDays } | ConvertTo-Json ) # download the backup file Invoke-WebRequest ` -Uri "$BaseURI$($Response.data.url)" ` -WebSession $UniFiSession ` -OutFile "$BackupLocation\$Server`_$(Get-Date -f $BackupTimeStampFormat).unf" # logout $Response = Invoke-WebRequest ` -Uri "$BaseURI/logout" ` -WebSession $UniFiSession #EOF
Thanks
I tried your script on my controller and with that code i got a response with a file to download
$Response = Invoke-RestMethod `
-Method Post `
-WebSession $UniFiSession `
-Uri "$BaseURI/api/s/default/cmd/backup" `
-Body $( @{ cmd='backup'; days=$BackupDays } | ConvertTo-Json )
in your snippet you got the Body
twice. So maybe that was the problem?
Oh the double body line was a mistake with the copy/paste.. I modified the quote above.
But it's not working for me.
Is this a backup file you can download or an html file around 100 kB ?
I got a link to a backup file and used your later command to download it.
Was a file with about 300KB.
Looked like a backup file (had just random noice in it. Did not try to restore it)
I don't understand, for me it's an html file or no download. Can you tell me how you test please ? I miss something I think. Thanks.
- installed powershell V6 (V6 adds
-SkipCertificateCheck
that i had to use because i dont have setup certs for my controller) - open powershell
- set stuff like
$BaseURI
and$Username
to my values - execute command by command
will keep working on it, but right now, this just produces a 0KB backup from my UDMP when run from a linux machine. Clearly something isn't working. =/ I did modify endpoint as mentioned in this discussion.
will keep working on it, but right now, this just produces a 0KB backup from my UDMP when run from a linux machine. Clearly something isn't working. =/ I did modify endpoint as mentioned in this discussion.
I had the same problem on one of my controllers. Problem is that the user you are using to connect must be a Super Administrator.
I had the user set to just Administrator and i got 0Kb files.
Careful!
This script can result in deleting a lot of unwanted files!
# delete outdated backups
find $output -ctime +$keep_days -type f -delete
should be ammended with
# delete outdated backups
find $output -ctime +$keep_days -type f -delete -iname "*.unf"
Very Cool Script.
Is there a script to restore this configuration download?