Backup Configuration to a USB Drive

:warning: This guide has been migrated from our website and might be outdated. Feel free to edit this guide to update it, and to remove this message after that.

This will step you through the process of setting up a backup of your Home Assistant configuration to a USB device. This is a good method if you don’t want to mask all of your passwords since the backup is kept locally at your home/residence.

:warning: This guide is aimed at users running Home Assistant Core in a Python Virtual Environment

Requirements

First, you need a USB drive. It should be formatted properly for your device and connected to your device before beginning. Any type of partition will work, but Linux filesystems are preferred so that you can set permissions.
Once connected you want to mount the drive. To find the path where it is located, you can use the dmesg command.

# dmesg | grep sd
[    0.909712] sdhci: Secure Digital Host Controller Interface driver
[    0.916414] sdhci: Copyright(c) Pierre Ossman
[    0.923366] sdhost: log_buf @ bac07000 (fac07000)
[    0.989001] mmc0: sdhost-bcm2835 loaded - DMA enabled (>1)
[    1.049095] sdhci-pltfm: SDHCI platform and OF driver helper
[726257.743301] sd 0:0:0:0: Attached scsi generic sg0 type 0
[726259.184810] sd 0:0:0:0: [sda] 124846080 512-byte logical blocks: (63.9 GB/59.5 GiB)
[726259.185603] sd 0:0:0:0: [sda] Write Protect is off
[726259.185613] sd 0:0:0:0: [sda] Mode Sense: 23 00 00 00
[726259.186432] sd 0:0:0:0: [sda] No Caching mode page found
[726259.186445] sd 0:0:0:0: [sda] Assuming drive cache: write through
[726259.206085]  sda: sda1
[726259.209004] sd 0:0:0:0: [sda] Attached SCSI removable disk

The device here is sda and our partition is sda1. So our partition is located here /dev/sda1.

Mount the drive (as root) to /media

# sudo mount /dev/sda1 /media/

Prepare the USB Device

Change to the /media directory and create a folder called hassbackup. Change the ownership to the user that runs Home Assistant. In this example case, the user and group are both homeassistant.

# cd /media/
/media# mkdir hassbackup
/media# chown homeassistant:homeassistant hassbackup/
/media# ls -al
total 28
drwxr-xr-x  4 root          root           4096 Apr 29 10:36 .
drwxr-xr-x 22 root          root           4096 Mar 22 18:37 ..
drwxr-xr-x  2 homeassistant homeassistant  4096 Apr 29 10:36 hassbackup
drwx------  2 root          root          16384 Apr 29 10:18 lost+found

Install Dependency

The script in the next section uses zip to preserve space on your drive. So we will install zip next.

/media# apt-get install zip
Reading package lists... Done
Building dependency tree
[...]
Setting up zip (3.0-8) ...

Download and Run Script

Become the homeassistant user (or whatever user runs Home Assistant). Change to whatever directory you would like the script placed into and run the following command.

# wget https://gist.githubusercontent.com/riemers/041c6a386a2eab95c55ba3ccaa10e7b0/raw/86727d4e72e9757da4f68f1c9d784720e72d0e99/usb_backup.sh

Make the downloaded script executable.

# chmod +x usb_backup.sh

Edit the script file using your preferred text editor (use nano if you are not advanced). Change the paths to reflect your configuration, then simply run ./usb_backup.sh.

$ .homeassistant/extraconfig/shell_code/usb_backup.sh
[i] Creating backup
[i] Backup complete: /media/hassbackup/hass-config_20170429_112728.zip
[i] Keeping all files no prunning set

Set Up Crontab

To automatically backup your configuration on a schedule, you can add a crontab for it as the homeassistant user.
Change the path below to the directory where you placed the usb_backup.sh and run the following line. This will backup every night at 3 am.

(crontab -l 2>/dev/null; echo "0 3 * * * /home/homeassistant/.homeassistant/extraconfig/shell_code/usb_backup.sh") | crontab -

Auto Mount the USB Device

NOTE: This does not automatically mount your USB drive at boot. You will need to manually mount your drive after each boot or add a line to your /etc/fstab file.

To manually mount a USB drive located at /dev/sda1, run the following line:

# mount /dev/sda1 /media

Alternatively, auto-mount the drive by adding the following entry to your /etc/fstab:

/dev/sda1  /media               ext4    defaults,noatime  0       1
2 Likes

HELP…
Holy Dooley… What’s happened to keeping it simple?
Is there no way this Noob can backup my Raspberry Pi SD card without having to learn a new computer language or even fathom what is being said.
I have HA up and running thanks to the user friendly desktop version. I have light switches working through Google Home and HA Cloud… So far so good but I am TERRIFIED the whole system is going to die and leave me in the dark
Do we have a simpler system to make a backup?
I run an iMac

3 Likes

It is probably easier to just create a snapshot and download the snapshot. This article explains this elegantly - How do I back everything up? SOLVED

1 Like

I agree that this sounds easier, but for people running in a Python Virtual Environment, I don’t believe it is possible.

As stated above:

This guide is aimed at users running Home Assistant Core in a Python Virtual Environment

Is this still the best method if I am running Home Assistant Core?

I’ve never run HA in python so do not have any direct experience. But it is my understanding that all variants of HA eventually offer a web based UI for access through browser. If that is the case with python environment that “snapshot” as well download of snapshot will work for you.
HTH.

Backing up your Raspberry, and with it, Node Red really isn’t that difficult.
You just insert a second micro-SD at least as big as your OS micro-SD into a USB port using a micro-SD adapter. Then run the SD card copier:

Then shut down the Pi and swap SD cards to use the just-copied SD card to boot the Pi. Every week or month, whatever you’re comfortable with, repeat.
This way you always have a fallback boot SD card.

2 Likes

Running HA core in a Python venv doesn’t offer a snapshot capability.

1 Like

That is good to know and hope that it is on future road map for python version, it is a pretty cool feature.

I like it but this assumes you have access to the GUI on the HA Rpi image. Is there a GUI on there that could be launched from the command prompt?

You will have to ask a Pi man. The GUI is the only way I have ever done it. I assume you are asking because your Pi is headless?
Mine is also headless, but the Pi is running the VNC Server (it’s already on the Pi, you just need to start it).

Turn on VNC Server from either the UI in Configuration, or from the command line with sudo raspi-config

Select “Interface Options”, then “Enable VNC”.

From the PC, just run the VNC viewer and enter the IP address of the Raspberry Pi.

Now, you have a GUI.

I’ve left a monitor and keyboard connected to my HA Rpi 3B. So from the command prompt I just type ‘sudo raspi-config’ to run the gui on the console?

i have a usb flash drive in the thin client.
i tried multiple ways for HA OS to mount and read the usb drive. all failed even though im logged in as root

i tried different variations as you can see n failed.
any idea why i dont have permission even though i am logged in as root?

This thread has nothing to do with HAOS.