Skip to content

Commit

Permalink
Add exlude list, preserve README.md, allow multiple branches per repo…
Browse files Browse the repository at this point in the history
…, 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
  • Loading branch information
Tylerjet authored Feb 25, 2024
1 parent 73f2974 commit eaaaa2f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
12 changes: 12 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
)
57 changes: 37 additions & 20 deletions script.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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/"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {} \;
find "$backup_path" -maxdepth 1 -mindepth 1 ! -name '.git' ! -name 'README.md' -exec rm -rf {} \;

0 comments on commit eaaaa2f

Please sign in to comment.