SHARE
TWEET

Untitled

a guest Jun 4th, 2011 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # Simple bukkit startup script by Piffey
  3. # This is by no means a clean script, but it will get the job done.
  4. # Suggestions? Improvements? Let me know at Piffey.com
  5.  
  6. # All of the screen stuff was adapted from ragon's <frederik.engels24 AT gmail DOT com> script for his Arch Linux package.
  7. # Reason: I don't use screen, but it's likely the best option for anyone looking for a simple script they can run and ignore.
  8. # His script worked perfect for my first server too. Thanks ragon.
  9.  
  10.  
  11. ## 03/27/11 Updates (ryunp)
  12. #    Modified for tmux
  13. #    Altered $dateformat into `_yymmdd-hhmmss` so you get: <world>_yymmdd-hhmmss
  14. #        This allows unique inter-minute backups
  15. #    Invocation variable ($bukkitinvoaction) instead of just RAM allocation
  16. #    Added bukkitstatus() to check offline/online status
  17. #        If online shows process activity/information
  18. #    Updated startbukkit() & stopbukkit() to check java process status after invoked/stopped
  19. #        Added friendlystop timing sequence to the stopbukkit() itself; checks for arg given in case statement
  20. #    updatebukkit() will save current JAR as <JAR>.old and rename newly downloaded file
  21. #    bukkitbackup() added zip + targz support by choosing, added customizable messaged for before/after
  22. #        Added a second backupmsg and completion var, tar & zip integration (if zip is installed; checks before attempting)
  23. #    Added status to argument case statement and catchall display, updated friendlystop
  24. ##
  25.  
  26. # Set these values to match your server's settings.
  27. bukkitdir="~/bukkit/"
  28. backupdir="~/bukkit/backups/"
  29. bukkitfilename="craftbukkit.jar"
  30. bukkitupdate="craftbukkit-updater.jar"
  31.  
  32. # Will omit backup messages if quotes left empty
  33. backupmsg="Backup in progress. Saving disabled for a moment."
  34. backupmsgdone="Level data successfully backed up!"
  35.  
  36. dateformat="_$(date '+%y%m%d-%H%M%S')"
  37.  
  38. # Determine additional archived level backups
  39. # Note: `zip` format is not native to most (all?) linux distro's, must install to work.
  40. backuplevelzip="true"
  41. backuplevelgz="false"
  42.  
  43. # Custom java binary location. Do not change unless custom installation was done, ex: `/opt/java/bin/java`.
  44. # This assumes java is in the default location: /usr/bin/java.
  45. javaloc="java"
  46.  
  47. # Invocation parameters. Set the amount of RAM you want to allocate, Java heap max and initial size.
  48. # Good practice is to have the numbers match.
  49. bukkitinvocation="$javaloc -Xmx2048M -Xms2048M -jar '$bukkitdir$bukkitfilename' nogui"
  50.  
  51. # Make sure you change this to the name of your world folder!
  52. # Add additional worlds by separating them with a white space, ex: =(world nether pvp chaos).
  53. declare -a worlds=(world)
  54. numworlds=${#worlds[@]}
  55.  
  56. # This currently points to the "preferred" release for bukkit which is kind of like a stable version.
  57. # Change this value if you want to use the snapshot release.
  58. bukkiturl=http://ci.bukkit.org/job/dev-CraftBukkit/promotion/latest/Recommended/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar
  59.  
  60. bukkitstatus()
  61. {
  62.     if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
  63.        echo "Bukkit server currently OFFLINE"
  64.        exit 0
  65.     else
  66.        echo "Bukkit server currently ONLINE:"
  67.  
  68.        # Get the Process ID of running JAR file
  69.        bukkitPID=`ps ax | grep -v grep | grep -v -i tmux | grep -v sh | grep "$bukkitfilename"`
  70.  
  71.        # Display process activity information of bukkit (5 character length pID)
  72.        top -n 1 -p ${bukkitPID:0:5} | grep ${bukkitPID:0:5}
  73.  
  74.        # Display prcoess infomation of bukkit
  75.        echo "$bukkitPID"
  76.     fi
  77. }
  78. startbukkit()
  79. {
  80.     if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
  81.         echo "Starting Bukkit server..."
  82.         cd $bukkitdir
  83.  
  84.         # Check for a tmux minecraft session already active; if so create a new window within, otherwise new session
  85.         # (Creating a new window in a minecraft tmux session will cause the session to stay active even after this scripts stopbukkit function is called)
  86.         if ! tmux ls | grep minecraft > /dev/null; then
  87.             tmux new -d -n bukkit -s minecraft "$bukkitinvocation"
  88.         else
  89.             echo "tmux minecraft sessions detected: creating window within!"
  90.             tmux neww -d -n bukkit -t minecraft "$bukkitinvocation"
  91.         fi
  92.  
  93.         # Check for process status, wait 7 seconds for plugin initialization
  94.         sleep 7
  95.         if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
  96.             echo "Bukkit server failed to start!"
  97.         else
  98.             echo "Bukkit server successfully started!"
  99.         fi
  100.     else
  101.         echo "Bukkit server is already running."
  102.         exit 0
  103.     fi
  104. }
  105.  
  106. stopbukkit()
  107. {
  108.     if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
  109.         echo "Bukkit server is not running."
  110.         exit 0
  111.     else
  112.         # Check for friendlystop trigger
  113.         if [ "$1" == "friendly" ]; then
  114.             echo "Sending 5 minute shutown warning"
  115.             tmux send -t minecraft:bukkit "say Going down in 5 minutes." C-m
  116.             sleep 240
  117.             echo "Sending 1 minute shutdown warning"
  118.             tmux send -t minecraft:bukkit "say Going down in 60 seconds." C-m
  119.             sleep 30
  120.             echo "Sending 30 second shutdown warning"
  121.             tmux send -t minecraft:bukkit "say Going down in 30 seconds." C-m
  122.             sleep 25
  123.             echo "Sending 5 second shutdown warning"
  124.             tmux send -t minecraft:bukkit "say Going down in 5 seconds." C-m
  125.             sleep 5
  126.         fi
  127.  
  128.         tmux send -t minecraft:bukkit 'stop' C-m
  129.  
  130.         # Check for process status, waiting 5 seconds for shutdown procedure to finish
  131.         sleep 5
  132.         if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
  133.             echo "Bukkit server successfully stopped!"
  134.         else
  135.             echo "Process failed to stop!"
  136.  
  137.             # Take more aggresive action against pID
  138.             echo "Attempting to kill rogue $bukkitfilename process..."
  139.  
  140.             # Get the Process ID of running JAR file
  141.             bukkitPID=`ps ax | grep -v grep | grep -v -i tmux | grep -v sh | grep "$bukkitfilename"`
  142.             kill ${bukkitPID:0:5}
  143.             sleep 1
  144.  
  145.             # Check for process status after pkill attempt
  146.             if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
  147.                 echo "$bukkitfilename process terminated!"
  148.             else
  149.                 echo "$bukkitfilename could not be killed!"
  150.                 exit 1
  151.             fi
  152.         fi
  153.     fi
  154. }
  155.  
  156. updatebukkit()
  157. {
  158.     # Alert players Bukkit server going down for repairs
  159.     if ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
  160.         tmux send -t minecraft:bukkit "say Going down for an update in 60 seconds." C-m
  161.         sleep 5
  162.         tmux send -t minecraft:bukkit "say Going down for an update in 5 seconds." C-m
  163.         sleep 5
  164.         stopbukkit
  165.     fi
  166.  
  167.     # Download new JAR
  168.     wget -O "$bukkitdir$bukkitupdate" "$bukkiturl"
  169.     sleep 1
  170.  
  171.     # Backup old JAR file and rename newly downloaded
  172.     mv -v "$bukkitdir$bukkitfilename" "$bukkitdir$bukkitfilename.old"
  173.     mv -v "$bukkitdir$bukkitupdate" "$bukkitdir$bukkitfilename"
  174.     echo "You must now manually start the Bukkit server."
  175. }
  176.  
  177. backupbukkit()
  178. {
  179.     echo "Starting multiworld backup..."
  180.     # Alert players backup is in progress
  181.     if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
  182.         echo "Bukkit server not running!"
  183.     else
  184.         if [ "$backupmsg" != "" ]; then tmux send -t minecraft:bukkit "say $backupmsg" C-m; fi
  185.         if [ "$backupmsg2" != "" ]; then tmux send -t minecraft:bukkit "say $backupmsg2" C-m; fi
  186.         tmux send -t minecraft:bukkit "save-all" C-m
  187.         sleep 5
  188.         tmux send -t minecraft:bukkit "save-off" C-m
  189.     fi
  190.  
  191.     # Check for backup directory
  192.     if ! [ -d $backupdir ];then mkdir -p $backupdir; fi
  193.  
  194.     # Determine if zip package is installed on host
  195.     if ! dpkg-query -W 'zip' > /dev/null; then backuplevelzip="false"; fi
  196.  
  197.     # Iterate through level backups
  198.     cd $backupdir
  199.     for ((i=0;i<$numworlds;i++)); do
  200.         echo "Copying '${worlds[$i]}' + '$bukkitfilename' to '$backupdir${worlds[$i]}$dateformat'..."
  201.         cp -R $bukkitdir${worlds[$i]} $backupdir${worlds[$i]}$dateformat
  202.         cp $bukkitdir$bukkitfilename $backupdir${worlds[$i]}$dateformat
  203.  
  204.         # TARgz backup sequence
  205.         if [ "$backuplevelgz" == "true" ]; then
  206.             echo "Creating tar archive file of '${worlds[$i]}$dateformat'..."
  207.             tar zcf ${worlds[$i]}$dateformat.tar.gz ${worlds[$i]}$dateformat/
  208.         fi
  209.  
  210.         # ZIP backup sequence
  211.         if [ "$backuplevelzip" == "true" ]; then
  212.              echo "Creating zip archive file of '${worlds[$i]}$dateformat'..."
  213.              zip -rq ${worlds[$i]}$dateformat ${worlds[$i]}$dateformat
  214.         fi
  215.     done
  216.     sleep 1
  217.  
  218.     # Alert players backup is finished
  219.     if ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
  220.         tmux send -t minecraft:bukkit "save-on" C-m
  221.         if [ "$backupmsgdone" != "" ]; then tmux send -t minecraft:bukkit "say $backupmsgdone" C-m; fi
  222.     fi
  223.     echo "Backup complete."
  224. }
  225.  
  226. case $1 in
  227.     status)
  228.         bukkitstatus
  229.         ;;
  230.     start)
  231.         startbukkit
  232.         ;;
  233.     stop)
  234.         stopbukkit
  235.         ;;
  236.     restart)
  237.         stopbukkit
  238.         sleep 5
  239.         startbukkit
  240.         ;;
  241.     update)
  242.         updatebukkit
  243.         ;;
  244.     backup)
  245.         backupbukkit
  246.         ;;
  247.     friendlystop)
  248.         stopbukkit friendly
  249.         ;;
  250.     *)
  251.         echo "Usage: $0 {status|start|stop|restart|update|backup|friendlystop}"
  252. esac
  253.  
  254. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top