{"id":355,"date":"2018-06-20T12:53:21","date_gmt":"2018-06-20T16:53:21","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=355"},"modified":"2023-02-08T17:13:27","modified_gmt":"2023-02-08T22:13:27","slug":"automated-hass-io-backups-recovering-from-disaster","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/automated-hass-io-backups-recovering-from-disaster\/","title":{"rendered":"Automated Hass.io Backups + Recovering From Disaster"},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/QxluSm4a06A\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"><\/iframe><\/p>\n<p><strong>Automated Hassio and Node-RED Backups<\/strong><\/p>\n<p>Today on the hookup we\u2019re going to talk about one of the most important things in the IT world, backups, and how to manage them automatically so you won\u2019t have to think about them!<\/p>\n<p>Backups are not a sexy subject, but they are really important.&nbsp; Whether it\u2019s a hard drive crash, a flash memory failure, or just carelessly deleting or saving over an important file , at some point we\u2019ve all been burned by not having a recent backup of our work.&nbsp; The best backup systems have three things in common: they happen often, and they happen automatically, and they are redundant, meaning it\u2019s best to have a backup of your backups.&nbsp; Today we\u2019re going to set up some automations in node red and home assistant that check all of those boxes.<\/p>\n<p>Lets talk first about backing up your backups.&nbsp; The most common way I\u2019ve seen people back up their home assistant configurations is using github.&nbsp; I\u2019m not a huge fan of this solution, and here\u2019s why: Github is public, any files that you upload can be viewed by anyone on the internet.&nbsp; This is great for the main purpose of github, which is a place to publish your open source code, but it\u2019s not great when you might have sensitive credentials in your files.&nbsp; Correct use of the secrets.yaml and git.ignore files is absolutely crucial to ensuring you don\u2019t publish your sensitive data for the entire world to see.&nbsp; You should know that there are nefarious bots combing github constantly searching for sensitive information that people accidentally uploaded\u2026 if it hits github servers even for a few seconds, you should assume that your sensitive information has been compromised.<\/p>\n<p>I also don\u2019t like that when backing up to github there is no local copy of the backup, aside from the one actually on the home assistant drive.&nbsp; I\u2019m fairly positive that github isn\u2019t going to just randomly shut down their servers, but as a reoccurring theme on my channel you might have noticed that if at all possible I like to keep things local.<\/p>\n<p>Instead of github, I would highly recommend that you store your backups on dropbox.&nbsp; Not only does dropbox give you a local copy of your files, but it is also cannot be accessed publicly on the internet.&nbsp; If you\u2019re already using dropbox and you don\u2019t want to waste your storage space on your home assistant backups, there\u2019s no reason you can\u2019t sign up for another dropbox account and use the free 2GB to store your backups.&nbsp; While you\u2019re at it, you might as well use my dropbox referral link in the description to create your new account so I can get some of that sweet sweet free storage.<\/p>\n<p>&nbsp;<\/p>\n<p>Lets walk through the process of creating a new dropbox account and linking it to your hassio instance.&nbsp; The first thing you need to do is add Daniel welch\u2019s addon repository to your hassio addons page.&nbsp; That link is in the description.&nbsp; Once you\u2019ve done that you\u2019ll have the option to install the hassio dropbox sync addon, go ahead and click install, and then we\u2019ll head on over to dropbox to create a new account<\/p>\n<p>(don\u2019t forget to use my referral link from the description).<\/p>\n<p>Enter your email address and choose a password to sign up.&nbsp; It will bring you to a screen to ask you to download the dropbox program, but you don\u2019t need to.&nbsp; Just click on the dropbox icon in the upper left to get to your web interface.&nbsp; On the next screen you can just hit \u201cend tour\u201d.&nbsp; Then click on the face icon in the upper right corner and select settings.&nbsp; Here you\u2019ll need to verify your email so you can qualify for a developer key.<\/p>\n<p>Next you\u2019ll need to navigate to the developer console for dropbox by going to this url <a href=\"https:\/\/www.dropbox.com\/developers\/apps\">https:\/\/www.dropbox.com\/developers\/apps<\/a> which is also posted in the description.&nbsp; Click on create app.<\/p>\n<p>On this next page you need to select dropbox API, name your app and decide whether you want to give it full access to your dropbox.&nbsp; Since I created a brand new dropbox account for this I chose just to give it full access.&nbsp; Otherwise it will create a folder inside a new folder in your drobox called apps, and your configuration will be slightly different.<\/p>\n<p>Click on agree to terms and press create app.<\/p>\n<p>Next you\u2019ll see a page with a bunch of options that you don\u2019t need to touch.&nbsp; All you need on this page is to generate an access token, click that button and copy your token.<\/p>\n<p>Next we\u2019ll head back to our dropbox sync addon and put in our access token and also tell it which folder to backup to.&nbsp; I made a folder called hassio backups.&nbsp; You can also set a \u201ckeep last\u201d parameter here, which will control how many snapshots will be kept locally on your SD card.<\/p>\n<p>(finish this using screenshots)<\/p>\n<p>Now that we\u2019ve completed our addon\u2019s config section we need to click the start button to activate it.&nbsp; It\u2019s important to note that clicking start doesn\u2019t actually sync your backups, to do this, you need to run a service call\u2026 more on that in a minute.<\/p>\n<p>Hassio has a great feature that allows us to create and name snapshots by calling a service.&nbsp; You\u2019ll recall that one of the most important parts about a good backup system is that they are automated.&nbsp; We\u2019re going to use node-red to handle this automation.&nbsp; Here\u2019s what the flow looks like.<\/p>\n<p>To start, we need to schedule how often we want this backup to happen.&nbsp; To do this I\u2019m using one of my favorite node red custom nodes, the light scheduler node\u2026 I\u2019ll include that package name in the description so you can add it using palette manager.&nbsp; I chose once a week, specifically on Monday since the weekend is typically when I\u2019m messing with my home assistant setup.&nbsp; After running through a switch node that filters out only the ON payload I send it into a service call that begins a hassio snapshot formatted with the current date.<\/p>\n<p>Because the snapshot is far from an instant process I\u2019ve added a conservative delay time of 1 hour from the creation of the snapshot to the firing of the service to upload our new backup files to dropbox.&nbsp; Once the service is called your dropbox sync addon does it\u2019s thing and uploads your new snapshot to your dropbox.&nbsp; Now we have a great backup system that we never need to think about\u2026 sort of.<\/p>\n<p>There is one downside to this system that I haven\u2019t been able to solve yet:&nbsp; On my instance of home assistant each snapshot is around 225 megabytes, which means that I\u2019ll only be able to store 8 full snapshots on dropbox before I need to go in and delete them.&nbsp; The dropbox sync addon will delete old local copies of your snapshots if you want it to using the \u201ckeep last\u201d parameter in the configuration, but it can\u2019t touch the files in your dropbox.&nbsp; This means every 2 months you\u2019ll need to manually delete old files from your dropbox like a caveman.<\/p>\n<p>Let me stop you there past Rob.&nbsp; Did you ever stop to question why each backup is 300 megabytes?&nbsp; I certainly haven\u2019t written 300 megabytes of code, in fact, all the important time consuming parts of my home assistant configuration are text files.&nbsp; So why are my backups 300 megabytes?<\/p>\n<p>Examining the archived backup file revealed some interesting problems with my home assistant setup, and maybe with yours.&nbsp; Located within your config folder is a file called home assistant v2 (dot) db.&nbsp; This is your home assistant database file where event and state changes are stored.&nbsp; How big is yours? Mine was a hulking 250 megabyes on a system that was only a few weeks old.&nbsp; Now, you may want this massive record of your home\u2019s events and states, but for the most part I couldn\u2019t care less.&nbsp; All I want is to have a 24 hour history graph and the ability to restore previous states after a reboot.<\/p>\n<p>To prevent your database from storing ALL of your home\u2019s happenings for all eternity you need to add a few things to your configuration file.<\/p>\n<p>The recorder component determines which events and states get added to your database, and also controls how long they stay there.<\/p>\n<p>By adding purge keep days, and purge interval we can modify how many days of information are kept in the database.&nbsp; Using purge interval 1 my home assistant will prune my database file every day, and by using purge keep days 1 it will only keep the last 24 hours of states when a purge is called.&nbsp; This means at any point I have between 24 and 48 hours of events and states in my database.&nbsp; This keeps the file at a much more reasonable 10 megabyes, which is acceptable to me.<\/p>\n<p>The second culprit was node-red.&nbsp; If you\u2019re using the notorious BDG version of this addon when you take a snapshot backup of it you are actually producing a backup of the install, not of your flows.&nbsp; Your configuration and flows for node red are actually located in your share folder, which we can backup using a partial snapshot by adding the share folder to our partial snapshot<\/p>\n<p>If you\u2019re using Francks version of Node RED your files are located in the config folder and will automatically be backed up when the homeassistant folder is included in your partial snapshot.<\/p>\n<p>By making these few small changes we should be able to get our snapshots down in the 15 megabyte range, which means our 2 gigabytes of dropbox space should last nearly 2.5 years before we need to manually delete old back up file.&nbsp; Hopefully we\u2019ll never have to use these files, but now we have a weekly, automatic, redundant backup that we can depend on if we do need them.<\/p>\n<p>The process of restoring your home assistant is actually pretty straight forward.&nbsp; There\u2019s an option to restore a partial snapshot in hassio, but I\u2019d actually recommend a different method.&nbsp; All you need to do is install a fresh copy of hassio on your sd card (I would recommend having a second SD card so you can keep your original installation intact in case you need to go back to it), go into your hassio menu and install your various addons including the samba share addon so you can access your folders from your PC.<\/p>\n<p>Once you\u2019ve got samba added, go ahead and replace the contents of your new config folder with the config folder from your backup image (a dot tar file can be opened just like a zip file using winrar).&nbsp; And if you use node-red make sure the addon is stopped and replace the contents of your node-red folder either in \/share if you\u2019re using the Notorious BDG version or \/config if you\u2019re using Franck\u2019s version and restart home assistant.<\/p>\n<p>Once your home assistant restarts you should be 100% back up and working.&nbsp; I tried this out on my own system and was able to go from a blank SD card to a fresh home assistant install with my exact same configuration in less than 90 minutes, which seems like a decently long time until you realize that about 70 minutes of that was waiting for hassio and various addons to install.<\/p>\n<p>I know no one wants to spend their precious time messing around with a backup system, but for me imagining the pain of reconfiguring my home assistant from scratch gives me serious nightmares.&nbsp; Hopefully you were able to get some useful information from this video.&nbsp; Next week we\u2019ll be back to building stuff.&nbsp; If you enjoyed this video, please consider subscribing, and as always, thanks for watching to the hookup.<\/p>\n<h3>Links<\/h3>\n<p>Dropbox referral link: <a href=\"https:\/\/db.tt\/iDkPuSSn6Q\">https:\/\/db.tt\/iDkPuSSn6Q<\/a><br \/>\nDaniel Welch&#8217;s addon repository: <a href=\"https:\/\/github.com\/danielwelch\/hassio-addons\">https:\/\/github.com\/danielwelch\/hassio-addons<\/a><br \/>\nDropbox developer console: <a href=\"https:\/\/www.dropbox.com\/developers\/apps\">https:\/\/www.dropbox.com\/developers\/apps<\/a><br \/>\nLight Scheduler: <a href=\"https:\/\/www.npmjs.com\/package\/node-red-contrib-light-scheduler\">https:\/\/www.npmjs.com\/package\/node-red-contrib-light-scheduler<\/a><br \/>\nNode-RED Backup Flow: <a href=\"https:\/\/github.com\/thehookup\/backups\/blob\/master\/backup_flow.txt\">https:\/\/github.com\/thehookup\/backups\/blob\/master\/backup_flow.txt<\/a><\/p>\n<p>Follow me on Twitter: @TheHookUp1<br \/>\nSupport my channel:<br \/>\nPatreon:&nbsp;<a href=\"https:\/\/www.patreon.com\/thehookup\">https:\/\/www.patreon.com\/thehookup<\/a><\/p>\n<p>Music by www.BenSound.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Automated Hassio and Node-RED Backups Today on the hookup we\u2019re going to talk about one of the most important things in the IT world, backups, and how to manage them automatically so you won\u2019t have to think about them! Backups are not a sexy subject, but they are really important.&nbsp; Whether it\u2019s a hard drive [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2607,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-355","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"acf":[],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/comments?post=355"}],"version-history":[{"count":4,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":2362,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/355\/revisions\/2362"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2607"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}