klipper-backup | save your config @ github

github screenshot

English below:

Wenn man immer mal wieder an den Einstellungen bei Klipper in den Konfigurationen herum spielt, dann wäre es doch toll, wenn es regelmäßige Backups gibt, die extern gespeichert werden.

Auf der Suche nach einer einfachen Lösung bin ich über folgende Seite gestolpert:
https://github.com/Staubgeborener/klipper-backup

Als erstes installiert man sich auf dem Raspberry oder direkt auf einem Drucker, wie in meinem Fall einem Neptune 4 Plus, git.

sudo apt update
sudo apt install git

Und jetzt schon fast das Wichtigste:

git config --global credential.helper store

Denn mit diesem Befehl wird der Token, den wir im Laufe der weiteren Installation erstellen werden, auch lokal zur Autentifizierung dient, gespeichert.
(Das war auch der Grund, warum ich immer wieder Fehlermeldungen beim Ausführen des Skripts erhalten habe.)

Also falls ihr so etwas wie “remote: Invalid username or password.” oder ähnliches bei der Ausführung des Scripts erhaltet, dann liegt es daran, dass git zumindest auf dem Elegoo Neptune 4 Board nicht direkt das Passwort speichert.

Wir erstellen uns bei Github ein neues Repository und einen Personal Access Token, dem wir Schreibrechte usw. geben.

Dann beginnen wir mit der eigentlichen Installation:

git clone https://github.com/Staubgeborener/klipper-backup.git && chmod +x ./klipper-backup/script.sh && cp ./klipper-backup/.env.example ./klipper-backup/.env

als nächstes bearbeiten wir die .env Datei mit einem Editor (z.B.) vim / nano etc.

nano klipper-backup/.env

Den frisch erstellten Github-Token kopieren wir an die Stelle “github_token=”, schreiben unseren Github-Benutzernamen an die enspechenden Stelle und tragen natürlich auch unser Github Repository ein.

Dann sieht es in etwa so aus:

github_token=ghp_xxx
github_username=noccis-github
github_repository=elegoo_neptune4backup
branch_name=main
commit_username="mks"
commit_email=""

# Indivdual file syntax:
#  Note: script.sh starts its search in $HOME which is /home/{username}/
# Using below example the script will search for: /home/{username}/printer_data/config/printer.cfg

path_printercfg=klipper_config/printer.cfg

# Backup folder syntax:
#  Note: script.sh starts its search in $HOME which is /home/{username}/
# Using below example the script will search for: /home/{username}/printer_data/config/*
# `/*` should always be at the end of the path when backing up a folder so that the files inside of the folder are prop$
#path_klipperdata=printer_data/config/*
path_klipperdata=klipper_config/*

Hier ist wir erstemal soweit fertig und wenden uns der moonraker.conf in Klipper zu.

Hier fügen wir folgendes hinzu und speichern die Datei:

[update_manager klipper-backup]
type: git_repo
path: ~/klipper-backup
origin: https://github.com/Staubgeborener/klipper-backup.git
managed_services: moonraker
primary_branch: main

Aber wir wollen das ganze ja auch noch ein wenig automatisieren:
Und deshalb erstellen wir einen neuen Service:

sudo nano /etc/systemd/system/klipper-backup.service

und dorthin kopieren wir:

[Unit]
Description=Klipper Backup Service
#Uncomment below lines if using network manager
#After=NetworkManager-wait-online.service
#Wants=NetworkManager-wait-online.service
#Uncomment below lines if not using network manager
#After=network-online.target
#Wants=network-online.target

[Service]
User={replace with your username}
Type=oneshot
ExecStart=/bin/bash -c '/home/mks/klipper-backup/script.sh "New Backup on boot $(date +%%D)"'

[Install]
WantedBy=default.target

Denkt bitte daran, die entsprechenden Werte und Pfade auf Euer Umfeld anzupassen.

Wir reloaden den Deamon und starten dann den Service:

sudo systemctl daemon-reload
sudo systemctl enable klipper-backup.service
sudo systemctl start klipper-backup.service

Optional kann man natürlich noch einen Cron installieren, der dann beispielsweise alle 4 Stunden ein Backup macht.

crontab -e

und hier wird dann folgendes eingefügt:

 */4 * * * $HOME/klipper-backup/script.sh

Aber via Mainsail oder Fluid ein Update anzustoßen wäre ja auch irgendwie praktisch. Also machen wir das in der printer.cfg möglich:

[gcode_macro update_git]
gcode:
    RUN_SHELL_COMMAND CMD=update_git_script

[gcode_shell_command update_git_script]
command: bash -c "bash $HOME/klipper-backup/script.sh"
timeout: 90.0
verbose: True

Fertig!

Und in Fuidd sieht das dann so aus:

Um sicher zu gehen, dass auch alles richtig funktioniert, könnt Ihr dann das Script direkt im Terminal einmal ausführen:

bash $HOME/klipper-backup/script.sh

Falls dort eine Passwortabfrage kommen sollte, dann einfach den Token, den Ihr Euch auf Github erstellt habt, nutzen und so sollte auch keine weitere Abfrage mehr kommen.

Viel Spaß!

English Version:

If you play around with the Klipper settings in the configurations from time to time, then it would be great if there were regular backups that are saved externally.

While looking for a simple solution, I stumbled across the following page:
https://github.com/Staubgeborener/klipper-backup

The first step is to install git on the Raspberry or directly on a printer, such as a Neptune 4 Plus in my case.

sudo apt update
sudo apt install git

And now almost the most important thing:

git config --global credential.helper store

This is because this command is used to save the token that we will create during the rest of the installation, which is also used locally for authentication.
(This was also the reason why I kept getting error messages when running the script).

So if you get something like “remote: Invalid username or password.” or something similar when running the script, it’s because git doesn’t save the password directly, at least on the Elegoo Neptune 4 board.

We create a new repository at Github and a personal access token, which we give write permissions etc. to.

Then we start with the actual installation:

git clone https://github.com/Staubgeborener/klipper-backup.git && chmod +x ./klipper-backup/script.sh && cp ./klipper-backup/.env.example ./klipper-backup/.env

Next, we edit the .env file with an editor (e.g. vim / nano etc.)

nano klipper-backup/.env

We copy the newly created Github token to the place “github_token=”, write our Github user name in the appropriate place and of course enter our Github repository.

Then it looks something like this:

github_token=ghp_xxx
github_username=noccis-github
github_repository=elegoo_neptune4backup
branch_name=main
commit_username="mks"
commit_email=""

# Indivdual file syntax:
#  Note: script.sh starts its search in $HOME which is /home/{username}/
# Using below example the script will search for: /home/{username}/printer_data/config/printer.cfg

path_printercfg=klipper_config/printer.cfg

# Backup folder syntax:
#  Note: script.sh starts its search in $HOME which is /home/{username}/
# Using below example the script will search for: /home/{username}/printer_data/config/*
# `/*` should always be at the end of the path when backing up a folder so that the files inside of the folder are prop$
#path_klipperdata=printer_data/config/*
path_klipperdata=klipper_config/*

Here we are ready for the first time and turn to the moonraker.conf in Klipper.
Here we add the following and save the file:

[update_manager klipper-backup]
type: git_repo
path: ~/klipper-backup
origin: https://github.com/Staubgeborener/klipper-backup.git
managed_services: moonraker
primary_branch: main

But we also want to automate the whole thing a little:
And that’s why we’re creating a new service:

sudo nano /etc/systemd/system/klipper-backup.service

and that’s where we copy:

[Unit]
Description=Klipper Backup Service
#Uncomment below lines if using network manager
#After=NetworkManager-wait-online.service
#Wants=NetworkManager-wait-online.service
#Uncomment below lines if not using network manager
#After=network-online.target
#Wants=network-online.target

[Service]
User={replace with your username}
Type=oneshot
ExecStart=/bin/bash -c '/home/mks/klipper-backup/script.sh "New Backup on boot $(date +%%D)"'

[Install]
WantedBy=default.target

Please remember to adapt the corresponding values and paths to your environment.

We will reload the Deamon and then start the service:

sudo systemctl daemon-reload
sudo systemctl enable klipper-backup.service
sudo systemctl start klipper-backup.service

Optionally, you can of course install a cron, which then makes a backup every 4 hours, for example.

crontab -e

and the following will be inserted here:

 */4 * * * $HOME/klipper-backup/script.sh

But triggering an update via Mainsail or Fluid would also be somewhat practical. So we make this possible in the printer.cfg:

[gcode_macro update_git]
gcode:
    RUN_SHELL_COMMAND CMD=update_git_script

[gcode_shell_command update_git_script]
command: bash -c "bash $HOME/klipper-backup/script.sh"
timeout: 90.0
verbose: True

Done!

And in Fuidd it looks like this:

To make sure that everything is working properly, you can run the script directly in the terminal:

bash $HOME/klipper-backup/script.sh

If you are asked for a password, just use the token you created on Github and you should not be asked for another one.
Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *