Automation

Making things work with minimal human intervention like IOT, accessing projects remotely , authorisation and backups

  3D Modelling Game Development Software Development Electronics Automation Art  
Automating boring tasks

October 2022

Backup itself to GitHub

August 2021

I’m using VS code and the extension Remote -SSH because I am running the Pi headless and it’s the easiest way to copy the public SSH key

Github ssh key

In the Raspberry Pi terminal:

Check if you already have a SSH key with

ls -al ~/.ssh

If not, generate one with

ssh-keygen -t rsa -b 4096 -C "[email protected]"

rsa is a placeholder name. Change it if you like

Start ssh agent

eval `ssh-agent -s`

Add key to agent

ssh-add ~/.ssh/id_rsa

Change rsa to a name you choose if you like

Navigate to the .ssh directory and copy the contents of public key id_rsa.pub

~cd .ssh

This is where using VS code to navigate to the directory is handy as copying the key from a text file from the pi and pasting on the host computer is harder than it sounds.

In a browser of the host computer log into github and navigate to Settings⇒ SSH and GPG keys

Add key, name key (anything you will remember) and paste key.

Create Git Repository

While still in the browser create and name a new repository

On the pi navigate to the Node-Red directory. It is a hidden directory called .node-red

cd ~/.node-red is the default

Initialize the local directory as a Git repository

git init -b main

Add the files in your new local repository. You only need the “flows_raspberrypi.json” for an effective backup. You can add the whole directory for a backup including all the packages. Note if “flowsraspberrypi.json” is not in the directory it is most probably called “flows.json”

git add flows_raspberrypi.json

or

git add .to add the whole directory

Before the commit you will need to let the pi know who is doing the commits with

git config --global user.email "[email protected]"

git config --global user.name "your_username"

Commit with

git commit -m “first commit”

Add remote origin (the repository we created earlier) changing username and repository_name to match your credentials

git remote add origin [email protected]:username/repository_name

If you mashed an extra key and added the wrong repository name like I did the first time, remove it and try adding again $git remote rm origin

Push with

git push -u origin master

-u links the local branch with the remote branch which means you may use git pull without any arguments.

Automate daily backup within node red

From Node-Red: In a new flow drop in a “inject” node a “exec” node and a “debug” node

Change the inject to repeat from “none” to “at a specific time”

Connect the inject to exec node

In exec node uncheck Append msg.payload and add to Command

cd ~/.node-red && git commit -a -m "daily commit" && git push

Connect the first output of the exec node to the debug node

Deploy flow

Test by clicking the inject tab.

I have also added a message sent to Telegram to confrim when a change has been committed but that is another project...