Automatic Backup of Configuration Files

I think you have to pay for private github repos.

Check out etckeeper. It tracks a directory in a revision control system(configurable with git, mercurial, plus others I’m not familiar with). The advantage of using RCS for config files is less disk use, and more importantly for me, less “noise” if you’re trying to see how you used to have it configured.

I use mercurial at work, and I’ve been dragging my feet to learn all the git differences, but they’re both “distributed”. GitHub is very popular, but it’s not git itself. You can have a git repository entirely on your local machine. Or for backup purposes you could push it to other machines under your control.

If you’re doing any kind of coding, I’d definitely advise you to learn some RCS system, because I have no idea how I got by with that old tedious process of manually copy in backups, trying changes, and saying “oh no! I broke it! Which named file was good again?”. I love mercurial, but if i had to advise a new user, I’d suggest learning git, because it seems it owns the open source world.

1 Like

You’re backup script got me curious. I want to replicate it, but decided I could go with a more scalable approach. I’ve made a script in Python. It’s more scalable, because you can let the script clean up the directory, backup more configfiles of other applications etc. For now, it’s pretty basic.
I’m referencing it in my HA like this:

# Launch Backup script
shell_command:
  backup_HassConfig: python /var/opt/homeassistant/PythonScripts/AutoHassBackups.py

The Python script is really basic right now, since it’s my first ever Python. But it works =)

To make this work:
sudo apt-get install sshpass

Then put this script in the appropriate directory:

from datetime import datetime, date, time
import subprocess

tijd = datetime.now()
tijd = tijd.strftime('%Y%m%d_%H%M%S')
localFileLoc = "/var/opt/homeassistant/"
remoteFileLoc = "USERNAME@IP:/share/Backups/PiController/Hass_"
remoteFileLoc = remoteFileLoc + tijd

shellCommand = 'sshpass -p "YOURPASSWORDHERE" scp -r' + " "+ localFileLoc + " " + remoteFileLoc

subprocess.call(shellCommand, shell=True)

This is designed to work a QNAP NAS with SCP (secure file copy over SSH). This way it backups my config files from HASS, but also the Python script which is in the same directory. I want to improve on it by creating ssh key instead of a password, but for now it’s fine.

3 Likes

Thank you. I like your script idea.

This is my 1line version of this with rsync:

rsync -av /home/hass/.homeassistant/* /mnt/sda1/'!!!Backup/'HASS/backup-$(date +“%Y%m%d%H%M”)/ --exclude deps --exclude home-assistant_v2.db

Nice to read all different ways to make an backup :+1:

I have a samba share setup (R:) and i use windows to edit my config. This is my simple oneliner .bat using xcopy in windows:

xcopy /s /z /i /h /exclude:excluded.txt R:\ “C:\Users\ME\Desktop\homeassistant\HA-backup-%date%”

excluded.txt consists of:

home-assistant_v2.db
deps

I have a Windows bat file too - have been having problems with shell commands from HASS. Windows task scheduler runs this every morning at 3am. I’ve included files rather than exclude, and I also copy hadashboard settings in second line of robocopy.

You will need to change the samba paths and backup directory to suit.

And u:user password to whatever you have for samba config - for example the default raspbian would be u:pi raspberry

NET USE \\PI_SAMBA_NAME\IPC$ /u:user password
robocopy "\\PI_SAMBA_NAME\PATH_TO_HASS_SHARE" "C:\backupfolder\HASS" "*.yaml" "*.sh" "*.xml" "*.conf" "*." /MIR /XA:H /W:0 /R:1 /REG > C:\externalbackup.log
robocopy "\\PI_SAMBA_NAME\homepi\hadashboard" "C:\backupfolder\hadashboard" "*.erb" "*.coffee" "*.scss" "*.rb" /MIR /XA:SH /W:0 /R:1 /REG /XJ >> C:\externalbackup.log
NET USE \\PI_SAMBA_NAME\IPC$ /D

Now that I have command shells accessible again I’ll be using the pi to do backups rather than windows.

Any idea on how to install ZIP on the Virtual Env. of a AIO Raspberry Installation Of Hass ???

I found this script that I want to run but needs Zip

#!/bin/bash

BACKUP_FILE=/media/pi/HASSUSB/hass-config_$(date +"%Y%m%d_%H%M%S").zip

pushd /home/hass/.homeassistant >/dev/null
zip -9 -q -r $BACKUP_FILE . -x"components/*" -x"deps/*" -x"home-assistant.db" -x"home-assistant_v2.db" -x"home-assistant.log"
popd >/dev/null

echo Backup complete: $BACKUP_FILE

@carlostico Take that zip binary I had you compile and copy it to /usr/local/bin

I’m using rclone (http://rclone.org) to copy the whole /etc folder to amazon cloud drive This is done through cron on a weekly basis.

1 Like

Could you share how please ?

I just visited your git page and couldn’t find the backup scripts. Can you let me know which file they are in or post another copy please? Backing up to git sounds like a great idea.

Just wanted to say thank you for sharing these scripts. I used the system of copying all python and yaml files in my HA instance via wildcard to my unraid server. I added the automation and the manual fire script to my HA.

Another option: edit and commit with git, have your HA install pull it from your repository

I’m using a cron job (triggers every night at 1 a.m.) and a small sh script to copy all yaml, xml and conf files in a zip and together with a requirements.txt for the virt_env to a local folder and then to my NAS via scp.

Here steh sh script:

    backupdir="$(date +%Y%m%d)_ha_files"
    bupath=/home/pi/backup_temp
    mkdir $bupath/$backupdir
    sudo -u hass pip freeze > $bupath/$backupdir/requirements.txt 
    sudo find /home/hass/.homeassistant/ -type f \( -name "*.yaml" -or -name "*.xml" -or -name "*.conf" \) -exec zip -9 $bupath/$backupdir/ha.zip {} \;
    scp -r $bupath/$backupdir pi@diskstation01:ha_backup/
1 Like

I use bitbucket (free private git by atlassian, issue tracker etc)
Edit on my mac then scp over, if it works, commit.
Being an scm tool, you obviously get all the branching and tagging benefits and easy rollback

Okay, so here is what I have tried. Everything showed up on the front end as it should and when I click on the ‘Activate’ link for the script, I get the pop-up in the lower left-hand side that states the script has been ‘turned on’.

However, while doing so I monitored the destination side and there were no changes to anything. No synchronization took place, no back-up image was made. I can run those exact same commands in terminal and they work flawlessly.

Any ideas?

shell_command:
  backup_config: rsync -azh -e ssh --delete --exclude '*.db' /home/hass/.homeassistant/ [email protected]:/mnt/usb_1/FileSync/AllFiles/HomeAssistant/
  backup_image: ssh [email protected] dd if=/dev/mmcblk0 of=/mnt/usb_1/FileSync/AllFiles/HARPi3_SD_Backup_$(date +%Y%m%d).img bs=1M

script:
  backup_config:
    alias: Back Up Configuration
    sequence:
      - service: shell_command.backup_config
  backup_image:
    alias: Image SD Card
    sequence:
      - service: shell_command.backup_image

 raspberrypi:
   name: HARPi3 System
   entities:
     - script.backup_config
     - script.backup_image

Did you run as the hass user in the virtual environment or just under the normal ssh login? The hass user has limited permissions and it can cause headaches like this. Checking it under hass/VE allows you to see what errors come up so you can troubleshoot.

I assume they work when you run them as the account “homeassistant” (or whichever user you’re running it as)?

If so, most likely it’s a path issue. You may find that explicitly providing the full path to the rsync binary in the backup_config command, and the ssh binary in the backup_image command, resolve it.

Uggg, no, I did not run as hass, it was under the normal ssh login.

So how does one run such commands as hass from within HA and how does one give that user the permissions to do so?