{"id":13343,"date":"2021-11-04T11:55:50","date_gmt":"2021-11-04T11:55:50","guid":{"rendered":"https:\/\/www.rjt.org.uk\/home\/?post_type=home_assistant_tip&#038;p=13343"},"modified":"2021-11-05T10:30:54","modified_gmt":"2021-11-05T10:30:54","slug":"setting-up-github-backup","status":"publish","type":"home_assistant_tip","link":"https:\/\/www.rjt.org.uk\/home\/archives\/home-assistant-tip\/setting-up-github-backup\/","title":{"rendered":"Setting up Github backup for Home Assistant"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Automatic Home Assistant Backup to GitHub\" width=\"910\" height=\"512\" src=\"https:\/\/www.youtube.com\/embed\/9-OG3bCQFFQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Also see <a href=\"https:\/\/diy-smarthome.com\/how-to-set-up-git-to-version-control-your-home-assistant-configuration\/\">https:\/\/diy-smarthome.com\/how-to-set-up-git-to-version-control-your-home-assistant-configuration\/<\/a><\/p>\n\n\n\n<p>To create the .gitignore execute the following<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">nano .gitignore<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">.gitignore file contents<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># WARNING: Make your GitHub repo Private if you are using this as it is\n# Example .gitignore file for your config dir.\n# An * ensures that everything will be ignored.\n*\n# You can whitelist files\/folders with !, these will not be ignored.\n!*.yaml\n!.gitignore\n!*.md\n!*.sh\n!*.js*\n\n# Comment these 2 lines if you don't want to include your SSH keys\n!.ssh\/\n!id_rsa*\n\n# Comment this if you don't want to include your Node-RED flows.\n# This is only valid if you install Node-RED as Home Assistant Add-on\n!node-red\/\n\n# Uncomment this if you don' want to include secrets.yaml \n# secrets.yaml\n\n# Ignore these files\/folders\n.storage\n.cloud\n.google.token \nhome-assistant.log<\/pre>\n\n\n\n<p>Git repository initialisation<br><code><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">git init<\/code><\/code><\/p>\n\n\n\n<p>Add all files to the repository<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">git add .<\/code><\/p>\n\n\n\n<p>Commit changes with message &#8220;first commit&#8221;<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">git commit -m \"first commit\"<\/code><\/p>\n\n\n\n<p>Add git remote<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">git remote add origin git@github.com:YOUR_GIT_HUB_ACC\/YOUR_REPO.git<\/code><\/p>\n\n\n\n<p>AUTOMATED HOME ASSISTANT GITHUB BACKUP<\/p>\n\n\n\n<p>Create a .ssh folder inside your HA config folder<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">mkdir .ssh<\/code><\/p>\n\n\n\n<p>Generate a private and public SSH keys<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">ssh-keygen -t rsa -b 4096 -C \"your@mail.com<\/code>&#8220;<\/p>\n\n\n\n<p>CHOOSE as path &#8211; .ssh\/id_rsa<\/p>\n\n\n\n<p>Get the content of this file<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">cat .ssh\/id_rsa.pub<\/code><\/p>\n\n\n\n<p>Paste content in GitHub SSH and GPG keys menu<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Tell Git where to find the newly created SSH keys<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">git config core.sshCommand \"ssh -i \/config\/.ssh\/id_rsa -F \/dev\/null\"<\/code><\/p>\n\n\n\n<p>When you do the first push you need to enter Yes to the Fingerprint Question.<\/p>\n\n\n\n<p><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">git add .<\/code><\/p>\n\n\n\n<p><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">git push -u origin master<\/code><\/p>\n\n\n\n<p>To create the script execute the following<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">nano ha_gitpush.sh<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Go to \/config folder or \n# Change this to your Home Assistant config folder if it is different\ncd \/config\n# Add all files to the repository with respect to .gitignore rules\ngit add .\n# Commit changes with message with current date stamp\ngit commit -m \"config files on `date +'%d-%m-%Y %H:%M:%S'`\"\n# Push changes towards GitHub\ngit push -u origin master<\/pre>\n\n\n\n<p>Save file and the make it executable<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">chmod +x ha_gitpush.sh<\/code><\/p>\n\n\n\n<p>Automation<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Put this in automations.yaml\n- id: l1k3\n  alias: push HA configuration to GitHub repo\n  trigger:\n  # Everyday at 23:23:23 time\n  - at: \u201923:23:23\u2019\n    platform: time\n  action:\n  - data:\n      addon: a0d7b954_ssh\n      input: \/config\/ha_gitpush.sh\n    service: hassio.addon_stdin<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Reset to HEAD \/ RESTORE\n\n# Going back to the commit before HEAD\ngit reset --hard HEAD^      \n\n# Going back two commits before HEAD\ngit reset --hard HEAD~2     \n\n# To undo a hard reset on Git\ngit reset --hard HEAD@{1}<\/pre>\n","protected":false},"template":"","class_list":["post-13343","home_assistant_tip","type-home_assistant_tip","status-publish","hentry","comments-off"],"_links":{"self":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip\/13343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip"}],"about":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/types\/home_assistant_tip"}],"wp:attachment":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/media?parent=13343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}