How Hubitat Backup/Restore has Saved My Ass

This is awesome, thanks for sharing this.
I don't need daily backups, but just set this up for Weekly updates on Monday and ran a test run just now and it worked great!

Thank you!

1 Like

I actually took it one step further and wrote a fully self-contained apple script to download the file and move it. Script below for anyone who may find it useful. Just save this as an application and trigger it from a recurring calendar event. Easy.

on run

tell application "Safari"

tell window 1

set current tab to (make new tab with properties {URL:"http://HUB IP/hub/backupDB?fileName=latest"})

delay 5

close current tab

end tell

end tell

tell application "Finder"

set sourceFolder to alias "Macintosh SSD:Users:dworkman:Downloads:"

set targetFolder to alias "Macintosh SSD:Users:dworkman:Documents:Hubitat:"

set latestFile to last item of (sort (files in (sourceFolder)) by modification date)

move latestFile to targetFolder

set movedFile to name of last item of (sort (files in (targetFolder)) by modification date)

display dialog "Hubitat backup is done: " & movedFile

end tell

end run

2 Likes

We've had a few shell scripts and batch files posted here, but I believe this is the first AppleScript posted on the Hubitat community!

3 Likes

Script further enhanced to delete files older than 30 days and display notifications (I was bored today....)

on run
tell application "Safari"
	tell window 1
		set current tab to (make new tab with properties {URL:"http://HUBIP/hub/backupDB?fileName=latest"})
		delay 5
		close current tab
	end tell
end tell
tell application "Finder"
	set sourceFolder to alias "Macintosh SSD:Users:dworkman:Downloads:"
	set targetFolder to alias "Macintosh SSD:Users:dworkman:Documents:Hubitat:"
	set latestFile to last item of (sort (files in (sourceFolder)) by modification date)
	move latestFile to targetFolder
	set movedFile to name of last item of (sort (files in (targetFolder)) by modification date)
end tell

set modDate to (30)

tell application "System Events"
	set fileList to (every item of targetFolder whose modification date is less than ((current date)) - modDate * days)
	set listOfNames to {}
	repeat with i in fileList
		set currentFileName to (the name of i)
		copy currentFileName to end of listOfNames
	end repeat
	set myString to items of listOfNames
	set my text item delimiters to ", "
	text items of listOfNames
	set listSize to count of fileList
end tell


tell application "Finder"
	if listSize is 0 then
		display notification "Hubitat backup complete. File downloaded: " & movedFile & ". No files deleted." with title "Hubitat Backup" sound name "Blow"
	else
		repeat with i in fileList
			delete i
		end repeat
		display notification "Hubitat backup complete. File downloaded: " & movedFile & ". File(s) deleted: " & myString & "." with title "Hubitat Backup" sound name "Blow"
		delay 1
	end if
end tell
end run
1 Like

Can someone with more Linux / UNIX experience than me help me get my backups saved with the same name as the file on the Hubitat hub? I have messed around with some of the CRON scripts from various threads on the community and arrived at a shell script below that saves the backup with the date/time I download it, but I would prefer to take the name from the HE file itself. I'd also prefer to download the backup taken by the hub rather than taking a new one with the "latest" web call. Thanks.

#!/bin/bash
# Script to download hubitat backup

backupdir='/home/pi/heBackups/c4'
he_ipaddr=192.168.0.8

cd $backupdir
ls -1tr | head -n -14 | xargs -d '\n' rm -f --

curl http://$he_ipaddr/hub/backup -o $backupdir/C4_$(date +%Y-%m-%d-%H%M).lzf | grep download | awk -F"=" '{ print $5}' | awk -F'"' '{print $2}' | sed '/^$/d' | tail -1 | xargs -I @curl http://$he_ipaddr/hub//backupDB?fileName=@

I haven't actually run a restore but I just want to confirm that the file size when copying to Windows is only 1.37K? When I download them manually from the hub to my Mac they are 1.4MB. When I download to Windows they are less than 2K? And on the hub they report that they are 1403584 which I assume is bytes - making the Mac download the right size. Confusing.

No, 1.37K is wrong. You should definitely getting at least 500kB.

Edit: sorry, I typed mB, not kB. Corrected this

Restoring from Windows may destroy your Hubitat's setup. Or the hubitat may say, Invalid. Did you use your web browser? If not, maybe your terminal downloader (curl, wget) may be outdated, so try with the latest web browser (chrome, edge, Firefox)

ok I ran a manual download in windows from the hubitat web page in the latest version of chrome and I am getting same as Mac - 1.37MB. Will check the Curl command in a minute in scheduler. But 1.37MB is still nowhere near >500MB?

???

That's not necessarily correct. The size depends on the number of device & apps, and the number of events stored per device.

My backups are 1.0 MB for one Hubitat and 1.7 MB for the other.

That's just fine.

1 Like

@aaiyar I just noticed that I typed mB, not kB. Sorry for the confusion

1 Like

Ok, good. So it is an issue with the terminal, as you are getting the correct size via Chrome

I have seen similar issue where downloading using a powershell script makes the backup much smaller than downloading manually in chrome. I am not able to automate my backups because of this reason. Does anyone have an insight as to why the backups are much smaller when downloading with an automated script?

Thanks for sharing your Apple Script! I have it working but I have to execute it manually because I can't get calendar to execute it. So a couple questions,

Does this look right?

Also is there a way to have it reboot the hb after the back-up? I saw this command but don't know how to include it,

curl -X POST http://$he_ipaddr/hub/reboot

Thanks

@douglaspitman I don't know how to use AppleScript, but this Automator calendar action backs up and reboots the hub.
https://www.dropbox.com/sh/zx53vrlkle7bxv3/AADloP2Ef651sUbMDx3H9jRxa?dl=0
Note that this doesn't work with login security, and all hubitat web page windows have to be closed for the reboot to work. Remember to open It in Automator and set your HubIP and destination folder. Then, you can add it to calendar. I would still recommend using the hubitat reboot app that installs right on the hubitat, such as this:

Shouldn't you change the "calendar" box to the AppleScript file?

You are correct, I should have posted this shot...

I was hoping to not add more apps to my hubs. I was running the apple script and letting time machine back it up to a NAS.

I attempted to get my Rpi working but it was getting way to far in the weeds for me to figure it out, and I struggled to gets my NAS to automatically mount on the pi.

the AppleScript worked so I was executing the script manually. I have had to reboot one of my hubs a few times lately so I thought I would try to add that to the AppleScript...

@douglaspitman

You could try this

Make sure the script is saved as an Application, and then select that Application as the file that is opened in the calendar event:

HB App

Screen Shot 2021-01-27 at 3.37.46 PM

I was able to get it working correctly. I added a reboot command to the AppleScript, what Im unsure of is it really a good practice to reboot a hub daily?

1 Like

@douglaspitman I started rebooting the hub daily and I noticed faster automations vs letting the hub run for a few weeks unrebooted, and the cache is removed, making backups a tad bit smaller.