Skip to content

Commit

Permalink
readd scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitaldot committed Jun 30, 2024
1 parent 8becfd3 commit e5880a0
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
83 changes: 83 additions & 0 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#! /bin/bash

cd "$(dirname "$0")"

NUMBER_OF_BACKUPS_TO_KEEP=5

if [ $# -lt 2 ]; then
echo "Usage: $0 source_dir target_dir"
exit 1
fi

SILENT=false
if [ $# -lt 3 ]; then
SILENT=$3
fi

source_dir=$1
target_dir=$2

SCREEN_SESSION=$(echo ${STY#*.} | cut -d. -f1 | cut -d- -f3)
backup_list="$source_dir/backuplist.txt"

save_off () {
COMMAND="save-off" make -C "$source_dir" command
}
save_on () {
COMMAND="save-on" make -C "$source_dir" command
}
post_status () {
if [ -v DEDI_LOGS_CHANNEL_WEBHOOK ]; then
curl -H "Content-Type: application/json" -X POST -d "{\"content\": \"**[$SCREEN_SESSION]** $1\"}" $DEDI_LOGS_CHANNEL_WEBHOOK
else
echo "$1"
fi
}

save_off

cd $target_dir
ls -1t | tail -n +$NUMBER_OF_BACKUPS_TO_KEEP | xargs rm
cd "$(dirname "$0")/.."

zipname="$(date +"%Y-%m-%d_%H-%M").zip"

backup_list=$(while read -r line; do echo "$line"; ((num_files++)); done < "$backup_list")
backup_list=$(echo "$backup_list" | tr " " "\n")
backup_output_file="$target_dir/$zipname"

post_status "Creating backup $zipname"

num_files=$(echo "$backup_list" | wc -l)

fail=false
i=0
echo "$backup_list" | while read -r line; do
post_status "Adding \`$line\` to backup... ($((++i))/$num_files)"
l="$source_dir/$line"
if zip -ur "$backup_output_file" "$l"; then
continue
else
excode=$?
if [ $excode -eq 9 ]; then
post_status ":warning: Backup interrupted."
fail=true
break
elif [ $excode -eq 12 ]; then
post_status ":warning: Backup failed: \`$line\` not found"
fail=true
break
else
post_status ":warning: Failed to add \`$line\` to backup"
fi
fi
done

post_status "Backup complete."
save_on

if [ "$fail" = true ]; then
exit 1
fi

exit 0
31 changes: 31 additions & 0 deletions scripts/make_minecraft_servers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /bin/bash

# This script takes a Make target as an argument, and then
# executes it for all Minecraft servers.
#
# For example, `bash MAKE_MINECRAFT_SERVERS.sh backup` would
# execute `make backup` for all of the Minecraft servers.

if [ $# -lt 1 ]; then
echo "Usage: $0 make_target [command]"
exit 1
fi

if [ -f ../.config/.env ]
then
export $(cat ../.config/.env | xargs)
fi

cd "$(dirname "$0")"
cd ../minecraft/servers

for server in $MINECRAFT_SERVERS; do
cd $server
if [ "$1" == "command" ]; then
COMMAND=$2 make $1
else
make $1
fi
cd ..
done
exit 0
17 changes: 17 additions & 0 deletions scripts/restart_minecraft_servers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#! /bin/bash

cd "$(dirname "$0")"

bash ./make_minecraft_servers.sh command "say Automatic restart in 5 minutes"
bash ./make_minecraft_servers.sh command "discordsrv bcast Automatic server restart in 5 minutes"

sleep 4m

bash ./make_minecraft_servers.sh command "say Automatic restart in 1 minute"

sleep 1m

bash ./make_minecraft_servers.sh command "say Automatic restart now"
bash ./make_minecraft_servers.sh command "stop"

exit 0
28 changes: 28 additions & 0 deletions scripts/server_status_to_webhook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /bin/bash
IFS=$' \t\r\n'

cd "$(dirname "$0")"

if [ -f ../.config/.env-minecraft ]
then
export $(cat ../.config/.env-minecraft | xargs)
fi

# same as BACKUP.sh
post_status () {
msg=$(echo $1 | sed -r 's/^.{2}//' | sed 's/.$//')
curl -H "Content-Type: application/json" -X POST -d "{\"content\": \"$msg\"}" "$DEDI_LOGS_CHANNEL_WEBHOOK"
}

# https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
mem=$(free -m | awk 'NR==2{printf "%s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }')
disk=$(df -h | awk '$NF=="/"{printf "%d/%dGB (%s)\n", $3,$2,$5}')
screens=$(screen -ls | grep Detached | awk '{print "/ " $1 }')

server_status="$(
echo '**Memory:** `'$mem'`, **Disk:** `'$disk'`'
echo '> **Active screens:** `'$screens'`'
)"

echo "mem: ${mem}, disk: ${disk}, screens: ${screens}"
post_status "${server_status@Q}"

0 comments on commit e5880a0

Please sign in to comment.