Pull Backup File

Noticed that my backup-pull script (which Im sure I found in these archives) was not working. I think it was because of a change to the LOGIN flow as well as lisitng backups.

This scripts requests a new backup from Hubitat, such that it doesnt pull the most recent, but generates a new one.

If someone could reference an update to pull the most recent, would appreciate it, but this works

#!/bin/bash

set -e

he_login="LOGIN"
he_passwd="PASSWORD"
he_ipaddr="IP.ADDRESS"

cookiefile="/tmp/hubitat.cookie"
sessionfile="/tmp/hubitat.cookie.session"
archive_dir="/PATH/TO/BACKUPS"

# Remove old cookies if they exist
rm -f $cookiefile $sessionfile

# cleanup backups (10 days)
touch $archive_dir/temp.lzf
find $archive_dir/*.lzf -mtime +10 -exec rm {} \;

# Step 1: Fetch login page to get CSRF token
curl -k -c $cookiefile -s https://$he_ipaddr/login | grep -o 'name="_csrf" value="[^"]*' | cut -d '"' -f3

# Step 2: Log in using CSRF token and store session cookies separately
curl -k -c $sessionfile -b $cookiefile -d "username=$he_login" -d "password=$he_passwd" -d "_csrf=$csrf_token" https://$he_ipaddr/login

# Debug: Verify successful authentication by checking session cookies
if ! grep -q "HUBSESSION" $sessionfile; then
    echo "Error: Login failed, session cookie not found."
    exit 1
fi

# Step 3: Generate a backup file
curl -k -sb $sessionfile "https://$he_ipaddr/hub/backupDB?fileName=latest" -o "$archive_dir/Hubitat_$(date +%Y-%m-%d-%H%M).lzf"

# Clean up cookie files
rm -f $cookiefile $sessionfile

echo "Backup completed."
1 Like

This bash script downloads the most recent backup:

2 Likes

This was basically the original version I had, and was both failing login and failing getting the list. However I see you found a different endpoint localBackups .. will give this a shot and see if it resolves my earlier issues.

Thank you

It’s @chowell’s work, so all credit goes to him.

The script has been updated over time, so if you haven’t refreshed your running version of the code in the last year or two, try that?

2 Likes

This is fantastic! Thanks @marktheknife
Made a quick mod to preserve the original filename but the login and list filter all worked

1 Like