Automatic Backup of Configuration Files

@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.

@silvrr yes, i have all configurated shell also.
i installed my HA from image hassbian.
my files are inside /home/homeassistant/.homeassistant

i use command:
sudo mkdir/mnt/usbdrive/backup/

and folders are OK in /mnt

but when i run the scrip… my PEN is empty.

$ cp -a /home/homeassistant/.homeassistant/configuration.yaml “/mnt/usbdrive/backup/configuration.yaml-$(date +”%m%d%Y-%H%M")"
cp: cannot create regular file ‘/mnt/usbdrive/backup/configuration.yaml-01192017-1903’: Permission denied

The issue is likely then that the first part of the CP command is wrong. The CP command says copy from one directory to another.

If you installed Hassbian then /home/hass/.homeassistant/ does not exist. Im suprised it didn’t notify you in the terminal window.

Try:

backup_configuration: cp -a /home/homeassistant/.homeassistant/configuration.yaml "/mnt/usbdrive/backup/configuration.yaml-$(date +"%m%d%Y-%H%M")"

@rpitera or @fabaff any idea why I cannot edit the orignal post in this thread anymore? I was going to add a note as hass is no longer the user so most of the directory information should be updated. Its locked though, I noticed it on a few threads.

@silvrr

$ cp -a /home/homeassistant/.homeassistant/configuration.yaml “/mnt/usbdrive/backup/configuration.yaml-$(date +”%m%d%Y-%H%M")"
cp: cannot create regular file ‘/mnt/usbdrive/backup/configuration.yaml-01192017-1903’: Permission denied