Why an External Drive?
The Raspberry Pi is a great piece of tech and it comes at a great price. The down side of being as cheap as possible is the fact that there is no on board memory, so the Pi utilizes SD cards for the OS. SD cards are pretty cheap nowadays, but do not like being written too very often. When SD cards are written on too much, the flash wears out until the card become corrupted and/or unusable. Home Assistant uses a database to store all events that occur so you have a record of everything that has happened in your home, which is great, but tends to cause many writes to occur on the SD card. To get around this, we can move all of the settings, databases, and other file off of the SD card and put them on an external drive. These steps will also help in the future when we want to start adding addition docker containers that might also do lots of writing.
Step 1: Where are the current files?
If you followed my tutorial on how to install Hass.io using docker, then these steps will apply to you. We need to take a look into what the installer script actually did so we can figure out how to change it. The installer script is meant to be generic and function for many different platforms, which causes the script to be rather long and hard to read, but around the middle, we see the creation of a JSON file containing three elements that are of interest to us:
The data element is the piece we are interested in, as that will be the location that the docker image will be looking for. Searching the script for the values of the variables above, we find the following:
The JSON file itself can be found at “/etc/hassio.json” and the Data folder can be found at “/usr/share/hassio”. Now that we have the location of both the current data folder and the config to change it, we can move on.
Step 2: Moving Existing Data to the External drive
In a previous post, I went over how to mount an external drive to the Pi and create a filesystem on it. Now we want to take advantage of the increased storage capacity and longevity of the drive. The first thing we want to do is move the current data folder to the external drive. I used the “cp” command to make a duplicate of this folder, making sure to be at the root directory first:
cd ../.. sudo cp -r /usr/share/hassio /mnt/hassio
This command copies the data folder and puts it into a folder called “hassio” onto the hassio mount point that I created for the disk in the previous post. If you mounted the disk to another location, just change the folder name. Note: This just makes a copy of the current state of the folder, the existing one will still exists if you make a mistake and need to go back.
Step 3: Changing Config to New Location
Finally, we want to edit the JSON file we saw earlier to point to the new location we just made. We can once again use “nano” to edit this file in the command line:
sudo nano etc/hassio.json
Inside the file, we can edit the “data” parameter to match the new location we created earlier.
Save and exit. To check if our changes took effect, we need to reboot the Pi:
Step 4: Verify the changes
Once the Pi comes back online, we want to verify that Hass.io is using the new location. This can be done by verifying the files are being updated on the external drive. Let the system run for a few minutes to ensure the database was written to recently and run this command:
sudo ls -l /mnt/hassio/hassio/homeassistant
Once again making sure the path matches your setup, you should see somthing similar:
The two files that should be updated are the “home-assistant.log” and “home-assistant_v2.db”. Either of these files should be within a couple minutes of your current time. To make even further sure that its correct, wait a few minutes and run the command again and see that the time has updated. If the time is never updating, go back to the JSON file and verify the path is correct and reboot the Pi once more.
Now that we have successfully moved all of the files used by HomeAssistant off of the SD card and on to an external drive, we have just greatly increased the longevity of the SD card. One thing that going through all this setup is showing, even if you backup all of your configs, preferences and data, it would be a huge pain to have to set all of this work we are doing again in the case of a failed SD card. Speaking of backing things up, in a future post I will go over how to set the Pi up to automatically backup all of our settings and data, so look forward to that soon.