Node-RED Flow backups using Git
Node-RED is particularly popular in home automation. For example, the free framework can be used to playfully create processes ("turn on light") that react dynamically to events ("if it's evening") - and that with low-code. The logic clicked together via drag & drop can then be exported via JSON, for example, and saved as a backup - if you think of it.
Newer Node-RED versions include projects functionality which already serves Git integration - so the following steps aren't needed, anymore!
Backing up the logic is done with a few mouse clicks - so individual or all flows can be exported and also imported again. However, it is a manual process that tends to be forgotten. Once an export is done, the files still need to be stored and versioned in a meaningful way afterwards.
Whenever possible, I always use Git to version files and document changes. Unfortunately, there doesn't seem to be much usable Git extensions for Node-RED - a semi-useful one is node-red-contrib-git-ui. This is installed as follows:
1$ npm i -S node-red-contrib-git-ui
After restarting the application/container, the extension is active.
An SSH key should now be generated for the container/application user so that content can be maintained without entering a password:
1$ ssh-keygen
A Git repository is now created in the application folder (/data when using the Docker container) and a staging branch is created. This is necessary because the extension unfortunately only updates flow changes in this branch.
1$ cd /data
2$ git init .
3$ git checkout -b staging
Then the URL of an empty remote repository is configured:
1$ git remote add ssh://...
Since the extension unfortunately automatically deposits all changed files in Git, the overview suffers. The remedy is to create a .gitignore allowlist that only allows flow-relevant files:
1*
2!flows.json
3!package.json
4!settings.js
5!staging/*
6!.gitignore
Afterwards, a git-ui entry can be found in the dropdown menu on the right - a commit can simply be triggered here. In the log submenu, past commits can also be viewed, compared and restored:
![Checking the history](/images/2021/11/GitHistory.png)
![Adding changes](/images/2021/11/GitCommit.png)
![Comparing and reverting changes](/images/2022/02/GitCompareRestore.png)
If you are looking for an easy way to backup flows regularly with Git, you should take a look at the extension. However, it would be nice if the documentation was better and there were more settings options.