Update 6.0.0
This commit is contained in:
parent
bb0b7539a0
commit
c8db437b79
63 changed files with 3667 additions and 737 deletions
|
@ -1,26 +1,29 @@
|
|||
#!/bin/sh
|
||||
|
||||
start_git_backup() {
|
||||
echo "Info: Starting Git Backup..."
|
||||
/usr/data/helper-script/files/git-backup/git-backup.sh -b "$BRANCH" -t "$IFS" -g origin > /dev/null 2>&1 &
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
start)
|
||||
echo "Starting Git Backup..."
|
||||
/usr/data/helper-script/files/git-backup/git-backup.sh -b "$BRANCH" -t "$IFS" -g origin & > /dev/null
|
||||
start_git_backup
|
||||
;;
|
||||
stop)
|
||||
echo "Stopping Git Backup..."
|
||||
pkill Git-Backup
|
||||
pkill inotifywait
|
||||
pkill git-backup.sh
|
||||
echo "Info: Stopping Git Backup..."
|
||||
killall -q git-backup.sh
|
||||
killall -q inotifywait
|
||||
;;
|
||||
restart)
|
||||
echo "Restarting Git Backup..."
|
||||
pkill Git-Backup
|
||||
pkill inotifywait
|
||||
echo "Info: Restarting Git Backup..."
|
||||
$0 stop
|
||||
sleep 1
|
||||
/usr/data/helper-script/files/git-backup/git-backup.sh -b "$BRANCH" -t "$IFS" -g origin & > /dev/null
|
||||
start_git_backup
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|restart}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
exit 0
|
||||
|
||||
exit 0
|
|
@ -20,16 +20,52 @@ timeout: 600.0
|
|||
verbose: true
|
||||
|
||||
|
||||
[gcode_macro GIT_BACKUP_STOP]
|
||||
[gcode_macro _GIT_STOP]
|
||||
description: Stop pushing to GitHub until manually resumed
|
||||
gcode:
|
||||
RUN_SHELL_COMMAND CMD=Backup_Stop
|
||||
RESPOND TYPE=command MSG="action:prompt_end"
|
||||
|
||||
|
||||
[gcode_macro _GIT_PAUSE]
|
||||
description: Stop pushing to GitHub until next reboot or until manually resumed
|
||||
gcode:
|
||||
RUN_SHELL_COMMAND CMD=Backup_Pause
|
||||
RESPOND TYPE=command MSG="action:prompt_end"
|
||||
|
||||
|
||||
[gcode_macro _GIT_RESUME]
|
||||
description: Resume pushing to GitHub
|
||||
gcode:
|
||||
RUN_SHELL_COMMAND CMD=Backup_Resume
|
||||
RESPOND TYPE=command MSG="action:prompt_end"
|
||||
|
||||
|
||||
[gcode_macro GIT_BACKUP_STOP]
|
||||
description: Stop pushing to GitHub until manually resumed
|
||||
gcode:
|
||||
RESPOND TYPE=command MSG="action:prompt_begin Git Backup"
|
||||
RESPOND TYPE=command MSG="action:prompt_text Do you want to stop pushing to GitHub until manually resumed?"
|
||||
RESPOND TYPE=command MSG="action:prompt_footer_button NO|RESPOND TYPE=command MSG="action:prompt_end"|error"
|
||||
RESPOND TYPE=command MSG="action:prompt_footer_button YES|_GIT_STOP|primary"
|
||||
RESPOND TYPE=command MSG="action:prompt_show"
|
||||
|
||||
|
||||
[gcode_macro GIT_BACKUP_PAUSE]
|
||||
description: Stop pushing to GitHub until next reboot or until manually resumed
|
||||
gcode:
|
||||
RUN_SHELL_COMMAND CMD=Backup_Pause
|
||||
RESPOND TYPE=command MSG="action:prompt_begin Git Backup"
|
||||
RESPOND TYPE=command MSG="action:prompt_text Do you want to stop pushing to GitHub until next reboot or until manually resumed?"
|
||||
RESPOND TYPE=command MSG="action:prompt_footer_button NO|RESPOND TYPE=command MSG="action:prompt_end"|error"
|
||||
RESPOND TYPE=command MSG="action:prompt_footer_button YES|_GIT_PAUSE|primary"
|
||||
RESPOND TYPE=command MSG="action:prompt_show"
|
||||
|
||||
|
||||
[gcode_macro GIT_BACKUP_RESUME]
|
||||
description: Resume pushing to GitHub
|
||||
gcode:
|
||||
RUN_SHELL_COMMAND CMD=Backup_Resume
|
||||
RESPOND TYPE=command MSG="action:prompt_begin Git Backup"
|
||||
RESPOND TYPE=command MSG="action:prompt_text Do you want to resume pushing to GitHub?"
|
||||
RESPOND TYPE=command MSG="action:prompt_footer_button NO|RESPOND TYPE=command MSG="action:prompt_end"|error"
|
||||
RESPOND TYPE=command MSG="action:prompt_footer_button YES|_GIT_RESUME|primary"
|
||||
RESPOND TYPE=command MSG="action:prompt_show"
|
||||
|
|
|
@ -7,37 +7,12 @@
|
|||
# - Dominik D. Geyer
|
||||
# - Phil Thompson
|
||||
# - Dave Musicant
|
||||
#
|
||||
# Edited to work on busybox ash shell, specifically the Creality K1 & K1Max
|
||||
#############################################################################
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#############################################################################
|
||||
#
|
||||
# Idea and original code taken from http://stackoverflow.com/a/965274
|
||||
# original work by Lester Buck
|
||||
# (but heavily modified by now)
|
||||
#
|
||||
# Requires the command 'inotifywait' to be available, which is part of
|
||||
# the inotify-tools (See https://github.com/rvoicilas/inotify-tools ),
|
||||
# and (obviously) git.
|
||||
# Will check the availability of both commands using the `which` command
|
||||
# and will abort if either command (or `which`) is not found.
|
||||
#
|
||||
# - Guislain Cyril
|
||||
|
||||
white=`echo -en "\033[m"`
|
||||
yellow=`echo -en "\033[1;33m"`
|
||||
green=`echo -en "\033[01;32m"`
|
||||
darkred=`echo -en "\033[31m"`
|
||||
|
||||
INSTALL=0
|
||||
PAUSE=0
|
||||
|
@ -49,7 +24,7 @@ TARGET=""
|
|||
EVENTS="${EVENTS:-close_write,move,move_self,delete,create,modify}"
|
||||
SLEEP_TIME=5
|
||||
DATE_FMT="+%d-%m-%Y (%H:%M:%S)"
|
||||
COMMITMSG="Auto-commit on %d by Git Backup"
|
||||
COMMITMSG="Auto-commit by Git Backup"
|
||||
SKIP_IF_MERGING=0
|
||||
|
||||
# Function to print script help
|
||||
|
@ -110,61 +85,161 @@ if [ "$PAUSE" = 1 ]; then
|
|||
exit 0
|
||||
elif [ "$STOP" = 1 ]; then
|
||||
echo "Info: Stopping automatic backups until manually restarted..."
|
||||
mv /etc/init.d/S52Git-Backup /etc/init.d/disabled.S52Git-Backup
|
||||
if [ -f mv /etc/init.d/S52Git-Backup ];then
|
||||
mv /etc/init.d/S52Git-Backup /etc/init.d/disabled.S52Git-Backup
|
||||
fi
|
||||
/etc/init.d/S52Git-Backup stop
|
||||
exit 0
|
||||
elif [ "$RESUME" = 1 ]; then
|
||||
echo "Info: Resuming automatic backups..."
|
||||
mv /etc/init.d/disabled.S52Git-Backup /etc/init.d/S52Git-Backup
|
||||
if [ -f /etc/init.d/disabled.S52Git-Backup ];then
|
||||
mv /etc/init.d/disabled.S52Git-Backup /etc/init.d/S52Git-Backup
|
||||
fi
|
||||
/etc/init.d/S52Git-Backup start
|
||||
exit 0
|
||||
elif [ "$INSTALL" = 1 ]; then
|
||||
# Install required packages using opkg
|
||||
if [ -f /opt/bin/opkg ]; then
|
||||
/opt/bin/opkg update
|
||||
/opt/bin/opkg install inotifywait procps-ng-pkill
|
||||
/opt/bin/opkg install inotifywait
|
||||
else
|
||||
echo "Error: opkg package manager not found. Please install Entware."
|
||||
echo
|
||||
echo "${white}${darkred} ✗ opkg package manager not found. Please install Entware.${white}"
|
||||
echo
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Prompt user for configuration
|
||||
echo "${white}"
|
||||
read -p " Please enter your ${green}GitHub username${white} and press Enter: ${yellow}" USER_NAME
|
||||
while [ -z "$USER_NAME" ]; do
|
||||
echo "${white}"
|
||||
echo "${darkred} ✗ Invalid GitHub username!${white}"
|
||||
echo
|
||||
read -p " Please enter your ${green}GitHub username${white} and press Enter: ${yellow}" USER_NAME
|
||||
done
|
||||
valid_email=false
|
||||
while [ "$valid_email" != true ]; do
|
||||
echo "${white}"
|
||||
read -p " Please enter your ${green}GitHub email address${white} and press Enter: ${yellow}" USER_MAIL
|
||||
echo "$USER_MAIL" | grep -E -q "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "${white}"
|
||||
echo "${darkred} ✗ Invalid email address!${white}"
|
||||
else
|
||||
valid_email=true
|
||||
fi
|
||||
done
|
||||
echo "${white}"
|
||||
read -p " Please enter your ${green}GitHub repository name${white} and press Enter: ${yellow}" REPO_NAME
|
||||
while [ -z "$REPO_NAME" ]; do
|
||||
echo "${white}"
|
||||
echo "${darkred} ✗ Invalid GitHub repository name!${white}"
|
||||
echo
|
||||
read -p " Please enter your ${green}GitHub repository name${white} and press Enter: ${yellow}" REPO_NAME
|
||||
done
|
||||
echo "${white}"
|
||||
read -p " Please enter your ${green}GitHub branch name${white} and press Enter: ${yellow}" REPO_BRANCH
|
||||
while [ -z "$REPO_BRANCH" ]; do
|
||||
echo "${white}"
|
||||
echo "${darkred} ✗ Invalid GitHub branch name!${white}"
|
||||
echo
|
||||
read -p " Please enter your ${green}GitHub branch name${white} and press Enter: ${yellow}" REPO_BRANCH
|
||||
done
|
||||
echo "${white}"
|
||||
read -p " Please enter your ${green}GitHub personal access token${white} and press Enter: ${yellow}" GITHUB_TOKEN
|
||||
while [ -z "$GITHUB_TOKEN" ]; do
|
||||
echo "${white}"
|
||||
echo "${darkred} ✗ Invalid GitHub personal access token!${white}"
|
||||
echo
|
||||
read -p " Please enter your ${green}GitHub personal access token${white} and press Enter: ${yellow}" GITHUB_TOKEN
|
||||
done
|
||||
echo "${white}"
|
||||
|
||||
# Prompt user to select folders to be watched
|
||||
# Folder to be watched
|
||||
IFS=/usr/data/printer_data/config
|
||||
|
||||
# Connect config directory to github
|
||||
cd "$IFS" || exit
|
||||
git config --global user.name "$USER_NAME"
|
||||
git config --global user.email "$USER_MAIL"
|
||||
git init
|
||||
git remote add origin "https://$USER_NAME:$GITHUB_TOKEN@github.com/$USER_NAME/$REPO_NAME.git"
|
||||
git checkout -b "$REPO_BRANCH"
|
||||
git add .
|
||||
git commit -m "Initial Backup"
|
||||
git push -u origin "$REPO_BRANCH"
|
||||
push_repo=$(git push -u origin "$REPO_BRANCH" 2>&1)
|
||||
if echo "$push_repo" | grep -q "fatal: remote origin already exists"; then
|
||||
echo
|
||||
rm -rf /usr/data/printer_data/config/.git
|
||||
killall -q inotifywait >/dev/null 2>&1
|
||||
/opt/bin/opkg --autoremove remove inotifywait >/dev/null 2>&1
|
||||
echo "${white}${darkred} ✗ A branch named $REPO_BRANCH already exists!"
|
||||
echo " Use another branch name or clean your repo and restart Git Backup installation.${white}"
|
||||
echo
|
||||
exit 0
|
||||
elif echo "$push_repo" | grep -q "error: failed to push some refs to"; then
|
||||
echo
|
||||
rm -rf /usr/data/printer_data/config/.git
|
||||
killall -q inotifywait >/dev/null 2>&1
|
||||
/opt/bin/opkg --autoremove remove inotifywait >/dev/null 2>&1
|
||||
echo "${white}${darkred} ✗ Your repository already contains commits and files cannot be merged!"
|
||||
echo " Please clean your repo and restart Git Backup installation.${white}"
|
||||
echo
|
||||
exit 0
|
||||
elif echo "$push_repo" | grep -q "remote: Repository not found"; then
|
||||
echo
|
||||
rm -rf /usr/data/printer_data/config/.git
|
||||
killall -q inotifywait >/dev/null 2>&1
|
||||
/opt/bin/opkg --autoremove remove inotifywait >/dev/null 2>&1
|
||||
echo "${white}${darkred} ✗ Your repository was not found!"
|
||||
echo " Check your provided information and restart Git Backup installation.${white}"
|
||||
echo
|
||||
exit 0
|
||||
elif echo "$push_repo" | grep -q "fatal: Authentication failed"; then
|
||||
echo
|
||||
rm -rf /usr/data/printer_data/config/.git
|
||||
killall -q inotifywait >/dev/null 2>&1
|
||||
/opt/bin/opkg --autoremove remove inotifywait >/dev/null 2>&1
|
||||
echo "${white}${darkred} ✗ Authentication failed!"
|
||||
echo " Check your GitHub personal access token and restart Git Backup installation.${white}"
|
||||
echo
|
||||
exit 0
|
||||
else
|
||||
git push -u origin "$REPO_BRANCH"
|
||||
fi
|
||||
|
||||
# Write configuration to .env file
|
||||
echo "IFS=$IFS" > "$IFS/.env"
|
||||
echo "GITHUB_TOKEN=$GITHUB_TOKEN" >> "$IFS/.env"
|
||||
echo "REMOTE=$REPO_NAME" >> "$IFS/.env"
|
||||
echo "BRANCH=$REPO_BRANCH" >> "$IFS/.env"
|
||||
echo "USER=$USER_NAME" >> "$IFS/.env"
|
||||
|
||||
# Create .gitignore file to protect .env variables
|
||||
echo ".env" > "$IFS/.gitignore"
|
||||
if [ ! -d /usr/data/helper-script-backup/git-backup ]; then
|
||||
mkdir -p /usr/data/helper-script-backup/git-backup
|
||||
fi
|
||||
ENV=/usr/data/helper-script-backup/git-backup/.env
|
||||
echo "IFS=$IFS" > "$ENV"
|
||||
echo "GITHUB_TOKEN=$GITHUB_TOKEN" >> "$ENV"
|
||||
echo "REMOTE=$REPO_NAME" >> "$ENV"
|
||||
echo "BRANCH=$REPO_BRANCH" >> "$ENV"
|
||||
echo "USER=$USER_NAME" >> "$ENV"
|
||||
|
||||
# Insert .env to S52gitwatch.sh and move to init.d
|
||||
# Insert .env to init.d
|
||||
echo "Info: Copying file..."
|
||||
cp -f /usr/data/helper-script/files/git-backup/S52Git-Backup /etc/init.d/S52Git-Backup
|
||||
sed -i "2i source $IFS/.env" /etc/init.d/S52Git-Backup
|
||||
sed -i "2i source $ENV" /etc/init.d/S52Git-Backup
|
||||
echo "Info: Linking file..."
|
||||
ln -sf /usr/data/helper-script/files/git-backup/git-backup.cfg /usr/data/printer_data/config/Helper-Script/git-backup.cfg
|
||||
if grep -q "include Helper-Script/git-backup" /usr/data/printer_data/config/printer.cfg ; then
|
||||
echo "Info: Git Backup configurations are already enabled in printer.cfg file..."
|
||||
else
|
||||
echo "Info: Adding Git Backup configurations in printer.cfg file..."
|
||||
sed -i '/\[include printer_params\.cfg\]/a \[include Helper-Script/git-backup\.cfg\]' /usr/data/printer_data/config/printer.cfg
|
||||
fi
|
||||
echo "Info: Starting Git Backup service..."
|
||||
chmod +x /etc/init.d/S52Git-Backup
|
||||
/etc/init.d/S52Git-Backup start
|
||||
|
||||
/etc/init.d/S52Git-Backup start >/dev/null 2>&1
|
||||
echo "Info: Restarting Klipper service..."
|
||||
/etc/init.d/S55klipper_service start
|
||||
echo
|
||||
echo "${white}${green} ✓ Git Backup has been installed and configured successfully!${white}"
|
||||
echo
|
||||
exit 0
|
||||
fi
|
||||
|
||||
|
@ -225,16 +300,13 @@ else
|
|||
fi
|
||||
|
||||
if [ -d "$TARGET" ]; then # if the target is a directory
|
||||
|
||||
TARGETDIR=$(echo "$IN" | sed -e "s/\/*$//") # dir to CD into before using git commands: trim trailing slash, if any
|
||||
|
||||
# construct inotifywait-commandline
|
||||
if [ "$(uname)" != "Darwin" ]; then
|
||||
INW_ARGS="-qmr -e $EVENTS $TARGETDIR"
|
||||
fi
|
||||
GIT_ADD="git add -A ." # add "." (CWD) recursively to index
|
||||
GIT_COMMIT_ARGS="-a" # add -a switch to "commit" call just to be sure
|
||||
|
||||
else
|
||||
stderr "Error: The target is neither a regular file nor a directory."
|
||||
exit 3
|
||||
|
@ -283,7 +355,7 @@ while true; do
|
|||
if [ -n "$line" ]; then
|
||||
# Process changes
|
||||
if [ -n "$DATE_FMT" ]; then
|
||||
COMMITMSG=$(echo "$COMMITMSG" | awk -v date="$(date "$DATE_FMT")" '{gsub(/%d/, date)}1') # splice the formatted date-time into the commit message
|
||||
COMMITMSG=$(echo "$COMMITMSG") # splice the formatted date-time into the commit message
|
||||
fi
|
||||
|
||||
cd "$TARGETDIR" || {
|
||||
|
@ -299,11 +371,10 @@ while true; do
|
|||
|
||||
$GIT_ADD # add file(s) to index
|
||||
$GIT commit $GIT_COMMIT_ARGS -m "$COMMITMSG" # construct commit message and commit
|
||||
|
||||
if [ -n "$PUSH_CMD" ]; then
|
||||
echo "Push command is $PUSH_CMD"
|
||||
eval "$PUSH_CMD"
|
||||
pkill 'inotifywait'
|
||||
killall -q inotifywait
|
||||
timeout
|
||||
fi
|
||||
fi
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue