Automatically Backup Your Klipper Config to Github to Maintain a Version History

Setting up your Klipper config often takes a lot of time. Doing it all over again the second time isn’t a whole lot easier. Backing it up allows you be back up and running in no time. On top of that, you may often make a change, and not realize the problem it caused much later, that’s where having a version history of your config can be useful. I wrote this article to document how I did it. It may not be the best way, but it was fairly easy. I ended up doing it this way because the script located here didn’t work for me, and I just wanted to get it up and running.

Also, check out the affiliate links to the right if you plan on shopping on Amazon to send a little change my way to keep this site running.

GitHub Setup

  1. Go to GitHub, click your profile pic at the top right and click “Your Repositories” -> New
  2. Give it a name(you will need it later), and make it private, unless you want it public.
  3. Click your profile pic -> Settings -> Developer Settings (On the left) -> Personal access token -> Generate new token. I did the classic token. I’m sure the other works fine, but it’s in beta.
  4. Set the expiration if you want and give it the ‘repo’ access under scopes.
  5. Click Generate Token, and then be sure to copy the access token on the next page. That will be the only time they ever show it to you again. If you don’t copy it, you will need to delete it and generate a new one. This is what you will use as a password later.

Push to the repository

UPDATE: I made a bash script that does all this for you. I only made it to make it easier to set this up on all 7 printers, so it has ZERO error checking. For example, if you mess up, and you end up running it multiple times, you will end up with crontab having multiple entries for running the script, and the script that it runs will also have multiple entries. Super easy to fix that just by deleting the lines, but use the script at your own risk. That script is at the end of this post.

  1. Now SSH into your Pi, or whatever computer is running klipper.
  2. cd into the directory where your config is stored. It is most likely ~/klipper_config or ~/printer_data/config, or maybe even both, like I have(I just realized this, and I both folders appear to be sync’ed, so I’m guessing it’s just a pointer). I’ll use the ~/printer_data/config folder in my examples since I think that’s what the latest version of Klipper uses.
    cd ~/printer_data/config
  3. Run “git init” which creates the config files and temp directory for git.
    git init
  4. Run “git add ~/printer_data/config/*” which adds all the config to the files that git will look at.
    git add ~/printer_data/config/*
    
  5. You may need to run these 2 commands, using your own info, but it will tell you after you run the next step after this one:
    git config --global user.email "you@example.com"
    git config --global user.name "Your Name"
  6. Run ‘git commit -m “initial upload”‘ which prepares the changed files, or in this case all the files, for upload.
    git commit -m "initial upload"
  7. Run “git config –global credential.helper store” which tells it to store your credentials
    git config --global credential.helper store
  8. Enter these 3 commands to upload. You will need to change the URL to the repository in the first command. These 3 commands should also show up near the bottom of the GitHub page for your repository, with your repository name in there:
    git remote add origin https://github.com/simpat1zq/YOURREPOSITORYNAME.git
    git branch -M main
    git push -u origin main
  9. It should ask you for a username and password. The username would be your GitHub username or email, and the password is the Personal Access Token from the first section.
  10. That should be it. Your files should be in GitHub now.

Create a script to push to GitHub

Now you just need to create a script that does a few of those commands for you on a set schedule.

  1. Create and edit a file that will contain the script.
    nano ~/klipperBackup.sh
    Add the following code to the file. If your config folder is different, then change that part in all 4 places in the code. The “-C ~/printer_data/config” option on each command tells it to run the git command from that directory, and the rest is the  same as the commands you ran previously. That option wasn’t needed before because you were already in that directory. And in the commit command, it is supposed to use the date as the commit message, which is required:
    #!/bin/sh
    git -C ~/printer_data/config add ~/printer_data/config/*
    git -C ~/printer_data/config commit  -m "`date`"
    git -C ~/printer_data/config push origin main
  2. Press CTRL+X to close the file. It will prompt you to save it.
  3. Make the script executable:
    chmod +x ~/klipperBackup.sh
  4. Run the script to make sure there aren’t any errors. And then you can make a small change to any of the files, and run it again to make sure it updated the file in GitHub:
    ~/klipperBackup.sh

Schedule the script to run

  1. Run crontab -e It may ask you which editor you want to use. I prefer Nano.
    crontab -e
  2. Go down to the bottom and add this. It will run the git upload every 30 minutes. It will only actually upload anything if there are changes. If you want to change the timing of the schedule, then change that first part of the line appropriately. Check out https://crontab.guru/ and hit the random button for examples, and edit the line there to make sure you have your schedule right.
    */30 * * * * ~/klipperBackup.sh
    
  3. Press CTRL+X to close the file. It will prompt you to save it

That’s it, the script should run and automatically upload any changes every 30 minutes. You can go to any file in GitHub and click History to see all the changes you have made to the file after setting this up. Of course, there’s nothing special in the instructions for Klipper specifically, but that’s just what I’m using it for, so I thought there might be others that would like to setup up but didn’t know how, like me.

And here’s the script I wrote that should automate a lot of the above. AGAIN, USE IT AT YOUR OWN RISK!! There is ABSOLUTELY NO error checking built into the script. Running it multiple times will cause multiple crontab entries. It also creates a script that the cron runs, and that script will have multiple entries. And if you enter your repository wrong once, then you will need to remove the git remote. (git remote remove origin).  I made this for my own use, but decided to share it as it likely won’t blow up your computer(I can’t guarantee that though).

#!/bin/bash
#Get the variables that will be used later
read -e -p "Config folder(Needs a '/' at the end): " -i "$HOME/printer_data/config/" cfg_folder
read -e -p "Email for git pushses to github: " git_email
read -e -p "Name for git pushes to github: " git_name
read -e -p "GitHub repository: " git_repo

#setup git
git -C $cfg_folder init
git config --global user.email $git_email
git config --global user.name $git_name
git config --global credential.helper store
git -C $cfg_folder remote add origin $git_repo
git -C $cfg_folder branch -M main

#run the initial upload
git -C $cfg_folder add  $cfg_folder"*"
git -C $cfg_folder commit -m "initial upload"
echo "You will need to enter your GitHub username/email and password"
git -C $cfg_folder push -u origin main

#create a script that does the commands from the section above(In hindsight, I should have just created this script first, and ran it)
cat <<EOT >> KlipperBU.sh
#!/bin/bash
git -C $cfg_folder add  $cfg_folder"*"
git -C $cfg_folder commit -m "initial upload"
git -C $cfg_folder push -u origin main
EOT

#make the script executable
chmod +x KlipperBU.sh

#Add the script to crontab
crontab -l | { cat; echo "*/30 * * * * ~/KlipperBU.sh"; } | crontab -

 

Leave a Reply