Skip to content

Config Remote Backup

Harrypulvirenti edited this page Aug 18, 2022 · 10 revisions

On this page, we will describe in detail how to setup correctly the backup functionality built-in in this Klipper setup. This functionality allows you to perform the backup of your klipper_config folder to a remote git repository in a manual way or also schedule a periodic backup of the configuration if changes are detected.

Prerequisites

How to setup the remote backup

The configuration of the remote backup functionality can be split into two parts. In the first one, we will perform all the steps to correctly configure a Klipper macro to manually trigger the backup of the klipper_config folder. In the second one, we will configure the pi to schedule a service that will automate the backup at a specific time of the day.

If you don't want to configure the auto backup and have a macro to trigger manually the remote backup is enough for you you can just skip the second part of the configuration.

Remote backup via Klipper macro

This part of the configuration is the mandatory one so make sure to follow all the steps in the correct order.

Step 1 - Setup of the GitHub SSH key

Generate a new SSH key that will be used by your git installation to automatically authenticate to your remote repository and upload the changes detected in your klipper_config folder.

To do so follow the instruction that you can find here

Note: You need to follow the first two sections of the guide provider: Generating a new SSH key and Adding your SSH key to the ssh-agent. No need to follow the Generating a new SSH key for a hardware security key section

After the generation of your SSH Key, as suggested already in the GitHub instruction, add your new ssh key to your GitHub account as described here

Step 2 - Create a remote git repository for our configuration

Now is the time to create a remote repository where we are going to upload all our configuration files. To do so login into your GitHub account a create a new Repository under your profile. It doesn't matter if you choose to make it public or private.

After creating the repository you should see some instructions to connect your local repository to your remote one.

Here is an example of the instructions that you should see with a small addition required

cd /home/pi/klipper_config

echo "# MyAmazingKlipperConfig" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin replace_this_with_your_repo_url
git push -u origin main

This set of commands provided by GitHub will basically initiate a new local repository in your pi so before initialising it you must navigate to the folder that you want to backup on GitHub and in our example, this is done with the command cd /home/pi/klipper_config.

If everything goes well you should see a confirmation in the console saying that the push was successfully done to the remote repository.

Step 3 - Setting up a script for the remote backup

Inside the klipper_config folder create another folder called scripts using the mkdir command.

Move inside the folder and create the script for the remote backup with the following command:

cd scripts
nano ./remote_backup.sh

Copy and paste the following script content inside the terminal

#!/bin/bash
git -C ~/klipper_config pull
git -C ~/klipper_config add .
git -C ~/klipper_config commit -m "`date`"
git -C ~/klipper_config push

Save using Ctrl+O then run the following command to make the script executable

sudo chmod +x ./remote_backup.sh

Step 4 - Setting up the macro for the remote backup

At this point, the only thing left to do is to setup a macro that will allow us to easily trigger the remote backup from the Klipper dashboard.

If you have installed correctly the G-Code Shell Command Extension you should have a file called shell_command.cfg inside your configuration folder.

Paste this new command inside the file

[gcode_shell_command remote_backup]
command: ~/klipper_config/scripts/remote_backup.sh
timeout: 10.
verbose: True

and inside the file where all your macros are located add this new macro:

[gcode_macro SAVE_CONFIG_REMOTE]
description: "Macro to perform an upload of the configuration to a GitHub repo"
gcode:
    RUN_SHELL_COMMAND CMD=remote_backup

Save and restart and after this, you should be able to manually trigger the remote backup of your Klipper configuration using the macro that we just created.

Scheduling the Remote backup via a System Service

Step 5 - Create a System Service to automatically trigger the remote backup

Execute these commands to create the service that we need in the right location

cd /etc/systemd/system
sudo nano ./KlipperBackup.service

Now as we did before copy and paste the following code inside the terminal and save it with "Ctrl+O"

[Unit]
Description=Klipper Config backup service
After=network-online.target
Requires=network-online.target

[Service]
Type=simple
Restart=always
User=pi
WorkingDirectory=/home/pi/klipper_config
ExecStart= /home/pi/klipper_config/scripts/remote_backup.sh

[Install]
WantedBy=multi-user.target

Enable the new service that we just created with the following commands:

sudo systemctl daemon-reload
sudo systemctl enable KlipperBackup
sudo systemctl restart KlipperBackup

Step 6 - Schedule the System Service execution using a timer

The last thing that we need to do is to actually schedule the service that we just created.

Create it with the following command

sudo nano ./KlipperBackup.timer

and paste the following content inside the terminal

[Unit]
Description=Run Klipper Backup service periodically

[Timer]
Unit=KlipperBackup.service
OnCalendar=Mon..Sat 01:00

[Install]
WantedBy=timers.target

Enable the timer with the following command

systemctl enable --now KlipperBackup.timer

And we are done! The timer that we just created will execute the backup service for us every day at 01:00 of the night but you can easily change this value updating configuration inside the timer.

You can check that the timer is correctly scheduled by using the systemctl list-timers command.

I hope that this small guide was useful for you and happy printing!