Automatic Backup of Configuration Files

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?

Youā€™ll need to provide the hass account with appropriate SSH keys, though Iā€™d suggest using a remote user thatā€™s not root :wink:

Indeed, Iā€™d suggest you:

  1. Create a matching hass account on the receiving server
  • Set the owner of /mnt/usb_1/FileSync/AllFiles/HomeAssistant/ to that user
  • Create an SSH key for hass on your HA server and set up authorized_keys on the receiving server
  • Change your script and replace root with hass

@silvrr hello friend, i am following your tutorial but i have a doubtā€¦

i have a raspberry pi, and i put them one Pen drive formated fat32
but when i run the script not happens anything.
i should create any folder inside Pen??

or install something on my rpi?

sorry my questions.

I assume you setup the shell commands also?

If you are following my example exactly does the directory /mnt/usbdrive/backup/ exist and does user hass/homeassistant have write privileges?

Also depending on the installer you used /home/hass/.homeassistant/ may not be the directory your files are stored in.

Best course of action is to test the cp command using the terminal and ensure it works. When in terminal any errors will be shown and can be troubleshooted more easily.