Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Staubgeborener committed Mar 5, 2024
2 parents b90ea30 + 326db08 commit defae2c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 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" \
)
4 changes: 2 additions & 2 deletions .github/workflows/close_bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 43 additions & 31 deletions script.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#!/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

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
[ $(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 +31,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 @@ -47,16 +56,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)
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
Expand All @@ -69,14 +72,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,31 +87,36 @@ 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/"
rsync -R "$file" "$backup_path/"
fi
done
fi
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 +126,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 +141,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 defae2c

Please sign in to comment.