Curl backup script not working? [SOLVED]

Why is this curl script not working? It just prints out the html from a hubitat page on the command line and doesn't do anything else. The variables are defined but obviously not included here.

curl -k -c $cookiefile -d "username=$he_login" -d "password=$he_passwd" https://$he_ipaddr/login

curl -k -sb $cookiefile https://$he_ipaddr/hub/backupDB?fileName=latest -o "$backupdir/Hubitat_$(date +%Y-%m-%d-%H%M).lzf"

Do you have login enabled on your hub?

That script you're using looks like part of one that I created a while ago.

Yes, probably is the same script as I got it elsewhere in the forums. I do have "Hub Login Security" checked on the "Hub Login Security" settings screen if that's what you're asking.

Here's the original. You should remove the quotation marks.

#!/bin/bash
#
he_login=your_he_login
he_passwd=your_he_passwd
he_ipaddr=your_he_ip_addr
cookiefile=`/bin/mktemp`
backupdir=/opt/hubitat/hubitat-backup
backupfile=$backupdir/$(date +%Y%m%d-%H%M).lzf
#
find $backupdir/*.lzf -mtime +5 -exec rm {} \;
curl -k -c $cookiefile -d username=$he_login -d password=$he_passwd https://$he_ipaddr/login
curl -k -sb $cookiefile https://$he_ipaddr/hub/backupDB?fileName=latest -o $backupfile
rm $cookiefile
1 Like

Ok, that seemed to do it, except that the file has a size of 0 bytes?

Try doing it without using https first and make sure it works.

i.e. you don't need to create a cookie or use -k for an insecure connection.

Same 0 byte result. I used
curl http://$he_ipaddr/hub/backupDB?fileName=latest -o $backupfile

You do still need to specify your username/password ...... (unless you disabled Hub login security).

Still 0 bytes with
curl -d username=$he_login -d password=$he_passwd http://$he_ipaddr/hub/backupDB?fileName=latest -o $backupfile

Would I need to escape any special characters in the password you think?

Or create a new password that doesn't have special characters

password with special characters was the culprit. Thanks for your help!

1 Like

For folks who are finding this and still looking for a solution:
Couple quick edits to that backup script --
use this to get the find command to work (my Synology needed this version, but it should work almost anywhere): find "$backupdir" -name "*.lzf" -mtime +10 -exec rm {} \;

(If you're nervous about that rm command, first change the rm to ls -l and run it to see what files it finds.)

And my backupdir has a space in the name, so I needed a bunch of " around things. Here's my updated version:

#!/bin/bash
# Script to download hubitat backup
he_login="XXX" #This is the username you use to log into the hub
he_passwd="XXX" #This is the password you use to log into the hub
he_ipaddr="XXX" #This is the host name or IP of the hub
backupdir='/volume1/Dir With Spaces/HubitatBackups'
cookiefilename="/cookiefile.txt"
cookiefile=$backupdir$cookiefilename
backupfile=$backupdir/LVRM_$(date +%Y%m%d-%H%M).lzf

#Delete Files more than 10 days old
find "$backupdir" -name "*.lzf" -mtime +10 -exec rm {} \;

# Fetch login page with user/pass to get a session cookie
curl -k -c "$cookiefile" -d username=$he_login -d password="$he_passwd" https://$he_ipaddr/login

# Now get the actual backupfile sending the session cookie
curl -k -sb "$cookiefile" https://$he_ipaddr/hub/backupDB?fileName=latest -o "$backupfile"

# remove the session cookie file
rm "$cookiefile"

If you're having issues, you can always get debugging output by bash -x scriptname