Skip to content

Instantly share code, notes, and snippets.

@corny
Last active September 11, 2025 08:03
Show Gist options
  • Save corny/3cf7af0f6cb7a00adeb3 to your computer and use it in GitHub Desktop.
Save corny/3cf7af0f6cb7a00adeb3 to your computer and use it in GitHub Desktop.

Revisions

  1. corny revised this gist Mar 29, 2015. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions unifi-backup.sh
    Original file line number Diff line number Diff line change
    @@ -20,16 +20,13 @@ mkdir -p $output
    curl_cmd="curl --cookie /tmp/cookie --cookie-jar /tmp/cookie --insecure --silent --fail"

    # authenticate against unifi controller
    result=`$curl_cmd --data "login=login" --data "username=$username" --data "password=$password" $baseurl/login -v 2>&1 | grep "HTTP/1.1 302 Found" || true`

    # successfully authenticated?
    if [ -z "$result" ]; then
    echo login failed
    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"}' $baseurl/api/cmd/system | sed -n 's/.*\(\/dl.*unf\).*/\1/p'`
    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
    @@ -39,3 +36,5 @@ $curl_cmd $baseurl/logout

    # delete outdated backups
    find $output -ctime +$keep_days -type f -delete

    echo Backup successful
  2. corny revised this gist Jan 17, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion unifi-backup.sh
    Original file line number Diff line number Diff line change
    @@ -37,5 +37,5 @@ $curl_cmd $baseurl$path -o $output/$filename
    # logout
    $curl_cmd $baseurl/logout

    # Delete outdated backups
    # delete outdated backups
    find $output -ctime +$keep_days -type f -delete
  3. corny revised this gist Jan 17, 2015. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions unifi-backup.sh
    Original file line number Diff line number Diff line change
    @@ -12,14 +12,17 @@ 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
    result=`$curl_cmd --data "login=login" --data "username=$username" --data "password=$password" $baseurl/login -v 2>&1 | grep "HTTP/1.1 302 Found" || true`

    # successfully authenticated?
    if [ -z "$result" ]; then
    echo login failed
    exit 1
    @@ -34,5 +37,5 @@ $curl_cmd $baseurl$path -o $output/$filename
    # logout
    $curl_cmd $baseurl/logout

    # Backups löschen, die älter als 24 Stunden sind
    find $output -ctime +1 -type f -delete
    # Delete outdated backups
    find $output -ctime +$keep_days -type f -delete
  4. corny created this gist Jan 17, 2015.
    38 changes: 38 additions & 0 deletions unifi-backup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!/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

    mkdir -p $output

    curl_cmd="curl --cookie /tmp/cookie --cookie-jar /tmp/cookie --insecure --silent --fail"

    # authenticate against unifi controller
    result=`$curl_cmd --data "login=login" --data "username=$username" --data "password=$password" $baseurl/login -v 2>&1 | grep "HTTP/1.1 302 Found" || true`

    if [ -z "$result" ]; 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"}' $baseurl/api/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

    # Backups löschen, die älter als 24 Stunden sind
    find $output -ctime +1 -type f -delete