Background
Following the discussions here Save backup files to a dedicated directory?, I have setup a Klipper Backup Shell script
This script allows to:
- Define paths that are monitored for changes
- Upon a change in the monitored paths, one of 3 possible backup strategies is executed:
- Copy the files from all monitored paths compressed as tar to a defined path
- Copy them again compressed as tar to a remote storage with rclone
- Check in the files into a git repository and potentially push them to a remote git provider like GitHub
- The script will run as a service and uses inotify to monitor for any file-system changes. This is very efficient, since it is done directly in the Linux kernel
Installation and setup
I recommend using and installing the script under the same user that is running Klipper. So login to the Linux shell and execute:
wget https://raw.githubusercontent.com/Sineos/useful_bits/main/Linux/backup_klipper.sh
chmod u+x backup_klipper.sh # make it executable
nano backup_klipper.sh # open it in an editor
On top of script you will find
########## SETTINGS ##############
# Directories to monitor for changes // mind the (..)
MONITOR_DIRS=("/home/klipper/printer_data/config" "/home/klipper/printer_data/database")
# Backup directory for local backups
BACKUP_DIR="/home/klipper/backup"
# Number of backups to keep for cleanup (only valid for 'local' backups)
# Set to 0 to keep unlimited
NUM_BACKUPS_TO_KEEP=5 # Adjust this number based on your requirement
# Remote setup for rclone // only tested with Google Drive
# Other targets may need adaption of the script
REMOTE_NAME="myGoogleremote" # Replace with your rclone remote name
REMOTE_DIR="remoteFolder" # Replace with your remote directory in rclone's remote
# Git repository setup
GIT_REPO_PATH="/path/to/git/repo" # Replace with your Git repository path
GIT_DEF_BRANCH="main" # For GitHub the default branch is "main"
GIT_REMOTE_NAME="origin" # Replace with your Git remote name
# User for systemd service // Name of the service
SERVICE_USER="pi"
SERVICE_NAME="backup_klipper.service"
MONITOR_DIRS
is a shell array and hold all paths that shall be monitored for changes and also is the paths that are included in the backup. Mind the brackets (…)BACKUP_DIR
is used only for local backups and is the path where the tar files is copied to.NUM_BACKUPS_TO_KEEP
is also only applicable to local backups and controls how many backups are actually kept. Set it to 0 to keep allREMOTE_NAME
/REMOTE_DIR
is used byrclone
as target destinationrclone
needs to be configured separately to know the desired backup target- Tested with Google drive
- Other destinations might need adaption of the script
- Refer to the rclone documentation for the details
GIT_REPO_PATH
/GIT_DEF_BRANCH
/GIT_REMOTE_NAME
are the respective settings for the git repository- The git repository needs to be created manually and associated with a potential remote
- There is an abundance of guides in the www how to do this
SERVICE_USER
specifies the user under which the monitoring and backing up will happen.- I strongly suggest to use the same user as the Klipper user
- Local backup destinations or git repositories need to be accessible by this user
SERVICE_NAME
Not really needed to modify
After the respective settings are done in the script AND rclone or git is correctly configured then the script can be installed as a service with following commands
./backup_klipper.sh install local
install the service for creating local backups./backup_klipper.sh install remote
install the service for creating remote backups./backup_klipper.sh install git
install the service for creating git backups
This script assumes that you know how to configure rclone or a git repository or potentially adapt it to your needs.
It is intended to demonstrate one possible approach out of many.
This script:
- is NOT an out-of-the-box solution that fits to every possible scenario
- is NOT a ready made, fire and forget solution
- will require manual sysadmin tasks
- might require adaption, especially for rclone
Note:
- Copying data / files to different path on the same system or medium does not qualify as a backup
- A SD card in the typical Klipper SBCs is inherently failure prone. One unclean shutdown can kill the file-system and they are very sensitive to wear
- If you want to protect against loss or disaster, then make sure to have backups on a different system / medium or even remote, e.g. some cloud storage