@@ -8,38 +8,29 @@ BACKUP_DIR="$INSTALL_DIR/backups"
88echo " 🔍 Checking for required dependencies..."
99for cmd in java curl jq; do
1010 if ! command -v $cmd & > /dev/null; then
11- echo " ❌ Error: $cmd is not installed. Ensure dependencies are pre-installed in the workflow environment ."
11+ echo " ❌ Error: $cmd is not installed. Please install it and re-run the script ."
1212 exit 1
1313 fi
1414done
1515
16- # === INPUT FROM ENVIRONMENT VARIABLES ===
17- MC_VERSION=${MC_VERSION:- " 1.21.5" } # Default to 1.21.5 if not set
18- SERVER_TYPE=${SERVER_TYPE:- " paper" } # Default to paper if not set
16+ # Update system packages and install dependencies
17+ sudo apt update && sudo apt install -y openjdk-21-jre-headless curl jq || {
18+ echo " ❌ Error: Failed to install dependencies. Check your internet connection and try again."
19+ exit 1
20+ }
1921
20- echo " 🌐 Minecraft Version: $MC_VERSION "
21- echo " 🛠️ Server Type: $SERVER_TYPE "
22+ # === USER INPUT FOR SERVER JAR ===
23+ echo " 🌐 Select the Minecraft version and server type:"
24+ read -rp " Enter Minecraft version (e.g., 1.21.5): " MC_VERSION
25+ echo " Available server types: fabric, paper, forge, vanilla"
26+ read -rp " Enter server type: " SERVER_TYPE
2227
2328# === FETCH SERVER JAR URL ===
2429echo " 🌐 Fetching server JAR URL from mcutils.com..."
25- API_URL=" https://mcutils.com/api/v1/jars?version=$MC_VERSION &type=$SERVER_TYPE "
26- RAW_RESPONSE=$( curl -s " $API_URL " )
27-
28- # Log the raw API response for debugging
29- echo " Raw API Response: $RAW_RESPONSE "
30+ SERVER_JAR_URL=$( curl -s " https://mcutils.com/api/v1/jars?version=$MC_VERSION &type=$SERVER_TYPE " | jq -r ' .url' )
3031
31- # Validate if the response is valid JSON
32- if ! echo " $RAW_RESPONSE " | jq empty 2> /dev/null; then
33- echo " ❌ Error: Received invalid JSON from the API. Please check the API response and your inputs."
34- exit 1
35- fi
36-
37- # Parse the response using jq
38- SERVER_JAR_URL=$( echo " $RAW_RESPONSE " | jq -r ' .url' )
39-
40- if [[ " $SERVER_JAR_URL " == " null" || -z " $SERVER_JAR_URL " ]]; then
32+ if [ " $SERVER_JAR_URL " == " null" ] || [ -z " $SERVER_JAR_URL " ]; then
4133 echo " ❌ Error: Could not find a server JAR for version $MC_VERSION and type $SERVER_TYPE . Please check your inputs and try again."
42- echo " Debug Info: The response from the server API was: $RAW_RESPONSE "
4334 exit 1
4435fi
4536
@@ -64,25 +55,42 @@ curl -L -o "$SERVER_JAR" "$SERVER_JAR_URL" || {
6455echo " ✅ Accepting Minecraft EULA..."
6556echo " eula=true" > eula.txt
6657
67- # === START SERVER ===
68- echo " ⚙️ Starting Minecraft server..."
69- java -Xms2G -Xmx4G -jar " $SERVER_JAR " nogui &
70- SERVER_PID=$!
71-
72- echo " ⏳ Waiting for the server to start..."
73- sleep 30
74-
75- if ps -p $SERVER_PID > /dev/null; then
76- echo " ✅ Server for version $MC_VERSION and type $SERVER_TYPE started successfully."
77- kill $SERVER_PID
78- wait $SERVER_PID || true
79- else
80- echo " ❌ Error: Server for version $MC_VERSION and type $SERVER_TYPE failed to start."
58+ # === CREATE START SCRIPT ===
59+ echo " ⚙️ Creating start script..."
60+ cat << EOF > start-server.sh
61+ #!/bin/bash
62+ cd "$INSTALL_DIR "
63+ echo "🟢 Starting Minecraft server..."
64+ java -Xms2G -Xmx4G -jar "$SERVER_JAR " nogui
65+ EOF
66+ chmod +x start-server.sh
67+
68+ # === CREATE DAILY BACKUP SCRIPT ===
69+ echo " ⚙️ Creating backup script..."
70+ cat << EOF > backup.sh
71+ #!/bin/bash
72+ cd "$INSTALL_DIR "
73+ tar -czf "$BACKUP_DIR /world-\$ (date +%F).tar.gz" world || {
74+ echo "⚠️ Warning: Failed to create backup. Check permissions."
8175 exit 1
82- fi
83-
84- # === CLEANUP ===
85- echo " 🧹 Cleaning up server files..."
86- rm -rf " $INSTALL_DIR "
76+ }
77+ find "$BACKUP_DIR " -type f -mtime +7 -delete || {
78+ echo "⚠️ Warning: Failed to clean old backups. Check permissions."
79+ }
80+ EOF
81+ chmod +x backup.sh
82+
83+ # === ADD TO CRONTAB ===
84+ echo " 📅 Adding scripts to crontab..."
85+ (crontab -l 2> /dev/null; echo " @reboot $INSTALL_DIR /start-server.sh" ) | crontab -
86+ (crontab -l 2> /dev/null; echo " 0 3 * * * $INSTALL_DIR /backup.sh" ) | crontab -
87+
88+ # === START THE SERVER ===
89+ echo " 🚀 Starting the server..."
90+ ./start-server.sh || {
91+ echo " ❌ Error: Failed to start the server. Check the logs for more details."
92+ exit 1
93+ }
8794
88- echo " ✅ Successfully tested server for version $MC_VERSION and type $SERVER_TYPE ."
95+ echo " ✅ Minecraft server $MC_VERSION ($SERVER_TYPE ) is up and running."
96+ echo " 🌍 World backups daily at 3 AM in: $BACKUP_DIR "
0 commit comments