Skip to content

Commit

Permalink
Fixes Issue #6 , Improvements, Changes, Added Feature (#24)
Browse files Browse the repository at this point in the history
* removed version file as to update you can either git pull in the repository folder or add the repo to moonraker update manager, modified install script pretty much all it does now in terms of installing is making sure that .env.example is turned into .env for the user. modified script and .env to use as minimal hardcoded paths as possible ensureing that any home path that might not be on the user of pi should now work, as well moved the backup location to its own folder instead of storing in the repository which ensures that moonraker does not see the repo as dirty

* uncommented github upload as all other tests passed

* ensuring that updates to this repo still work properly, changed date format to M-D-Y for commit messages

* check if pc timezone is set to America and use US date formatting else use original date stamp for commit messages

* Update README.md

* Update README.md

Perfect the software update section sees those changes and prompts as expected!

* Delete install.sh

Removing install.sh as its sort of redundant since all it does is the command `cp .env.example .env` which could just be done with an && right after git clone.

* Update README.md

Add install instructions

* remove extra / for backup folder and config path, which now allows for the script to ignore read only files that should not have edits like KAMP, fluidd.cfg, mainsail.cfg. updated script.sh to remove the local backup files after upload so that any deletions can be logged on next backup.

* Update README.md

* Update README

* add global default branch git command to remove the warning about branch changes, and the git branch -M main as its now not needed. Removed a debug line in script.sh along with adding code to ignore the additional printer.cfg backups that fluidd like to make multiples of to remove excess clutter from the backup.

* format of printer backup configs was incorrect should have been an underscore not a dash near the end

* Update README.md

* update url

* remove /* from for loop in script.sh as it boke indidual path searching, instead placed into folder path syntax so its only seen when specfically searching through a folder only

* don't really need --global as can just set the local git config to keep the error from showing. Removed extra newline near top os script.sh

* update readme

* added back in one file path example for those who may want to use that method instead of backing up the entire config folder

* realized i no longer needed eval, just needed to remove the quotes around the variables and runs fine.

* added commend to descript what second elif is doing after skipping symbolic links, -u and --set-upstream were the same parameter chose one and removed the other

* Use source to grab all variables from .env in one go instead of using grep and sed parse the data. Check if .git exists in backup directory so that we only initialize the git repository once and don't continuously reinitialize on subsquent runs. FEATURE ADD: Commit username and email. You can now set a custom commit username and email if left blank the username will be the current user running the script ('whoami') and email is ('whoami')@('hostname --long')

* revert links back to https://github.com/Staubgeborener/klipper-backup, final push to test main for PR

* didn't update .env.example, now i can test

* Push V1.0.0 tag as well as commiting a extra whitespace to check moonraker updates

* adjust README.md to match requested change from Staubgeborener. fix issue found with git init sill trying to use master the branch name is now definable in .env, as well as setting the default branch directly in the config file before initializing. If .git already exists grab the branch name and use that instead.

* no need to cd into .git just echo to .git/config

* Just updated the comments for folder syntax to be a bit more clear and show examples of setup/explaination

---------

Co-authored-by: Tyler's Neptune 4 Pro <[email protected]>
  • Loading branch information
Tylerjet and Tyler's Neptune 4 Pro authored Jan 10, 2024
1 parent 302297b commit 5eff516
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 94 deletions.
30 changes: 14 additions & 16 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
github_token=ghp_xxxxxxxxxxxxxxxx
github_username=USERNAME
github_repository=REPOSITORY
branch_name=main
commit_username=""
commit_email=""

path_printercfg=/home/pi/printer_data/config/printer.cfg
path_mainsailcfg=/home/pi/printer_data/config/mainsail.cfg
path_moonrakercfg=/home/pi/printer_data/config/moonraker.conf
path_timelapsecfg=/home/pi/printer_data/config/timelapse.cfg
path_telegramcfg=/home/pi/printer_data/config/telegram.conf
path_crowsnestcfg=/home/pi/printer_data/config/crowsnest.conf
path_adxlcfg=/home/pi/printer_data/config/adxl.cfg
path_pacalcfg=/home/pi/printer_data/config/pa_cal.cfg
path_getprobelimitcfg=/home/pi/printer_data/config/GET_PROBE_LIMITS.cfg
path_savedvariablescfg=/home/pi/printer_data/config/saved_variables.cfg
path_pa_flow_calibratecfg=/home/pi/printer_data/config/pa_flow_calibrate.cfg
path_soundscfg=/home/pi/printer_data/config/sounds.cfg
path_macroscfg=/home/pi/printer_data/config/macros.cfg
# 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

#back up whole folder syntax:
#path_klipperdata=/home/pi/printer_data/
#path_printercfg=printer_data/config/printer.cfg

backup_folder=klipper
# 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/*
# The star denotes a wildcard meaning that any and all files in the folder will be selected
path_klipperdata=printer_data/config/*

backup_folder=config_backup/klipper
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This is a backup script to create manual or automated klipper backups in a githu
## RTFM
I would suggest reading the [wiki](https://github.com/Staubgeborener/klipper-backup/wiki), as this provides detailed step-by-step instructions.

If you have already used the script before release 1.0, please read [this wiki article](https://github.com/Staubgeborener/klipper-backup/wiki/migration) before opening an issue!

## YouTube
The user [Minimal 3DP](https://github.com/minimal3dp) has created a video about the initial setup and use of klipper-backup and made it available on YouTube. This and the wiki should explain many questions in advance.

Expand Down
59 changes: 0 additions & 59 deletions install.sh

This file was deleted.

64 changes: 46 additions & 18 deletions script.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,69 @@
#!/usr/bin/env bash

# Check for updates
[ $(git rev-parse HEAD) = $(git ls-remote $(git rev-parse --abbrev-ref @{u} | \
sed 's/\// /g') | cut -f1) ] && echo -e "Klipper-backup is up to date\n" || echo -e "Klipper-backup is $(tput setaf 1)not$(tput sgr0) up to date, consider making a $(tput setaf 1)git pull$(tput sgr0) to update\n"

# Set parent directory path
parent_path=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)

# Initialize variables from .env file
github_token=$(grep 'github_token=' "$parent_path"/.env | sed 's/^.*=//')
github_username=$(grep 'github_username=' "$parent_path"/.env | sed 's/^.*=//')
github_repository=$(grep 'github_repository=' "$parent_path"/.env | sed 's/^.*=//')
backup_folder=$(grep 'backup_folder=' "$parent_path"/.env | sed 's/^.*=//')
source "$parent_path"/.env

# Change directory to parent path
cd "$parent_path" || exit

# Check if backup folder exists, create one if it does not
if [ ! -d "$parent_path/$backup_folder" ]; then
mkdir "$parent_path/$backup_folder"
if [ ! -d "$HOME/$backup_folder" ]; then
mkdir -p "$HOME/$backup_folder"
fi

# Copy important files into backup folder
while read -r path; do
file=$(basename "$path")
cp -r "$path" "$parent_path/$backup_folder"
done < <(grep -v '^#' "$parent_path"/.env | grep 'path_' | sed 's/^.*=//')
while IFS= read -r path; do
# Iterate over every file in the path
for file in $HOME/$path; do
# Check if it's a symbolic link
if [ -h "$file" ]; then
echo "Skipping symbolic link: $file"
# Check if file is an extra backup of printer.cfg moonraker/klipper seems to like to make 4-5 of these sometimes no need to back them all up as well.
elif [[ $(basename "$file") =~ ^printer-[0-9]+_[0-9]+\.cfg$ ]]; then
echo "Skipping file: $file"
else
cp $file $HOME/$backup_folder/
fi
done
done < <(grep -v '^#' "$parent_path/.env" | grep 'path_' | sed 's/^.*=//')

# Add basic readme to backup repo
backup_parent_directory=$(dirname "$backup_folder")
echo -e "# klipper-backup 💾 \nKlipper backup script for manual or automated GitHub backups \n\nThis backup is provided by [klipper-backup](https://github.com/Staubgeborener/klipper-backup)." > "$HOME/$backup_parent_directory/README.md"

# Individual commit message, if no parameter is set, use the current timestamp as commit message
timezone=$(timedatectl | grep "Time zone" | awk '{print $3}')
if [ -n "$1" ]; then
commit_message="$1"
elif [[ "$timezone" == *"America"* ]]; then
commit_message="New backup from $(date +"%m-%d-%y")"
else
commit_message="New backup from $(date +"%d-%m-%y")"
fi

# Git commands
git init
git filter-branch --force --index-filter \
'git rm -r --cached --ignore-unmatch "$parent_path"/.env' \
--prune-empty --tag-name-filter cat -- --all
#git rm -rf --cached "$parent_path"/.env
git add "$parent_path"
cd "$HOME/$backup_parent_directory"
# Check if .git exists else init git repo
if [ ! -d ".git" ]; then
mkdir .git
echo "[init]
defaultBranch = $branch_name" >> .git/config #Add desired branch name to config before init
git init
branch=$(git symbolic-ref --short -q HEAD)
else
branch=$(git symbolic-ref --short -q HEAD)
fi

[[ "$commit_username" != "" ]] && git config user.name "$commit_username" || git config user.name "$(whoami)"
[[ "$commit_email" != "" ]] && git config user.email "$commit_email" || git config user.email "$(whoami)@$(hostname --long)"
git add .
git commit -m "$commit_message"
git push https://"$github_token"@github.com/"$github_username"/"$github_repository".git
git push -u https://"$github_token"@github.com/"$github_username"/"$github_repository".git $branch
# Remove klipper folder after backup so that any file deletions can be logged on next backup
rm -rf $HOME/$backup_folder/
1 change: 0 additions & 1 deletion version

This file was deleted.

0 comments on commit 5eff516

Please sign in to comment.