From eaaaa2f1ae76537362052c0f154132c438ad7bc2 Mon Sep 17 00:00:00 2001 From: MrTylerjet Date: Sun, 25 Feb 2024 04:55:50 -0700 Subject: [PATCH 1/6] Add exlude list, preserve README.md, allow multiple branches per repo, lint and pretty code. (#66) * lint & pretty * add exlude array * do not delete README.md at end of script to preserve the file if a user wants to modify its contents locally * only create README.md when it does not yet exist in the backup folder * move like 20 to one line instead of multi-line * remove cherrypicks line as no longer needed * add the untrack line so that new additions to exclude are pushed to remote repo. * change branch check so that if the branch name does not match the currently checked out branch we change to it or create and change it, instead of the current process of renaming the branch entirely. This allows for one git repo to be used for backups of multiple printers by having diffent named branches for each * update comments for exclude code * update printout when branch switch occurs * removed end of line 37. as git already prints out the changing of branch to the new line * branch code should now work properly --- .env.example | 12 +++++++++++ script.sh | 57 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/.env.example b/.env.example index 5ee2554..8a7d68a 100644 --- a/.env.example +++ b/.env.example @@ -17,3 +17,15 @@ commit_email="" # `/*` should always be at the end of the path when backing up a folder so that the files inside of the folder are properly filtered and searched path_klipperdata=printer_data/config/* + +# Array of strings in .gitignore pattern git format https://git-scm.com/docs/gitignore#_pattern_format for files that should not be uploaded to the remote repo +# New additions must be enclosed in double quotes and should follow the pattern format as noted in the above link +exclude=( \ +"*.swp" \ +"*.tmp" \ +"printer-[0-9]*_[0-9]*.cfg" \ +"*.bak" \ +"*.bkp" \ +"*.csv" \ +"*.zip" \ +) \ No newline at end of file diff --git a/script.sh b/script.sh index 9b4c9a3..a0bcb8a 100755 --- a/script.sh +++ b/script.sh @@ -1,7 +1,10 @@ #!/usr/bin/env bash # Set parent directory path -parent_path=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P) +parent_path=$( + cd "$(dirname "${BASH_SOURCE[0]}")" + pwd -P +) # Initialize variables from .env file source "$parent_path"/.env @@ -11,10 +14,10 @@ backup_path="$HOME/$backup_folder" empty_commit=${empty_commit:-"yes"} git_host=${git_host:-"github.com"} full_git_url="https://"$github_token"@"$git_host"/"$github_username"/"$github_repository".git" +exclude=${exclude:-"*.swp" "*.tmp" "printer-[0-9]*_[0-9]*.cfg" "*.bak" "*.bkp" "*.csv" "*.zip"} # Check for updates -[ $(git -C "$parent_path" rev-parse HEAD) = $(git -C "$parent_path" ls-remote $(git -C "$parent_path" rev-parse --abbrev-ref @{u} | \ -sed 's/\// /g') | cut -f1) ] && echo -e "Klipper-backup is up to date\n" || echo -e "NEW klipper-backup version available!\n" +[ $(git -C "$parent_path" rev-parse HEAD) = $(git -C "$parent_path" ls-remote $(git -C "$parent_path" rev-parse --abbrev-ref @{u} | sed 's/\// /g') | cut -f1) ] && echo -e "Klipper-backup is up to date\n" || echo -e "NEW klipper-backup version available!\n" # Check if backup folder exists, create one if it does not if [ ! -d "$backup_path" ]; then @@ -27,12 +30,17 @@ cd "$backup_path" if [ ! -d ".git" ]; then mkdir .git echo "[init] - defaultBranch = "$branch_name"" >> .git/config #Add desired branch name to config before init + defaultBranch = "$branch_name"" >>.git/config #Add desired branch name to config before init git init - # Check if the current checked out branch matches the branch name given in .env if not update to new branch - elif [[ $(git symbolic-ref --short -q HEAD) != "$branch_name" ]]; then - echo "New branch in .env detected, rename $(git symbolic-ref --short -q HEAD) to $branch_name branch" - git branch -m "$branch_name" +# Check if the current checked out branch matches the branch name given in .env if not branch listed in .env +elif [[ $(git symbolic-ref --short -q HEAD) != "$branch_name" ]]; then + echo -e "Branch: $branch_name in .env does not match the currently checked out branch of: $(git symbolic-ref --short -q HEAD)." + # Create branch if it does not exist + if git show-ref --quiet --verify "refs/heads/$branch_name"; then + git checkout "$branch_name" >/dev/null + else + git checkout -b "$branch_name" >/dev/null + fi fi # Check if username is defined in .env @@ -48,7 +56,7 @@ if [[ "$commit_email" != "" ]]; then git config user.email "$commit_email" else # Get the MAC address of the first network interface for unique id - if ! command -v ifconfig &> /dev/null; then + if ! command -v ifconfig &>/dev/null; then mac_address=$(ipconfig | grep -o -E '([0-9a-fA-F]:?){6}' | head -n 1) else mac_address=$(ifconfig | grep -o -E '([0-9a-fA-F]:?){6}' | head -n 1) @@ -69,14 +77,12 @@ if [[ "$full_git_url" != $(git remote get-url origin) ]]; then git remote set-url origin "$full_git_url" fi -git config advice.skippedCherryPicks false - # Check if branch exists on remote (newly created repos will not yet have a remote) and pull any new changes -if git ls-remote --exit-code --heads origin $branch_name > /dev/null 2>&1; then +if git ls-remote --exit-code --heads origin $branch_name >/dev/null 2>&1; then git pull origin "$branch_name" # Delete the pulled files so that the directory is empty again before copying the new backup # The pull is only needed so that the repository nows its on latest and does not require rebases or merges - find "$backup_path" -maxdepth 1 -mindepth 1 ! -name '.git' -exec rm -rf {} \; + find "$backup_path" -maxdepth 1 -mindepth 1 ! -name '.git' ! -name 'README.md' -exec rm -rf {} \; fi cd "$HOME" @@ -86,19 +92,19 @@ while IFS= read -r path; do # Check if path does not end in /* or / if [[ ! "$path" =~ /\*$ && ! "$path" =~ /$ ]]; then path="$path/*" - elif [[ ! "$path" =~ \$ && ! "$path" =~ /\*$ ]]; then + elif [[ ! "$path" =~ \$ && ! "$path" =~ /\*$ ]]; then path="$path*" fi fi # Check if path contains files - if compgen -G "$HOME/$path" > /dev/null; then + if compgen -G "$HOME/$path" >/dev/null; then # Iterate over every file in the path for file in $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 + # 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 -r --parents "$file" "$backup_path/" @@ -109,8 +115,13 @@ done < <(grep -v '^#' "$parent_path/.env" | grep 'path_' | sed 's/^.*=//') cp "$parent_path"/.gitignore "$backup_path/.gitignore" -# Create and add Readme to 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)." > "$backup_path/README.md" +# utilize gits native exclusion file .gitignore to add files that should not be uploaded to remote. +# Loop through exclude array and add each element to the end of .gitignore +for i in ${exclude[@]}; do + # add new line to end of .gitignore if there is not one + [[ $(tail -c1 "$backup_path/.gitignore" | wc -l) -eq 0 ]] && echo "" >>"$backup_path/.gitignore" + echo $i >>"$backup_path/.gitignore" +done # Individual commit message, if no parameter is set, use the current timestamp as commit message if [ -n "$1" ]; then @@ -120,6 +131,12 @@ else fi cd "$backup_path" +# Create and add Readme to backup folder if it doesn't already exist +if ! [ -f "README.md" ]; then + 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)." >"$backup_path/README.md" +fi +# Untrack all files so that any new excluded files are correctly ignored and deleted from remote +git rm -r --cached . >/dev/null 2>&1 git add . git commit -m "$commit_message" # Check if HEAD still matches remote (Means there are no updates to push) and create a empty commit just informing that there are no new updates to push @@ -129,4 +146,4 @@ fi git push -u origin "$branch_name" # Remove files except .git folder after backup so that any file deletions can be logged on next backup -find "$backup_path" -maxdepth 1 -mindepth 1 ! -name '.git' -exec rm -rf {} \; \ No newline at end of file +find "$backup_path" -maxdepth 1 -mindepth 1 ! -name '.git' ! -name 'README.md' -exec rm -rf {} \; From 74deda4b12c60ed276e8bea87e129be1505f29e8 Mon Sep 17 00:00:00 2001 From: MrTylerjet Date: Tue, 27 Feb 2024 02:11:01 -0700 Subject: [PATCH 2/6] change ifconfig line to use /dev/urandom instead for the unique_id (#70) --- script.sh | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/script.sh b/script.sh index a0bcb8a..626b0a9 100755 --- a/script.sh +++ b/script.sh @@ -55,16 +55,10 @@ fi if [[ "$commit_email" != "" ]]; then git config user.email "$commit_email" else - # Get the MAC address of the first network interface for unique id - if ! command -v ifconfig &>/dev/null; then - mac_address=$(ipconfig | grep -o -E '([0-9a-fA-F]:?){6}' | head -n 1) - else - mac_address=$(ifconfig | grep -o -E '([0-9a-fA-F]:?){6}' | head -n 1) - fi # Use the MAC address to generate a unique identifier - unique_id=$(echo "$mac_address" | sha256sum | cut -c 1-8) - git config user.email "$(whoami)@$(hostname --long)-$unique_id" - sed -i "s/^commit_email=.*/commit_email=\"$(whoami)@$(hostname --long)-$unique_id\"/" "$parent_path"/.env + unique_id=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 7 | head -n 1) + git config user.email "$(whoami)@$(hostname --short)-$unique_id" + sed -i "s/^commit_email=.*/commit_email=\"$(whoami)@$(hostname --short)-$unique_id\"/" "$parent_path"/.env fi # Check if remote origin already exists and create if one does not From 1640410c65a83c02eb21f3b6b178e11fdc53615e Mon Sep 17 00:00:00 2001 From: Zach <152570+ziti@users.noreply.github.com> Date: Tue, 27 Feb 2024 05:09:56 -0600 Subject: [PATCH 3/6] Adds config option for the protocol to use (#68) --- script.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script.sh b/script.sh index 626b0a9..9d77000 100755 --- a/script.sh +++ b/script.sh @@ -12,8 +12,9 @@ source "$parent_path"/.env backup_folder="config_backup" backup_path="$HOME/$backup_folder" empty_commit=${empty_commit:-"yes"} +git_protocol=${git_protocol:-"https"} git_host=${git_host:-"github.com"} -full_git_url="https://"$github_token"@"$git_host"/"$github_username"/"$github_repository".git" +full_git_url=$git_protocol"://"$github_token"@"$git_host"/"$github_username"/"$github_repository".git" exclude=${exclude:-"*.swp" "*.tmp" "printer-[0-9]*_[0-9]*.cfg" "*.bak" "*.bkp" "*.csv" "*.zip"} # Check for updates From dd071c93c7741b062c38c1db581384d86993397f Mon Sep 17 00:00:00 2001 From: MrTylerjet Date: Thu, 29 Feb 2024 00:06:36 -0700 Subject: [PATCH 4/6] forgot to remove comment about mac address for unique id, and move (whoami)@(hostname --short)-unique_id to a variable and use that for lines 61 & 62 (#72) --- script.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/script.sh b/script.sh index 9d77000..7179d9d 100755 --- a/script.sh +++ b/script.sh @@ -56,10 +56,10 @@ fi if [[ "$commit_email" != "" ]]; then git config user.email "$commit_email" else - # Use the MAC address to generate a unique identifier unique_id=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 7 | head -n 1) - git config user.email "$(whoami)@$(hostname --short)-$unique_id" - sed -i "s/^commit_email=.*/commit_email=\"$(whoami)@$(hostname --short)-$unique_id\"/" "$parent_path"/.env + user_email=$(whoami)@$(hostname --short)-$unique_id + git config user.email "$user_email" + sed -i "s/^commit_email=.*/commit_email=\"$user_email\"/" "$parent_path"/.env fi # Check if remote origin already exists and create if one does not From 7880242865f827b1971c0a1f52dd018c7fd8f4b9 Mon Sep 17 00:00:00 2001 From: Staubgeborener Date: Thu, 29 Feb 2024 21:54:57 +0100 Subject: [PATCH 5/6] close_bot.yml stale timer to 24 days --- .github/workflows/close_bot.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/close_bot.yml b/.github/workflows/close_bot.yml index 66742a9..ec5212f 100644 --- a/.github/workflows/close_bot.yml +++ b/.github/workflows/close_bot.yml @@ -12,10 +12,10 @@ jobs: steps: - uses: actions/stale@v5 with: - days-before-issue-stale: 30 + days-before-issue-stale: 14 days-before-issue-close: 14 stale-issue-label: "stale" - stale-issue-message: "This issue is stale because it has been open for 30 days with no activity." + stale-issue-message: "This issue is stale because it has been open for 14 days with no activity." close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale." days-before-pr-stale: -1 days-before-pr-close: -1 From 326db085ddf857324353f4c8896b24849b12f8aa Mon Sep 17 00:00:00 2001 From: MrTylerjet Date: Sat, 2 Mar 2024 13:58:52 -0700 Subject: [PATCH 6/6] Update script.sh (#76) --- script.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.sh b/script.sh index 7179d9d..c4a8dd4 100755 --- a/script.sh +++ b/script.sh @@ -102,7 +102,7 @@ while IFS= read -r path; do elif [[ $(basename "$file") =~ ^printer-[0-9]+_[0-9]+\.cfg$ ]]; then echo "Skipping file: $file" else - cp -r --parents "$file" "$backup_path/" + rsync -R "$file" "$backup_path/" fi done fi