Last active
October 14, 2024 09:14
-
-
Save CodeBrauer/47ffc2a16919a9b359e5da7563777acc to your computer and use it in GitHub Desktop.
simple minecraft server status / running script for local/lan server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# === Configuration Variables === | |
SCREEN_NAME="mc" # Screen session name for Minecraft | |
SERVER_JAR="server.jar" # Minecraft server jar file | |
MEMORY="-Xmx1024M -Xms1024M" # Java memory allocation for the server | |
SERVER_PATH="/root/services/minecraft" # Path to your Minecraft server directory | |
BACKUP_PATH="/root/services/minecraft-backups" # Path to store backups | |
BACKUP_RETENTION_DAYS=14 # Number of days to retain backups | |
# Files and directories to exclude from backup | |
EXCLUDE_FILES=( | |
"$SERVER_JAR" # Exclude the server JAR file | |
"./libraries/*" # Exclude the libraries folder | |
"./versions/*" # Exclude the versions folder | |
"./logs/*" # Exclude the logs folder | |
) | |
# === Function to start the server === | |
start_server() { | |
if screen -list | grep -q "$SCREEN_NAME"; then | |
echo "Minecraft server is already running!" | |
else | |
echo "Starting Minecraft server..." | |
cd "$SERVER_PATH" | |
screen -AmdLS "$SCREEN_NAME" java $MEMORY -jar "$SERVER_JAR" nogui | |
echo "Minecraft server started." | |
fi | |
} | |
# === Function to stop the server === | |
stop_server() { | |
if screen -list | grep -q "$SCREEN_NAME"; then | |
echo "Saving the world and stopping Minecraft server..." | |
screen -S "$SCREEN_NAME" -X stuff "save-all\n" # Save the world | |
sleep 2 # Give it time to save | |
screen -S "$SCREEN_NAME" -X stuff "stop\n" # Stop the server | |
sleep 5 # Allow some time for the server to shut down | |
echo "Minecraft server stopped." | |
else | |
echo "Minecraft server is not running!" | |
fi | |
} | |
# === Function to check the status of the server === | |
status_server() { | |
if screen -list | grep -q "$SCREEN_NAME"; then | |
echo "Minecraft server is running." | |
else | |
echo "Minecraft server is not running." | |
fi | |
} | |
# === Function to create a backup === | |
backup_server() { | |
echo "Backing up the Minecraft server..." | |
# Stop the server and save the world before backing up | |
stop_server | |
# Create timestamp for the backup | |
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") | |
BACKUP_FILE="$BACKUP_PATH/minecraft-backup-$TIMESTAMP.zip" | |
# Ensure the backup directory exists | |
mkdir -p "$BACKUP_PATH" | |
# Change to the server directory to ensure correct paths | |
cd "$SERVER_PATH" | |
# Prepare the exclude options for the zip command | |
EXCLUDE_ARGS=() | |
for FILE in "${EXCLUDE_FILES[@]}"; do | |
EXCLUDE_ARGS+=("-x" "$FILE") | |
done | |
# Create a zip backup of the server directory, excluding specified files and directories | |
echo "Creating backup..." | |
zip -r "$BACKUP_FILE" . "${EXCLUDE_ARGS[@]}" | |
echo "Backup created: $BACKUP_FILE" | |
# Delete backups older than the retention period | |
find "$BACKUP_PATH" -type f -name "*.zip" -mtime +$BACKUP_RETENTION_DAYS -exec rm {} \; | |
echo "Old backups (older than $BACKUP_RETENTION_DAYS days) have been deleted." | |
# Start the server again after the backup | |
start_server | |
} | |
# === Function to send a Minecraft command to the server === | |
send_command() { | |
if screen -list | grep -q "$SCREEN_NAME"; then | |
COMMAND="$*" | |
echo "Sending command to Minecraft server: $COMMAND" | |
screen -S "$SCREEN_NAME" -X stuff "$COMMAND\n" | |
echo "Command sent." | |
else | |
echo "Minecraft server is not running!" | |
fi | |
} | |
# === Main script logic === | |
case "$1" in | |
start) | |
start_server | |
;; | |
stop) | |
stop_server | |
;; | |
status) | |
status_server | |
;; | |
restart) | |
stop_server | |
sleep 5 | |
start_server | |
;; | |
backup) | |
backup_server | |
;; | |
cmd) | |
shift | |
send_command "$@" | |
;; | |
*) | |
echo "Usage: $0 {start|stop|status|restart|backup|cmd <minecraft-command>}" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment