surferpup

Minecraft Server Control Bash Script w/ Ram Drive Control

Nov 8th, 2013
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 27.43 KB | None | 0 0
  1. #!/bin/bash
  2. # /etc/init.d/minecraft
  3. # version 2.01 2013-11-09
  4. # by surferpup
  5. # Starts and administers a Minecraft Server with RamDrive Option
  6. #
  7. # Script allows for Start/Stop/Restart Minecraft
  8. #                      -- will auto configure RamDrive if desired
  9. #                   Specify Java and jarfile settings
  10. #                   Show server status
  11. #                   Execute Minecraft Command
  12. #                   Reboot Server
  13. #                   Backup Mincraft (daily, weekly, etc)
  14. #                   Manage Hourly/Daily/Weekly/Monthly/Manual BKU files
  15. #                   Synchronize Ram Drive (if used)
  16. #                   Chime (i.e. Hourly Server Chime)
  17. #                   Random Quotes (if you provide a file)
  18. #                   Cleaning of Cron Log on Start
  19. #                   Interacts with Cron nicely
  20. #                   Displays Help
  21. #
  22. # TODO:
  23. #
  24. #                   Add feature to create backup directories if they do not exist
  25. #                   Create Selectable Restore from Backup
  26. #                   Error recovery (allow to auto-move world.bak to world)
  27. #                   Full install script (automate settings and setup)
  28. #
  29. #
  30. #  NOTES:  1) I run my Feed the Beast Unleashed server on Ubunutu, and I am using mcpc+.  This script accommodates these settings.  
  31. #          2) I am using a 4GB RamDrive (/dev/shm) which I have modified in /etc/fstab to only be 4GB.
  32. #             The RamDrive is synchronized every 5 minutes by a cron job executing this script  
  33. #          3) I currently run two Minecraft servers on the same host with two copies of this script.  For you to do so,
  34. #             you need to make sure each instance has a different mine craft folder, a differently-named jar file, a different world location
  35. #             and you will need customzed versions of this script for each instance (it really isn't that difficult).
  36. #          4) I am using HugePages -- if you want to do so, you will need to modify /etc/sysctl.conf and /etc/security/limits.conf
  37. #             There is a great calculator for HugePages here -> http://www.peuss.de/node/67  You will need to set a user group for large pages.
  38. #          5) I chmod 755 the script and copied it to /etc/init.d.  I then placed a symlink in /usr/local/bin (ln -s /etc/inti.d/minecraft /usr/local/bin)
  39. #             This allows it to run without the ./ prefix or the .sh extension.
  40. #          6) I also modified rc.local to execute this script on startup by adding the following command (which also sends messages to my log file):
  41. #                 sudo -u surferpup /etc/init.d/minecraft start >> /home/surferpup/minecraft.cronlog 2>&1
  42. #          7) I am including my crontab entries below (sudo crontab -e to edit yours).
  43. #          8) For backups to work properly, you must create a backup folder outside of the Minecraft Install, and subfolders in that folder named as follows:
  44. #                     hourly,daily,weekly,monthly,manual
  45. #             Make sure to set the number of backup files in each class you wish to keep.
  46. #          8) This script is working well on a production server of 10-12 players, although it would work for any size server.
  47. #             -- I literally pay no mind to the server now, it just runs.  Enjoy.
  48. #  
  49. # CHANGELOG
  50. #   VER      DATE            CHANGE
  51. #       -----    -------------   ----------------------------------------------------------------------------
  52. #       2.01      2013-11-09     -- Corrected problem in mc_resstart and _mc_reboot where giving backup option
  53. #                                   would not issue user warnings.
  54. ########################################################
  55. ### CRONTAB ENTRIES EXAMPLE -- for automating functions
  56. #########################################################
  57. # These entries will execute accoringly and send output to logifle.
  58. # m h  dom mon dow   command
  59. # Random Quote - at 18,33 and 48 Minutes every hour except 7 am.
  60. # 18,33,48 0-6,8-23 * * *         su -c '/etc/init.d/minecraft quote >> /home/surferpup/minecraft.cronlog 2>&1' -s /bin/sh surferpup
  61. # Restart Minecraft with Daily Backup Odd Days at 7 am (30 minute warning starts at 6:30)
  62. # 30 6 1-31/2 * *                 su -c '/etc/init.d/minecraft restart daily >> /home/surferpup/minecraft.cronlog 2>&1' -s /bin/sh surferpup
  63. # Reboot Server with Daily Backup Even Days at 7 am(30 minute warning starts at 6:30)
  64. # 30 6 2-30/2 * *                 su -c '/etc/init.d/minecraft reboot daily >> /home/surferpup/minecraft.cronlog 2>&1' -s /bin/sh surferpup
  65. # Synchronize RamDrive every 5 Minutes except at minute 0 (backups/restarts and chimes run then)
  66. # 5-55/5 * * * *          su -c '/etc/init.d/minecraft ramdrivesave >> /home/surferpup/minecraft.cronlog 2>&1' -s /bin/sh surferpup
  67. # Hourly Chime with Backup (not at 7 am -- restart or reboot will run)
  68. # 0 0-6,8-23 * * *        su -c '/etc/init.d/minecraft chime hourly >> /home/surferpup/minecraft.cronlog 2>&1' -s /bin/sh surferpup
  69. # Note:  Weekly and Monthly Backup handled automatically by script.  Goal is to not duplicate backups.
  70. #        If Daily backups fall on first of week or first of month, then the script will change them to weekly or monthly surferpup
  71. #
  72. #
  73. ########################################################
  74. ### MINECRAFT SERVER SETTINGS
  75. ########################################################
  76. #echo "Number of args = $#"
  77. JARFILE="mcpcp.jar"                             # Jarfile of Minecraft server
  78. SERVICENAME="minecraft"                         # if running multiple MC Servers on same host -- each need different name
  79. USERNAME="surferpup"                                 # Username to run Minecraft under
  80. MCSTORE="/home/$USERNAME/UnleashedServer"       # Hard drive location of Minecraft files and world directory
  81. RAMPATH="/dev/shm/minecraft_ramdisk"            # RamDisk location of Minecraft files and world directory
  82. WORLD=world                                     # Name of world folder (used with MCSTORE and MCPATH)
  83. USE_RAMDRIVE=1                                  # Use Ram Disk No = 0, Yes = 1
  84. SCRIPTLOG="/home/$USERNAME/$SERVICENAME.cronlog"    # Log file for script messages.  Auto clened in the start script.
  85. BACKUPPATH="/home/$USERNAME/minecraft_backups"  # Backup Directory and Path
  86. MANUALBACKUPS=10                                # Number of manual backups to keep
  87. HOURLYBACKUPS=24                                # Number of hourly backups to keep
  88. DAILYBACKUPS=7                                  # Number of daily backups to keep
  89. WEEKLYBACKUPS=4                                 # Number of weekly backups to keep
  90. MONTHLYBACKUPS=12                               # Number of monthly backups to keep
  91. RANDOMQUOTEFILE="/home/surferpup/ChimeTastic"   # File path of Random Quotes (Blank if none)
  92. ########################################################
  93. ### JAVA-SPECIFIC SETTINGS
  94. ########################################################
  95. CPU_COUNT=8                                     # Number of CPU Cores (for Java Garbage Collector)
  96. #                                               # JAVA INVVOCATION STRING
  97. INVOCATION="java \
  98.             -Xmx8G \
  99.             -Xmn2G \
  100.             -XX:PermSize=2G \
  101.             -XX:MaxPermSize=2G \
  102.             -XX:TargetSurvivorRatio=80 \
  103.             -XX:MaxTenuringThreshold=31 \
  104.             -XX:+UseParNewGC \
  105.             -XX:ParallelGCThreads=`expr $CPU_COUNT - 1` \
  106.             -XX:+UseConcMarkSweepGC \
  107.             -XX:+UseGCOverheadLimit \
  108.             -XX:+DisableExplicitGC \
  109.             -XX:MaxGCPauseMillis=500 \
  110.             -XX:GCPauseIntervalMillis=150 \
  111.             -XX:+AggressiveOpts \
  112.             -XX:+TieredCompilation \
  113.             -XX:ReservedCodeCacheSize=2G \
  114.             -XX:+UseCodeCacheFlushing \
  115.             -XX:CompileThreshold=1000 \
  116.             -XX:+UseLargePages \
  117.             -XX:LargePageSizeInBytes=2m \
  118.             -jar $JARFILE nogui"
  119. ########################################################
  120. ### INITIALIZE SHELL VARIABLES -- DO NOT MODIFY
  121. ########################################################
  122. # Path to Minecraft files and world directory
  123. if [ $USE_RAMDRIVE -eq 1 ] && [ ! -n "$OPTION" -a "$OPTION" != " " ] ; then
  124.     MCPATH=$RAMPATH
  125. else
  126.     MCPATH=$MCSTORE
  127. fi
  128. COMMANDLINE=$@                                  # Commandline as typed by user
  129. COMMAND=$1                                      # First commandline argument (used for function select)
  130. OPTION=$2                                       # Second commandline argument
  131. TO_SCREEN="screen -p 0 -S $SERVICENAME -X eval 'stuff " # Key Variable for screen session functionality
  132. BACKUPTYPES=("hourly" "daily" "weekly" "monthly" "manual")  #Backup types allowed
  133. DEFAULTBACKUP="manual"                          # Default Backup Type (lower case - must have directory)
  134. WARNING_NOUN="The Server"                       # Noun for Warning Message
  135. WARNING_VERB="rebooted"                         # Reboot/Restart warning message.
  136. ########################################################
  137. ### FORCE USER TO DESIGNATED USER (Used extensively)
  138. ########################################################
  139. as_user() {
  140.     if [ "`whoami`" == "$USERNAME" ] ; then
  141.         bash -c "$1"
  142.     else
  143.         su - $USERNAME -c "$1"
  144.     fi
  145. }
  146. ########################################################
  147. ### RAMDRIVE HELPER FUNCTION -- Synchronize
  148. ########################################################
  149. mc_ramdrivesave() {
  150.     if [ "$USE_RAMDRIVE" == "1" ]; then
  151.         if [ ! -f $MCPATH/$JARFILE ]; then
  152.             echo `date +"%Y.%m.%d-%r:  Error.. Minecraft not in ramdrive"`
  153.         else
  154.             echo `date +"%Y.%m.%d-%r:  Saving ramdrive to disk."`
  155.             if [ -d "$MCSTORE/$WORLD.bak" ]; then
  156.                 echo `date +"%Y.%m.%d-%r:  last $WORLD.bak still exist, crashed warning! manual check required!!!"`
  157.                 exit 1
  158.             fi
  159.             if [ -d "$MCSTORE/$WORLD" ]; then
  160.                 as_user "mv $MCSTORE/$WORLD $MCSTORE/$WORLD.bak"
  161.             fi
  162.             if mc_status; then
  163.                 #  Minecraft currently running, so save world
  164.                 as_user "$TO_SCREEN \"save-off\"\015'"
  165.                 as_user "$TO_SCREEN \"save-all\"\015'"
  166.                 as_user "rsync -r -t $MCPATH/ $MCSTORE/"
  167.                 as_user "$TO_SCREEN \"save-on\"\015'"
  168.                 echo `date +"%Y.%m.%d-%r:  Ram Drive Synchronization complete ... world save turned back on."`
  169.             else
  170.                 # Minecraft currently not running
  171.                 echo `date +"%Y.%m.%d-%r:  Synchronizing Ram Drive, screen session closed"`
  172.                 as_user "rsync -r -t $MCPATH/ $MCSTORE/"
  173.             fi
  174.             if [ -d "$MCSTORE/$WORLD.bak" ]; then
  175.                 as_user "rm -r $MCSTORE/$WORLD.bak"
  176.             fi
  177.         fi
  178.     else
  179.         echo `date +"%Y.%m.%d-%r:  ramdrivesave: Ram Drive not in use."`
  180.     fi
  181. }
  182. ########################################################
  183. ### SERVER REBOOT
  184. ### Useage:  minecraft reboot
  185. ###          minecraft reboot now
  186. ###          minecraft reboot <backup option>
  187. ########################################################
  188. mc_reboot_server() {
  189.     echo `date +"%Y.%m.%d-%r:  Commencing server reboot."`
  190.     if [ -z "$OPTION" ] || contains ; then
  191.         if mc_status ; then
  192.             echo `date +"%Y.%m.%d-%r:  Reboot process initated with user warnings."`
  193.             WARNING_NOUN="The server"
  194.             WARNING_VERB="rebooted"
  195.             mc_warning
  196.         fi
  197.     else
  198.         if [ "$OPTION" == "now" ]; then
  199.             echo `date +"%Y.%m.%d-%r:  Reboot initated WITHOUT user warnings."`
  200.         else
  201.             echo ""
  202.             echo "Bad option: $COMMANDLINE"
  203.             echo ""
  204.             mc_showhelp
  205.             exit 1
  206.         fi
  207.     fi
  208.     if mc_status; then
  209.         mc_stop
  210.     else
  211.         echo `date +"%Y.%m.%d-%r:  Prior to reboot, $JARFILE not running."`
  212.     fi
  213.     if contains ; then # backup requested prior to reboot
  214.         echo `date +"%Y.%m.%d-%r:  Prior to reboot, $(echo $OPTION | perl -ne 'print ucfirst($_)') backup requested."`
  215.         mc_backup
  216.     fi
  217.  
  218.     echo `date +"%Y.%m.%d-%r:  Rebooting server..."`
  219.     as_user "reboot"
  220. }
  221.  
  222. #######################################################
  223. ### MINECRAFT START, STOP, RESTART, STATUS, COMMAND
  224. ### Useage:  minecraft start
  225. ###          minecraft stop
  226. ###          minecraft restart
  227. ###          minecraft restart now
  228. ###          minecraft restart <backup option>
  229. ###          minecraft status
  230. ###          minecraft command <"command in quotes">
  231. #######################################################
  232. mc_start() {
  233.     if mc_status; then
  234.         echo `date +"%Y.%m.%d-%r:  Tried to start but $JARFILE was already running!"`
  235.     else
  236.         echo `date +"%Y.%m.%d-%r:  $JARFILE was not running... starting."`
  237.         if [ -d $MCSTORE/$WORLD.bak ]; then
  238.             echo `date +"%Y.%m.%d-%r:  last $WORLD.bak still exist, crashed warning! manual check required!!!"`
  239.             exit 1
  240.         fi
  241.         if [[ ( ! -f "$MCPATH/$JARFILE"  || "$OPTION" == "reload" ) &&  $USE_RAMDRIVE -eq 1 ]]; then
  242.             if [ "$OPTION" == "reload" ]; then
  243.                 echo `date +"%Y.%m.%d-%r:  Reloading Ram Drive ... Remove old drive."`
  244.                 as_user "rm -r $RAMPATH"
  245.                 echo `date +"%Y.%m.%d-%r:  Old Ram Drive removed .. reloading Ram Drive from Hard Drive..."`
  246.             else
  247.                     echo `date +"%Y.%m.%d-%r:  Ram drive empty...  prepping."`
  248.                 fi
  249.             as_user "rsync -r -t $MCSTORE/ $MCPATH/"
  250.             echo `date +"%Y.%m.%d-%r:  Ram drive prepared."`
  251.         fi
  252.         if [ ! -f "$MCPATH/$JARFILE" ]; then
  253.             echo "Minecraft path $MCPATH does not contain $JARFILE.  Aborting."
  254.             exit 1
  255.         fi
  256.     echo `date +"%Y.%m.%d-%r:  Cleaning Script Cron Log ... "`
  257.     tail -1500 $SCRIPTLOG > $SCRIPTLOG$(echo tmp); mv $SCRIPTLOG$(echo tmp) $SCRIPTLOG
  258.         cd $MCPATH
  259.         echo "Starting Minecraft from $MCPATH/$JARFILE...."
  260.         as_user "cd $MCPATH && screen -dmS $SERVICENAME $INVOCATION"
  261.         sleep 7
  262.         if mc_status; then
  263.             echo `date +"%Y.%m.%d-%r:  $JARFILE is now running."`
  264.         else
  265.             echo `date +"%Y.%m.%d-%r:  Could not start $JARFILE."`
  266.         fi
  267.     fi
  268. }
  269. mc_stop() {
  270.     if mc_status; then
  271.         echo `date +"%Y.%m.%d-%r:  $JARFILE is running... stopping."`
  272.         as_user "$TO_SCREEN \"say SERVER SHUTTING DOWN IN 5 SECONDS. Saving map...\"\015'"
  273.         as_user "$TO_SCREEN \"stop\"\015'"
  274.         sleep 10
  275.     else
  276.         echo `date +"%Y.%m.%d-%r:  Attempted to stop, but $JARFILE was not running."`
  277.     fi
  278.  
  279.     if mc_status; then
  280.         echo `date +"%Y.%m.%d-%r:  $JARFILE could not be shut down... still running."`
  281.     else
  282.         mc_ramdrivesave # Synchronize Ramdrive with Hardrive if in use.
  283.         echo `date +"%Y.%m.%d-%r:  $JARFILE is shut down."`
  284.     fi
  285. }
  286. mc_restart() {
  287.     echo `date +"%Y.%m.%d-%r:  Commencing Minecraft Restart."`
  288.     if [ -z "$OPTION" ] || contains ; then
  289.         if mc_status ; then
  290.             echo `date +"%Y.%m.%d-%r:  Minecraft Restart process initated with user warnings."`
  291.             WARNING_NOUN="Minecraft"
  292.             WARNING_VERB="restarted"
  293.             mc_warning
  294.         fi
  295.     else
  296.         if [ "$OPTION" == "now" ]; then
  297.             echo `date +"%Y.%m.%d-%r:  Minecraft Restart process initated WITHOUT user warnings."`
  298.         else
  299.             echo ""
  300.             echo "Bad option: $COMMANDLINE"
  301.             echo ""
  302.             mc_showhelp
  303.             exit 1
  304.         fi
  305.     fi
  306.     if mc_status; then
  307.         mc_stop
  308.     else
  309.         echo `date +"%Y.%m.%d-%r:  Prior to restart, $JARFILE not running."`
  310.     fi
  311.     if contains ; then # backup requested prior to Minecraft restart
  312.     echo `date +"%Y.%m.%d-%r:  Prior to restart, $(echo $OPTION | perl -ne 'print ucfirst($_)') backup requested."`
  313.         mc_backup
  314.     fi
  315.     echo `date +"%Y.%m.%d-%r:  Restarting $JARFILE..."`
  316.     mc_start
  317. }
  318. mc_status() {
  319.     ps aux |grep -F -v grep|grep -F -v SCREEN|grep -F --quiet $JARFILE
  320.     return $?
  321. }
  322.  
  323. ##########
  324. ### NOTES on mc_command:
  325. ###     Command will default to whatever the default $COMMAND variable is (usually "list")
  326. ###     Used as a helper function to "minecraft status"
  327. ###     IF you issue an invalid Minecraft Server command, the error will show up in the Minecraft Console
  328. ##########
  329. mc_command() {
  330.     if [ -z $OPTION]; then
  331.         OPTION="list"
  332.     fi
  333.     if pgrep -u $USERNAME -f $JARFILE > /dev/null; then
  334.         pre_log_len=`wc -l "$MCPATH/server.log" | awk '{print $1}'`
  335.         echo `date +"%Y.%m.%d-%r:  $JARFILE is running... executing command"`
  336.         as_user "$TO_SCREEN \"$OPTION\"\015'"
  337.         sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
  338.         # print output
  339.         tail -n $[`wc -l "$MCPATH/server.log" | awk '{print $1}'`-$pre_log_len] "$MCPATH/server.log"
  340.     fi
  341. }
  342.  
  343. ########################################################
  344. ###  Backups
  345. ########################################################
  346. mc_backup() {
  347.     # Check for vaild option
  348.     BACKUPTYPE=$OPTION
  349.     if [ -z $OPTION ]; then
  350.         BACKUPTYPE=$DEFAULTBACKUP
  351.     else
  352.         if ! contains ; then # Check if allowed backup type
  353.             echo ""
  354.             echo "The backup type you chose ($BACKUPTYPE) is not defined."
  355.             mc_showhelp
  356.             exit 1
  357.         fi
  358.     fi
  359.     echo `date +"%Y.%m.%d-%r:  $(echo $BACKUPTYPE | perl -ne 'print ucfirst($_)') Backup."`
  360.     echo `date +"%Y.%m.%d-%r:  Backing up minecraft files"`
  361.     # If using Ram Drive, synchronize Hard Drive location to Ram Drive
  362.     if [ $USE_RAMDRIVE -ne 1 ]; then
  363.         mc_saveoff
  364.     else
  365.         mc_ramdrivesave
  366.     fi
  367.     # On daily backups, check if day is first of month or first of week. If so, change the backup tyoe accordiningly
  368.     if [[ ($BACKUPTYPE == "daily" || $BACKUPTYPE == "weekly") && $(echo `date +"%d"`) == 01 ]]; then
  369.         echo `date +"%Y.%m.%d-%r:  It is the first of the month, $BACKUPTYPE changed to monthly."`
  370.         BACKUPTYPE="monthly"
  371.     elif [[ $BACKUPTYPE == "daily" && $(echo `date +"%u"`) == 1 ]]; then
  372.         echo `date +"%Y.%m.%d-%r:  It is the first of the week, $BACKUPTYPE changed to weekly."`
  373.         BACKUPTYPE="weekly"
  374.     fi
  375.     # Back up the world folder on the Hard Drive
  376.     echo `date +"%Y.%m.%d-%r:  World save done, commencing $(echo $BACKUPTYPE | perl -ne 'print ucfirst($_)') Backup to $BACKUPPATH/$BACKUPTYPE."`
  377.     as_user "tar -czPf $BACKUPPATH/$BACKUPTYPE/MCBKUP_`date +"%Y.%m.%d-%H%M%S"`.tar.gz $MCSTORE"
  378.     if [ $USE_RAMDRIVE -ne 1 ]; then
  379.         mc_saveon
  380.     fi
  381.     echo `date +"%Y.%m.%d-%r:  $(echo $BACKUPTYPE | perl -ne 'print ucfirst($_)') backup complete"`
  382.     # Remove excess backup files (oldest first) from backup folder
  383.     filearray=("$BACKUPPATH""/$BACKUPTYPE"/* ) #filearray will store the list of files in the directory
  384.     numfiles=${#filearray[@]}
  385.     maxfiles="${BACKUPTYPE^^}BACKUPS" # maxvalues = string containing name of variable for # of backups
  386.     if [ $numfiles -gt ${!maxfiles} ]; then # Dereference maxfiles (${!maxfiles}) to get max # of backups
  387.         REMOVED="$(echo ${filearray[@]:0:`expr $numfiles - ${!maxfiles}`})" #Dereferenced again
  388.         echo `date +"%Y.%m.%d-%r:  Removing $REMOVED"`
  389.         as_user "rm $REMOVED"
  390.     else
  391.         echo `date +"%Y.%m.%d-%r:  No backup files removed."`
  392.     fi
  393.     echo `date +"%Y.%m.%d-%r:  $(echo $BACKUPTYPE | perl -ne 'print ucfirst($_)') Backup complete"`
  394. }
  395. ########################################################
  396. ### BACKUP HELPER FUNCTIONS
  397. ########################################################
  398. contains() {
  399.     for element in "${BACKUPTYPES[@]}"
  400.     do
  401.         if [ $element == "$OPTION" ] ; then
  402.             return 0
  403.         fi
  404.     done
  405.     return 1
  406. }
  407. mc_saveoff() {
  408.     if mc_status; then
  409.         echo `date +"%Y.%m.%d-%r:  $JARFILE is running... suspending saves"`
  410.         as_user "$TO_SCREEN \"say SERVER BACKUP STARTING. Server going readonly...\"\015'"
  411.         as_user "$TO_SCREEN \"save-off\"\015'"
  412.         sleep .1
  413.         as_user "$TO_SCREEN \"save-all\"\015'"
  414.         sleep 10
  415.     else
  416.         echo `date +"%Y.%m.%d-%r:  $JARFILE was not running. Not suspending saves."`
  417.     fi
  418. }
  419. mc_saveon() {
  420.     if mc_status; then
  421.         echo `date +"%Y.%m.%d-%r:  $JARFILE is running... re-enabling saves"`
  422.         as_user "$TO_SCREEN \"save-on\"\015'"
  423.         as_user "$TO_SCREEN \"say SERVER BACKUP ENDED. Server going read-write...\"\015'"
  424.         echo `date +"%Y.%m.%d-%r:  $JARFILE is running... world save turned back on."`
  425.     else
  426.         echo `date +"%Y.%m.%d-%r:  $JARFILE was not running. Not resuming saves."`
  427.     fi
  428. }
  429. ########################################################
  430. ### Hourly Chime (Optional Backup)
  431. ########################################################
  432. mc_chime() {
  433.     local MESSAGE_WORD="without"
  434.     if contains; then
  435.         MESSAGE_WORD="with $(echo $BACKUPTYPE | perl -ne 'print ucfirst($_)')"
  436.     else
  437.         if [ ! -z $OPTION ]; then
  438.             echo ""
  439.             echo "Bad option: $COMMANDLINE"
  440.             echo ""
  441.             mc_showhelp
  442.             exit 1
  443.         fi
  444.     fi
  445.     echo `date +"%Y.%m.%d-%r:  Hourly Chime $MESSAGE_WORD Backup."`
  446.     as_user "$TO_SCREEN \"say ---------------------------------------\"\015'"
  447.     as_user "$TO_SCREEN \"say The server time is `date +"%r CST"`.\"\015'"
  448.     as_user "$TO_SCREEN \"say Hourly Chime Annoyance Lessification complete.\"\015'"
  449.     as_user "$TO_SCREEN \"say NOTE:  The Mining World will be respawned.\"\015'"
  450.     as_user "$TO_SCREEN \"say PLEASE REMOVE YOUR ITEMS FROM MINING WORLD BY SUNDAY!!!\"\015'"
  451.     as_user "$TO_SCREEN \"say ---------------------------------------\"\015'"
  452.     sleep .1
  453.     as_user "$TO_SCREEN \"list\"\015'"
  454.  
  455.     if contains ; then
  456.         mc_backup
  457.     fi
  458. }
  459. ########################################################
  460. ### Blurt out random Quote from Quote File (if it exists)
  461. ########################################################
  462. mc_quote() {
  463.     if [ -f "$RANDOMQUOTEFILE" ] ; then
  464.         as_user "$TO_SCREEN \"say RANDOM THOUGHT:\"\015'"
  465.     sleep .1
  466.     RANDOMTHOUGHT=$(shuf -n 1 $RANDOMQUOTEFILE)
  467.     echo "$RANDOMTHOUGHT"
  468.         as_user "$TO_SCREEN \"say $RANDOMTHOUGHT\"\015'"
  469.     sleep .1
  470.     else
  471.         echo `date +"%Y.%m.%d-%r:  There is not quotefile at $RANDOMQUOTEFILE"`
  472.     fi
  473. }
  474. ########################################################
  475. ### Server Reboot/Restart Warning Message
  476. ########################################################
  477. mc_warning() {
  478.     as_user "$TO_SCREEN \"say $WARNING_NOUN will be $WARNING_VERB in 30 minutes. Please plan your excursions accordingly.\"\015'"
  479.     sleep 10m
  480.     as_user "$TO_SCREEN \"say $WARNING_NOUN will be $WARNING_VERB in 20 minutes. You will be logged off when this happens.\"\015'"
  481.     sleep 10m
  482.     as_user "$TO_SCREEN \"say $WARNING_NOUN will be $WARNING_VERB in 10 minutes  Dig Beauford!!  Dig!!!!!!.\"\015'"
  483.     sleep 5m
  484.     as_user "$TO_SCREEN \"say $WARNING_NOUN will be $WARNING_VERB in 5 minutes.  The server will be down while the backup completes.\"\015'"
  485.     sleep 3m
  486.     as_user "$TO_SCREEN \"say $WARNING_NOUN will be $WARNING_VERB in 2 minutes  Please log out soon.\"\015'"
  487.     sleep 1m
  488.     as_user "$TO_SCREEN \"say $WARNING_NOUN will be $WARNING_VERB in 1 minute.\"\015'"
  489.     as_user "$TO_SCREEN \"say $WARNING_NOUN will come back when the process completes.  Please give it about 2 minutes.\"\015'"
  490.     sleep 30
  491.     as_user "$TO_SCREEN \"say 30 SECONDS TILL ${WARNING_NOUN^^} WILL BE ${WARNING_VERB} ...\"\015'"
  492.     sleep 15
  493.     as_user "$TO_SCREEN \"say 15 SECONDS TILL ${WARNING_NOUN^^} WILL BE ${WARNING_VERB} ...\"\015'"
  494.     sleep 5
  495.     as_user "$TO_SCREEN \"say 10 SECONDS TILL ${WARNING_NOUN^^} WILL BE ${WARNING_VERB} ...\"\015'"
  496.     sleep 5
  497.     as_user "$TO_SCREEN \"say 5 SECONDS TILL ${WARNING_NOUN^^} WILL BE ${WARNING_VERB} ...\"\015'"
  498.     sleep 1
  499.     as_user "$TO_SCREEN \"say 4 SECONDS TILL ${WARNING_NOUN^^} WILL BE ${WARNING_VERB} ...\"\015'"
  500.     sleep 1
  501.     as_user "$TO_SCREEN \"say 3 SECONDS TILL ${WARNING_NOUN^^} WILL BE ${WARNING_VERB} ...\"\015'"
  502.     sleep 1
  503.     as_user "$TO_SCREEN \"say 2 SECONDS TILL ${WARNING_NOUN^^} WILL BE ${WARNING_VERB} ...\"\015'"
  504.     sleep 1
  505.     as_user "$TO_SCREEN \"say 1 SECONDS TILL ${WARNING_NOUN^^} WILL BE ${WARNING_VERB} ...\"\015'"
  506.     sleep 1
  507. }
  508. ########################################################
  509. ### Help Text
  510. ########################################################
  511. mc_showhelp() {
  512.     ME=`basename $0` # The name of this script
  513.     DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # the path of this script
  514.    echo ""
  515.    echo "Usage: $DIR/$ME <command> <option>"
  516.    echo ""
  517.    echo "Command <option>         Description"
  518.    echo "----------------         -------------------------------------------------------------------"
  519.    echo "start <option>           Start Minecraft. Option \"reload\" will recreate the RamDrive."
  520.    echo "                           ex.  minecraft start"
  521.    echo "                                minecraft start reload"
  522.    echo "stop                     Stop Minecraft gracefully."
  523.    echo "restart <option>         Restart Minecraft gracefully with backup and 30 minute user warning."
  524.    echo "                           ex.  minecraft restart"
  525.    echo "                                minecraft restart now   (Skips warning)"
  526.    echo "                                minecraft restart daily (or any backup type) -- backup and restart."
  527.  
  528.    echo "reboot <option>          Reboots server gracefully with backup and 30 minute user warning."
  529.    echo "                           ex.  minecraft reboot"
  530.    echo "                                minecraft reboot now  (Skips warning)"
  531.    echo "status                   Shows status of server and list of current players."
  532.    echo "command <option>         Execute any minecraft server comand (option in \"\")."
  533.    echo "                           ex.  minecraft command \"say Hello World!\""
  534.    echo "                                default option = \"list\""
  535.    echo "backup <option>          Backup the world file to $BACKUPPATH with the following options:"
  536.    for element in "${BACKUPTYPES[@]}"
  537.    do
  538.    echo "                                $element (backup to $BACKUPPATH/$element)"
  539.    done
  540.    echo "                                default option is $DEFAULTBACKUP"
  541.    echo "                           NOTE:  Backups DO NOT cause a Minecraft restart.."
  542.    echo "chime <option>           Print the Chime message.  Can also execute any backup type."
  543.    echo "                           ex.  minecraft chime"
  544.    echo "                                minecraft chime hourly (or any backup type)"
  545.    echo "ramdrivesave             Force ramdisk to synch to hard drive world folder."
  546.    echo "--------------"
  547.    echo "          Script $ME stored in $DIR.  Use \"sudo nano $DIR/$ME\" to edit."
  548.    echo ""
  549. }
  550. ########################################################
  551. ### COMMAND LINE ARGUMENTS
  552. ########################################################
  553. case "$COMMAND" in
  554.    start)
  555.        mc_start
  556.        ;;
  557.    stop)
  558.        mc_stop
  559.        ;;
  560.    restart)
  561.        mc_restart
  562.        ;;
  563.    reboot)
  564.        mc_reboot_server
  565.        ;;
  566.    status)
  567.        if mc_status; then
  568.            echo "Minecraft is running from $MCPATH/$JARFILE."
  569.            mc_command
  570.        else
  571.            echo `date +"%Y.%m.%d-%H:  $JARFILE is not running."`
  572.        fi
  573.        ;;
  574.    command)
  575.        mc_command
  576.        ;;
  577.    backup)
  578.        mc_backup
  579.        ;;
  580.    ramdrivesave)
  581.        mc_ramdrivesave
  582.        ;;
  583.    chime)
  584.        mc_chime
  585.        ;;
  586.    quote)
  587.        mc_quote
  588.        ;;
  589.    ?)
  590.        mc_showhelp
  591.        exit 1
  592.        ;;
  593.    *)
  594.        mc_showhelp
  595.        exit 1
  596.        ;;
  597. esac
Advertisement
Add Comment
Please, Sign In to add comment