Skip to content

Instantly share code, notes, and snippets.

@AgentLoneStar007
Last active August 26, 2025 19:32
Show Gist options
  • Select an option

  • Save AgentLoneStar007/f3e7fb5729c5be75c6e01854c399cae5 to your computer and use it in GitHub Desktop.

Select an option

Save AgentLoneStar007/f3e7fb5729c5be75c6e01854c399cae5 to your computer and use it in GitHub Desktop.
An example backup script for Minecraft servers on Linux. Requires the zip utility.
#!/bin/bash
# Set the script to stop when an error occurs.
set -eo pipefail
# Get the current date as a variable
printf -v DATE '%(%m-%d-%Y)T'
# Get the server's directory location and make sure we're in that directory
SERVER_DIR_PATH=$(dirname "$(realpath $0)")
cd $SERVER_DIR_PATH
# Script variables. Customize to your liking.
BACKUP_FILE_NAME="Server $DATE Backup"
## $PWD will put the backups folder in the server directory. If you want
## to provide a directory outside of the server's directory, put the
## absolute path to the directory here.
BACKUP_DIRECTORY="$PWD/backups/"
# Announce server backup in console
echo "Started server backup..."
# Append the file extension to the file name and create the counter var
NAME="${BACKUP_FILE_NAME}.zip"
COUNTER=1
# Create the backups directory if it doesn't exist
echo "Checking for backup directory..."
if [ ! -d "$BACKUP_DIRECTORY" ]; then
echo "Backups directory does not exist. Creating folder..."
mkdir -p "$BACKUP_DIRECTORY"
fi
# Now, check if a file with the current name already exists inside the backups directory
# If it does, a while loop will increment the counter and create a new filename until a unique one is found
while [ -f "$BACKUP_DIRECTORY/$NAME" ]; do
NAME="${BACKUP_FILE_NAME}-${COUNTER}.zip"
COUNTER=$((COUNTER + 1))
done
# Get the name of this script
SCRIPT_NAME=$(basename "$0")
# Compress the files, ignoring this script and the backup directory
zip -r -q "$NAME" * -x "$(basename "$BACKUP_DIRECTORY/")/*" "$SCRIPT_NAME"
# And move the archive to the backups directory
mv "$NAME" "$BACKUP_DIRECTORY"
echo "Backup completed! File located at \"$BACKUP_DIRECTORY/$NAME\"."
@indoorjetpacks
Copy link

was modifying this a little and troubleshooting, I believe export name="Server $date Backup.zip" be export NAME='Server $date Backup.zip" instead since the rest of the variable references it as $NAME

@AgentLoneStar007
Copy link
Author

Thanks for reminding me this existed.

Along with that issue, this script will also include any existing backups (for a possibly huge file size), it will overwrite any previous backups made the same day without confirmation, and it will use the directory layout from the root file system as the directory layout in the archive. (Example: when you create a backup, it will use the directory layout "/home/user/Desktop/Minecraft Server/" if your server is at that directory.) I'll rewrite this and replace it with a better version when I can.

@AgentLoneStar007
Copy link
Author

Updated. The script will now archive the files in a directory layout relative to the server's directory and not the root directory, it will create a new backup every time the script is used, and it will ignore the backups directory in the archive.

@indoorjetpacks
Copy link

Ha, that was all what I was also modifying it to fix, got mostly the same results, but the counter is a good idea that I hadn't thought of.
Thanks for the update! The error check at the top is nice too :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment