First attempt at linux scripting - Spigot server updater

Discussion in 'Spigot Discussion' started by geraintthomas, Jan 19, 2020.

  1. Go easy on me! Not only is this my first time coding on a linux platform, but is also my first post here. Been seeing a lot of useful info on these forums so thought I'd join in.

    So hello!

    Anyway. When updating the Minecraft Spigot server on my Raspberry Pi, the whole process of moving the world and server info to a backup folder, deleting the lot, downloading build tools, running it with a java command, waiting and then putting your server info back in after it all was a little long winded for someone like me who's new to all of the Linux stuffage.

    I decided to throw together a script that does it all for you, and thought I'd throw it on here to see what you guys think. It's made life easier for me when updates are released.

    What it does:
    - Asks for server folder location (home/pi/minecraft is default)
    - Asks for version to install (latest is default)

    - Creates a directory at 'home/pi/minecraft_backup'
    - Moves server files (properties, whitelist, worlds, etc) to the backup folder
    - Deletes the original server folder
    - Creates a new empty one
    - Downloads the latest BuildTools.jar to that folder
    - Runs the BuildTools.jar and builds the server
    - When complete, moves your server files and world folders back into the new folder
    - Deletes the 'minecraft_backup' folder
    - States completion





    Here's the code:

    Code (Text):

    # Minecraft Server Updater Script. Ver 1.0. Geraint Thomas

    echo -e "\e[1;4mMinecraft Server Updater.\e[0m"


    echo "Step 01: Specify location of Minecraft server folder below..."

    echo -e "\e[1;96mDirectory to install:\e[0m"
    read -p "" -i /home/pi/minecraft -e serverpath

    echo "Step 02: Specify which version of Minecraft server to install below..."

    echo -e "\e[1;96mVersion to install:\e[0m"
    read -p "" -i latest -e version

    echo -e "\e[1;95mReady.\e[0m"
    read -n 1 -s -r -p "Press any key to begin installation."

    echo "Updating Minecraft Server to $version version."

    sleep 1

    echo "Backing up world and server properties..."

    sleep 2

    mkdir /home/pi/minecraft_backup
    mv $serverpath/plugins /home/pi/minecraft_backup/plugins
    mv $serverpath/world /home/pi/minecraft_backup/world
    mv $serverpath/world_nether /home/pi/minecraft_backup/world_nether
    mv $serverpath/world_the_end /home/pi/minecraft_backup/world_the_end
    mv $serverpath/banned-ips.json /home/pi/minecraft_backup/banned-ips.json
    mv $serverpath/banned-players.json /home/pi/minecraft_backup/banned-players.json
    mv $serverpath/eula.txt /home/pi/minecraft_backup/eula.txt
    mv $serverpath/ops.json /home/pi/minecraft_backup/ops.json
    mv $serverpath/ /home/pi/minecraft_backup/
    mv $serverpath/usercache.json /home/pi/minecraft_backup/usercache.json
    mv $serverpath/whitelist.json /home/pi/minecraft_backup/whitelist.json

    echo "Deleting old server..."

    sleep 2

    sudo rm -r $serverpath/
    mkdir $serverpath/
    cd $serverpath/

    echo "Downloading Build Tools..."

    sleep 2


    echo "Rebuilding server. This may take up to 45 minutes."

    sleep 3

    java -Xmx1024M -jar BuildTools.jar --rev $version

    echo "Restoring world and server properties..."

    mv /home/pi/minecraft_backup/plugins $serverpath/plugins
    mv /home/pi/minecraft_backup/world $serverpath/world
    mv /home/pi/minecraft_backup/world_nether $serverpath/world_nether
    mv /home/pi/minecraft_backup/world_the_end $serverpath/world_the_end
    mv /home/pi/minecraft_backup/banned-ips.json $serverpath/banned-ips.json
    mv /home/pi/minecraft_backup/banned-players.json $serverpath/banned-players.json
    mv /home/pi/minecraft_backup/eula.txt $serverpath/eula.txt
    mv /home/pi/minecraft_backup/ops.json $serverpath/ops.json
    mv /home/pi/minecraft_backup/ $serverpath/
    mv /home/pi/minecraft_backup/usercache.json $serverpath/usercache.json
    mv /home/pi/minecraft_backup/whitelist.json $serverpath/whitelist.json
    sudo rm -r /home/pi/minecraft_backup

    echo "Finished."
    echo "Press any key to exit."

    It's full of 'echo' commands at the start just to give some nice spacing within Terminal. Easier to read.

    It's relatively simple, and it's probably full of lazy scripting. Plus there's probably something out there that's similar, but even so it was quite a nice challenge for me and a nice way of getting into scripting.

    Let me know what you think!
    #1 geraintthomas, Jan 19, 2020
    Last edited: Jan 20, 2020
    • Useful Useful x 1
  2. Strahan


    Looks nice. One thing though; you aren't "backing up" the server. You are just moving it. You should be copying the files to the backup folder, not moving them. You also do not need to pull them back from backup once done; it's a backup, it's meant to stay there.

    For example, lets say you are running the script to update to 1.16 when it comes out. Your script will create the backup folder, move all the stuff there. You run BT, you get the 1.16 jar. Now you move it all back and start. You then find out 1.16 somehow hosed up your world and you want to revert to 1.15, however you're out of luck because there is only one instance of your world. The backup folder is empty since you moved everything back.

    PS there is no need to wipe out the server path prior to the upgrade either. All you needs do is copy the new Spigot jar over when complete. Spigot detects and handles in-place upgrades and performs data conversion of legacy files as required.

    Lastly, if your BT takes 45 minutes you may be better off running it off your regular PC then just copying the resultant jar over the network to the Pi. It takes less than 10 minutes on my PC. Much less time for the server to be down, and actually, if you run it somewhere other than the server the server can stay up while it builds and only needs to be downed for the file moving so even less time there as well.
    #2 Strahan, Jan 20, 2020
    Last edited: Jan 20, 2020
  3. Why don't you just copy the entire $serverpath to the backup (and vice versa)? What's the point in all those hard coded file names
    • Agree Agree x 1
  4. Very true about the backup, will change that so that it copies it. Nice thinking there.

    The 45 minutes was actually left there from when I originally made it. A website stated it would take around that so I wrote 44, but it does take 10-15 minutes. Need to change that...

    Because some of the files are being rebuilt by the new BuildTools. If I did that and copied them back after it was finished, I'd be overwriting the new server with the old.
  5. make the backup, (via copy, not moving), build spigot in another dir (/tmp is pretty standard), then overwrite the main spigot with the new spigot. Your method doesn't account for extra worlds, renamed worlds, spigot/paper/bukkit configurations, and shitty plugins that don't know how to use the data folder.
  6. Ah, that makes total sense. Thanks for the help. I'll give it another go but, from what I remember, when I tried to use BuildTools while there were existing files in the folder, the build failed with "Build Failure - failed to execute goal", with some Apache maven errors. I'll try it again, but if not I'll try deleting the apache maven folder in the script so that it makes a new one.

    I've also just changed the script so that the backup folder is a variable, so that I only have to change one path rather than all of them for the time being until I implement this change.

    Is there any server updater like this about? Or does everyone just do it manually?
  7. I'd assume most people just make their own scripts, they're incredibly simple. Also, I specifically mentioned to build spigot in another directory, so I'm not sure why you're saying that.
  8. Yes I got your specifics, but it was purely to make the script a bit neater. Rather than change every line to /tmp, I've changed it to $backup, then set backup=/tmp. Seems a little pointless, but easier to change the directory in the future if I ever needed to.
  9. Oh! Spigot is built in a tmp, then copy over. Apologies, just realised what you meant. Had an idiot moment.

    Cheers for that, I'll give it a try.
    • Funny Funny x 1
  10. Here's my take on your script anyway (minus the clutter, you can implement that yourself if you must), although I usually wouldn't use read commands, but rather arguments for automation purposes.
    Code (Text):


    read -p "Server location (no trailing slash): " -e server
    read -p "Jar name: " -e jar
    read -p "Backup location: " -i "${server}/../bak/" -e backup
    read -p "Version: " -i latest -e version
    read -n 1 -s -r -p "Press any key to begin installation."

    cd /
    cp -a $server $backup
    mkdir -p /tmp/spigot
    cd /tmp/spigot
    java -jar BuildTools.jar --rev $version
    cp spigot*.jar "${server}/${jar}"
    cd .. && rm -rf spigot
    I haven't tested it so wouldn't be surprised if it didn't work
  11. Thanks for that script! I was actually having some trouble trying to update my minecraft server on my raspberry pi (woopsi) and I didn't really took time to code a linux script (cause I hate it, ugh)