I wanted a quick, no BS automated daily backup of my configuration files on GitHub. Things I needed:
- No password! -> Must use certificate.
- No feedback -> Pipe feedback to
- Simple automation -> Single line in
N.B. there is a lot of text, but only to be very descriptive. It’s a very quick process!
- Create repo
- Create key
- Upload Deploy Key
- Change git remote
- Set cron task
Setup Config Directory witth Git
You can follows steps 1-5 inclusive of this HA GitHub guide to get your configuration directory setup, and to make sure you can successfully push to GitHub.
Create Single Keyin the terminal under the user that is the owner ( homeassistant in my case) for the HA configuration files. Create the key for the `homeassistant` user by doing the following.
[email protected]:~/.homeassistant $ ssh-keygen Generating public/private rsa key pair. # Leave the default location for the user, which is inside the .ssh folder in the home dir of the user. Enter file in which to save the key (/home/homeassistant/.ssh/id_rsa): # Press enter twice to not assign a password to key. Enter passphrase (empty for no passphrase): The keys randomart image is: +---[RSA 2048]----+ | .... | |. . ...o . | |.o o ++ o | |. o * Booo | | * &.=S . | | %.=. . | | . *.Xo o | |. *.+ E+ | |o. ..o. | +----[SHA256]-----+ # Certificate is complete! [email protected]:~/.homeassistant $
Upload Deploy Key
- Go to your GitHub repo online
- Click Settings in the top bar
- Click Deploy Keys
- Click Add deploy keys (in the top right)
- Give is a meaning Title for later
- Back in the terminal, use the following command to print out your public key:
- Copy the entire text, from ssh-rsa to [email protected] inclusive.
- Paste into the the Key field!
- Tick Allow write access so your HA can push code
Change Git to an SSH RemoteIf you followed the guide, you will need to change from password based authentication with HTTPS to key authentication with SSH. Instruction pulled from Git [here](https://help.github.com/articles/changing-a-remote-s-url/)
- Back in your repo on github.com, click on the green “Clone” button
- Choose SSH
- Cop contents
- Paste the following into terminal:
git remote set-url origin [PASTE] # Should look similar to this "git remote set-url origin [email protected]:USERNAME/REPOSITORY.git"
- as a test, run a
git pulland it shouldn’t ask you for a password!
Automate with Cron
- Back in terminal (still as the same user) type
- Choose the
/bin/nanooption, which is 2 for me. as is below:
[email protected]:~ $ crontab -e no crontab for homeassistant - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/ed 2. /bin/nano <---- easiest 3. /usr/bin/vim.basic 4. /usr/bin/vim.tiny Choose 1-4 : 2
- Paste the following on the last line of the opened file:
0 2 * * * /bin/bash -c "cd /home/homeassistant/.homeassistant && git commit -am 'Daily commit `date`' --quiet &> /dev/null && git push --quiet"
- Save and exit with Ctrl+x, and press Y then enter.
- Confirm the command is saved with
crontab -l(lowercase “L”)
Explanation of the Cron AutomationWhat this will do is, everyday at 2AM, add all changed files to the Git "commit" and call it "Daily commit" then the date and time e.g.
Daily commit Sun 4 Feb 02:00:01 AEDT 2018
Suppress all messages, and push put to GitHub!
Debugging and/or Changes
- If you need to change the date, time, or frequency, best thing to do is Google what you wish to make it. E.g. “cron every 30 minutes”
- If you feel like the cron is not working, paste the following part of the command into the terminal as the user who will push the code:
/bin/bash -c "cd /home/homeassistant/.homeassistant && git commit -am 'Daily commit `date`' && git push" # This will print out messages which might hint to what is wrong in Git.