Skip to content

Commit

Permalink
add backup notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Mar 11, 2024
1 parent 3158088 commit 350fca9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 34 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,21 @@ For exact details please refer to ``backup.sh``.

- ``BACKUP_FILE_FORMAT``:
This can be used to specify the backup timestamp.
It uses the date command line tool to interpret the placeholder varibales(``date ${BACKUP_FILE_FORMAT}``).
It uses the date command line tool to interpret the placeholder varibales (``date ${BACKUP_FILE_FORMAT}``).
- ``BACKUP_TARGET``: Rclone backup target name
- ``MAX_AGE_BACKUP_FILES``:
Specify the maximum length of time a backup file should be kept. One backup file is always kept.
- ``POST_START_BACKUP``: Enable backup after server stop
- ``PRE_START_BACKUP``: Enable pre start backup
- ``BACKUP_SUCCESS_SCRIPT``: Will be executed when the backup creation was successful.
- ``BACKUP_FAILED_SCRIPT``: Will be executed when the backup creation has failed.

An example for a `BACKUP_FAILED_SCRIPT` could look as following.
```
curl -u "user:password" -d "$MESSAGE" "https://ntfy.example.com/minecraft_backups?title=Backup%Failed"
```

The `$MESSAGE` environment variable will contain the stdout and stderr from the backup script.

### Why pre and post backups

Expand Down
18 changes: 10 additions & 8 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ source ./scripts/utils.sh

if [ -f /run/secrets/rcloneconfig ]; then
if [ -f /etc/mcserver/config/bukkit.yml ]; then
printf "\n[${CYAN} INFO ${RESET}] Starting backup\n"
echo -e "\n[${CYAN} INFO ${RESET}] Starting backup"
mkdir -p "$WDIR"

FILENAME="$1-$(date ${BACKUP_FILE_FORMAT}).zip"
FILENAME="$1-$(date "${BACKUP_FILE_FORMAT}").zip"

# Start zipping server
zip -9rq "${WDIR}/$FILENAME" "/etc/mcserver/"

# Rclone move
printf "\n[${CYAN} INFO ${RESET}] Start uploading of backup $FILENAME\n"
rclone --config /run/secrets/rcloneconfig move "${WDIR}/$FILENAME" "$BACKUP_TARGET" -v &&
rclone --config /run/secrets/rcloneconfig --min-age $MAX_AGE_BACKUP_FILES delete "$BACKUP_TARGET" -v
echo -e "\n[${CYAN} INFO ${RESET}] Start uploading of backup $FILENAME"
rclone --config /run/secrets/rcloneconfig move "${WDIR}/$FILENAME" "$BACKUP_TARGET" -v

echo -e "\n[${CYAN} INFO ${RESET}] Cleaning up old backups"
rclone --config /run/secrets/rcloneconfig --min-age "$MAX_AGE_BACKUP_FILES" delete "$BACKUP_TARGET" -v

# Delete WDIR
rm -rf "$WDIR"
printf "\n[${CYAN} INFO ${RESET}] Finished backup\n"
echo -e "\n[${CYAN} INFO ${RESET}] Finished backup"
else
printf "\n[${CYAN} INFO ${RESET}] Minecraft server is not initialized\n"
echo -e "\n[${CYAN} INFO ${RESET}] Minecraft server is not initialized"
fi

else
printf "\n[${CYAN} INFO ${RESET}] Backup is disabled\n"
echo -e "\n[${CYAN} INFO ${RESET}] Backup is disabled"
fi

exit 0
18 changes: 9 additions & 9 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ set -e
source ./scripts/utils.sh

is_true "$BUILD_CACHING" && {
printf "\n[${CYAN} INFO ${RESET}] BUILD CACHING IS ACTIVATED\n\n"
echo -e "\n[${CYAN} INFO ${RESET}] BUILD CACHING IS ACTIVATED\n"

SPIGOT_REF=$(curl https://hub.spigotmc.org/versions/${MC_VERSION}.json | jq -r '.refs.Spigot')
SPIGOT_REF=$(curl "https://hub.spigotmc.org/versions/${MC_VERSION}.json" | jq -r '.refs.Spigot')

[ -f spigot_ref.txt ] &&\
[ "$(cat spigot_ref.txt)" == "$SPIGOT_REF" ] && {
printf "\n[${CYAN} INFO ${RESET}] VERSION UP TO DATE (${PURPLE}${SPIGOT_REF}${RESET}) - USING CACHED BUILD\n\n"
echo -e "\n[${CYAN} INFO ${RESET}] VERSION UP TO DATE (${PURPLE}${SPIGOT_REF}${RESET}) - USING CACHED BUILD\n"
exit 0
}

echo $SPIGOT_REF > spigot_ref.txt
echo "$SPIGOT_REF" > spigot_ref.txt

printf "\n[${CYAN} INFO ${RESET}] CACHED BUILD OUTDATED - BUILDING LATEST VERSION\n\n"
echo -e "\n[${CYAN} INFO ${RESET}] CACHED BUILD OUTDATED - BUILDING LATEST VERSION\n"
}

mkdir $WDIR || true
Expand All @@ -29,10 +29,10 @@ cd $WDIR
curl https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar \
--output ./BuildTools.jar

java -jar ./BuildTools.jar --rev $MC_VERSION
java -jar ./BuildTools.jar --rev "$MC_VERSION"

mv ./spigot-*.jar $ORIGIN/spigot.jar
mv ./spigot-*.jar "$ORIGIN/spigot.jar"

cd $ORIGIN
cd "$ORIGIN"

rm -r -f $WDIR
rm -r -f "$WDIR"
12 changes: 10 additions & 2 deletions scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
set -e
source ./scripts/utils.sh

backup() {
if output=$(./scripts/backup.sh "$1" 2>&1); then
notify "$BACKUP_SUCCESS_SCRIPT" "$output"
else
notify "$BACKUP_FAILED_SCRIPT" "$output"
fi
}

./scripts/information.sh
is_true "$PRE_START_BACKUP" && ./scripts/backup.sh "pre" &
is_true "$PRE_START_BACKUP" && backup "pre" &
./scripts/build.sh
./scripts/run.sh
is_true "$POST_START_BACKUP" && ./scripts/backup.sh "post"
is_true "$POST_START_BACKUP" && backup "post"
37 changes: 23 additions & 14 deletions scripts/utils.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@

CYAN="\033[0;36m"
PURPLE="\033[0;35m"
RESET="\033[0m"

function is_true {
if [ "$1" == "true" ] ||\
[ "$1" == "t" ] ||\
[ "$1" == "TRUE" ] ||\
[ "$1" == "T" ] ||\
[ "$1" == "1" ]; then
return 0
fi
return 1

CYAN="\033[0;36m"
PURPLE="\033[0;35m"
RESET="\033[0m"

function is_true {
if [ "$1" == "true" ] ||\
[ "$1" == "t" ] ||\
[ "$1" == "TRUE" ] ||\
[ "$1" == "T" ] ||\
[ "$1" == "1" ]; then
return 0
fi
return 1
}

function notify() {
script="$1"

if [ -n "$script" ]; then
export MESSAGE="$2"
bash -c "$script"
fi
}

0 comments on commit 350fca9

Please sign in to comment.