Advertisement
masa-

mc_common_functions.sh

Aug 16th, 2017
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 29.12 KB | None | 0 0
  1. #!/bin/bash
  2. # Initial version from Minecraft wiki
  3. # Heavily modified, customized and expanded by masa (kiesus@gmail.com)
  4. # version x.x.x 2013-11-07 (YYYY-MM-DD)
  5.  
  6. export LANG=en_US.UTF-8
  7. export LC_ALL=en_US.UTF-8
  8.  
  9. as_user() {
  10.     ME=`whoami`
  11.  
  12.     if [ ${ME} == ${USERNAME} ] ; then
  13.         bash -c "${1}"
  14.     else
  15.         su - ${USERNAME} -c "${1}"
  16.     fi
  17. }
  18.  
  19.  
  20. mc_start() {
  21.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  22.     L_PREFIX="${L_TIMESTAMP}"
  23.  
  24.     # Check that the server is not already running
  25.     if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  26.     then
  27.         echo "${L_PREFIX} [WARNING] mc_start(): ${SERVICE} is already running" | tee -a ${EVENT_LOG_FILE}
  28.     else
  29.         echo "${L_PREFIX} [INFO] mc_start(): Starting ${SERVICE}..." | tee -a ${EVENT_LOG_FILE}
  30.  
  31.         if [ "${USE_TEMP_LOCATION}" = "true" ]
  32.         then
  33.             # Copy the save files rom the actual game directory to the run-time location inside /tmp
  34.             copy_save_files_from_game_dir_to_tmp
  35.         fi
  36.  
  37.         # Start the server process
  38.         as_user "cd ${INSTANCE_PATH} && screen -dmS ${SCREEN_SESSION} ${INVOCATION}"
  39.         sleep ${POST_START_DELAY}
  40.  
  41.         # Verify that the server process was started successfully, by checking a few times in a delayed loop
  42.         COUNTER=0
  43.  
  44.         while [ $COUNTER -lt ${START_GRACE_DELAY} ]
  45.         do
  46.             if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  47.             then
  48.                 L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  49.                 L_PREFIX="${L_TIMESTAMP}"
  50.  
  51.                 echo "${L_PREFIX} [INFO] mc_start(): ${SERVICE} is now running" | tee -a ${EVENT_LOG_FILE}
  52.  
  53.                 # Create the state file to indicate that the server is/should be running
  54.                 as_user "touch ${STATE_FILE}"
  55.  
  56.                 # Disable automatic saving?
  57.                 if [ "${DISABLE_AUTOSAVE}" = "true" ]
  58.                 then
  59.                     sleep ${START_TO_DISABLE_AUTOSAVE_DELAY}
  60.                     mc_saveoff
  61.                 fi
  62.  
  63.                 break
  64.             fi
  65.  
  66.             sleep 1
  67.  
  68.             ((COUNTER += 1))
  69.         done
  70.  
  71.         # If the loop counter hit the max value, the process was not started successfully
  72.         if [ ${COUNTER} -ge ${START_GRACE_DELAY} ]
  73.         then
  74.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  75.             L_PREFIX="${L_TIMESTAMP}"
  76.  
  77.             echo "${L_PREFIX} [ERROR] mc_start(): Could not start ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  78.         fi
  79.     fi
  80. }
  81.  
  82.  
  83. mc_stop() {
  84.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  85.     L_PREFIX="${L_TIMESTAMP}"
  86.  
  87.     # Check that the process is running
  88.     if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  89.     then
  90.         echo "${L_PREFIX} [INFO] mc_stop(): Stopping ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  91.  
  92.         if [ "${1}" = "broadcast" ]
  93.         then
  94.             as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"say §dSERVER SHUTTING DOWN NOW. Saving the map...\"\015'"
  95.         fi
  96.  
  97.         as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"save-all\"\015'"
  98.         sleep ${SAVEALL_STOP_INTERVAL}
  99.  
  100.         as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"stop\"\015'"
  101.         sleep ${POST_STOP_DELAY}
  102.  
  103.         # Check if the process was successfully stopped
  104.         COUNTER=0
  105.         while [ ${COUNTER} -lt ${STOP_GRACE_DELAY} ]; do
  106.  
  107.             if ! pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  108.             then
  109.                 # Remove the state file to indicate that the server has been stopped gracefully
  110.                 as_user "rm ${STATE_FILE} > /dev/null 2>&1"
  111.  
  112.                 L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  113.                 L_PREFIX="${L_TIMESTAMP}"
  114.                 echo "${L_PREFIX} [INFO] mc_stop(): ${SERVICE} has been stopped" | tee -a ${EVENT_LOG_FILE}
  115.  
  116.                 if [ "${USE_TEMP_LOCATION}" = "true" ]
  117.                 then
  118.                     # Copy the save files from the temp directory (in RAM) into the actual server directory
  119.                     copy_save_files_from_tmp_to_game_dir
  120.                     local RET=$?
  121.  
  122.                     # If copying the save file succeeded, and this is not a restart, then remove the temp files
  123.                     if [ $RET -eq 0 ] && [ "$1" != "true" ]
  124.                     then
  125.                         remove_save_files_from_tmp_location
  126.                     fi
  127.                 fi
  128.  
  129.                 break
  130.             fi
  131.  
  132.             sleep 1
  133.  
  134.             ((COUNTER += 1))
  135.         done
  136.  
  137.         if [ ${COUNTER} -ge ${STOP_GRACE_DELAY} ]
  138.         then
  139.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  140.             L_PREFIX="${L_TIMESTAMP}"
  141.  
  142.             echo "${L_PREFIX} [ERROR] mc_stop(): ${SERVICE} could not be stopped" | tee -a ${EVENT_LOG_FILE}
  143.         fi
  144.     else
  145.         echo "${L_PREFIX} [WARNING] mc_stop(): ${SERVICE} was not running" | tee -a ${EVENT_LOG_FILE}
  146.     fi
  147. }
  148.  
  149.  
  150. mc_restart() {
  151.     # Use the arguments as override delay values, if they exist
  152.     if [ -n "${1}" ]
  153.     then
  154.         RESTART_FIRST_WARNING=${1}
  155.  
  156.         if [ -n "${2}" ]
  157.         then
  158.             RESTART_LAST_WARNING=${2}
  159.         fi
  160.     fi
  161.  
  162.     # Check if the server process is running, and broadcast warning messages before stopping the server
  163.     if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  164.     then
  165.         if [ ${RESTART_FIRST_WARNING} -gt ${RESTART_LAST_WARNING} ]
  166.         then
  167.             as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"say §dSERVER RESTARTING IN ${RESTART_FIRST_WARNING} SECONDS\"\015'"
  168.             ((DELAY = ${RESTART_FIRST_WARNING} - ${RESTART_LAST_WARNING}))
  169.             sleep ${DELAY}
  170.         fi
  171.  
  172.         as_user "screen -p 0 -S $SCREEN_SESSION -X eval 'stuff \"say §dSERVER RESTARTING IN ${RESTART_LAST_WARNING} SECONDS\"\015'"
  173.         sleep ${RESTART_LAST_WARNING}
  174.  
  175.         L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  176.         L_PREFIX="${L_TIMESTAMP}"
  177.         echo "${L_PREFIX} [INFO] mc_restart(): Stopping ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  178.  
  179.         mc_stop "true"
  180.  
  181.         if ! pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  182.         then
  183.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  184.             L_PREFIX="${L_TIMESTAMP}"
  185.             echo "${L_PREFIX} [INFO] mc_restart(): ${SERVICE} has been stopped" | tee -a ${EVENT_LOG_FILE}
  186.         fi
  187.     fi
  188.  
  189.     # (Re-)Start the server
  190.     if ! pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  191.     then
  192.         L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  193.         L_PREFIX="${L_TIMESTAMP}"
  194.         echo "${L_PREFIX} [INFO] mc_restart(): (Re-)starting ${SERVICE}..." | tee -a ${EVENT_LOG_FILE}
  195.  
  196.         mc_start
  197.  
  198.         L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  199.         L_PREFIX="${L_TIMESTAMP}"
  200.  
  201.         if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  202.         then
  203.             echo "${L_PREFIX} [INFO] mc_restart(): ${SERVICE} is now running" | tee -a ${EVENT_LOG_FILE}
  204.         else
  205.             echo "${L_PREFIX} [ERROR] mc_restart(): Could not start ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  206.         fi
  207.  
  208.     else
  209.         echo "${L_PREFIX} [ERROR] mc_restart(): ${SERVICE} could not be stopped" | tee -a ${EVENT_LOG_FILE}
  210.     fi
  211. }
  212.  
  213.  
  214. mc_restart_if_up() {
  215.     # Use the arguments as override delay values, if they exist
  216.     if [ -n "${1}" ]
  217.     then
  218.         RESTART_FIRST_WARNING=${1}
  219.  
  220.         if [ -n "${2}" ]
  221.         then
  222.             RESTART_LAST_WARNING=${2}
  223.         fi
  224.     fi
  225.  
  226.     # First check if the server is supposed to be running
  227.     if [ -f "${STATE_FILE}" ]
  228.     then
  229.         # Check if the server process is running, and broadcast warning messages before stopping the server
  230.         if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  231.         then
  232.             if [ ${RESTART_FIRST_WARNING} -gt ${RESTART_LAST_WARNING} ]
  233.             then
  234.                 as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"say §dSERVER RESTARTING IN ${RESTART_FIRST_WARNING} SECONDS\"\015'"
  235.                 ((DELAY = ${RESTART_FIRST_WARNING} - ${RESTART_LAST_WARNING}))
  236.                 sleep ${DELAY}
  237.             fi
  238.  
  239.             as_user "screen -p 0 -S $SCREEN_SESSION -X eval 'stuff \"say §dSERVER RESTARTING IN ${RESTART_LAST_WARNING} SECONDS\"\015'"
  240.             sleep ${RESTART_LAST_WARNING}
  241.  
  242.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  243.             L_PREFIX="${L_TIMESTAMP}"
  244.             echo "${L_PREFIX} [INFO] mc_restart_if_up(): Stopping ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  245.  
  246.             mc_stop "true"
  247.         fi
  248.  
  249.         # The server was running and was successfully stopped, or at least is supposed to be running. Now we start it up again.
  250.         if ! pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  251.         then
  252.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  253.             L_PREFIX="${L_TIMESTAMP}"
  254.  
  255.             echo "${L_PREFIX} [INFO] mc_restart_if_up(): ${SERVICE} has been stopped" | tee -a ${EVENT_LOG_FILE}
  256.             echo "${L_PREFIX} [INFO] mc_restart_if_up(): Restarting ${SERVICE}..." | tee -a ${EVENT_LOG_FILE}
  257.  
  258.             mc_start
  259.  
  260.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  261.             L_PREFIX="${L_TIMESTAMP}"
  262.  
  263.             if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  264.             then
  265.                 echo "${L_PREFIX} [INFO] mc_restart_if_up(): ${SERVICE} is now running" | tee -a ${EVENT_LOG_FILE}
  266.             else
  267.                 echo "${L_PREFIX} [ERROR] mc_restart_if_up(): Could not start ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  268.             fi
  269.  
  270.         else
  271.             echo "${L_PREFIX} [ERROR] mc_restart_if_up(): ${SERVICE} could not be stopped" | tee -a ${EVENT_LOG_FILE}
  272.         fi
  273. #   else
  274. #       L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  275. #       L_PREFIX="${L_TIMESTAMP}"
  276. #       echo "${L_PREFIX} [WARNING] mc_restart_if_up(): ${SERVICE} was not running" | tee -a ${EVENT_LOG_FILE}
  277.     fi
  278. }
  279.  
  280.  
  281. mc_saveoff() {
  282.     if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  283.     then
  284.         if [ "${1}" = "broadcast" ];
  285.         then
  286.             as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"say §dServer going read-only...\"\015'"
  287.         fi
  288.  
  289.         as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"save-off\"\015'"
  290.     fi
  291. }
  292.  
  293.  
  294. mc_saveon() {
  295.     if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  296.     then
  297.         if [ "${1}" = "broadcast" ];
  298.         then
  299.             as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"say §dServer going read-write...\"\015'"
  300.         fi
  301.  
  302.         as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"save-on\"\015'"
  303.     fi
  304. }
  305.  
  306.  
  307. mc_saveall() {
  308.     if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  309.     then
  310.         if [ "${1}" = "broadcast" ];
  311.         then
  312.             as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"say §dSaving the map...\"\015'"
  313.         fi
  314.  
  315.         as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"save-all\"\015'"
  316.         sleep ${POST_SAVEALL_DELAY}
  317.     fi
  318. }
  319.  
  320.  
  321. copy_save_files_from_game_dir_to_tmp() {
  322.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  323.  
  324.     if [ -z "${WORLD_PATH_TMP}" ] || [ -z "${WORLD_PATH_REAL}" ] || [ -z "${WORLD_PATH_SYMLINK}" ]
  325.     then
  326.         echo "Empty real or temp world path, or world path symlink name!"
  327.         exit 1
  328.     fi
  329.  
  330.     # Make the necessary temporary directories
  331.     if [ ! -d "${WORLD_PATH_TMP}" ]
  332.     then
  333.         echo "${L_TIMESTAMP} [INFO] copy_save_files_from_game_dir_to_tmp(): Creating the temporary save directory '${WORLD_PATH_TMP}'" | tee -a ${EVENT_LOG_FILE}
  334.         as_user "mkdir -p ${WORLD_PATH_TMP} > /dev/null 2>&1"
  335.     fi
  336.  
  337.     # The symlink does not point to the temp directory
  338.     # (which means that it likely points to the real directory on disk, see remove_save_files_from_tmp_location())
  339.     if [ -h "${WORLD_PATH_SYMLINK}" ] && [ `readlink -f ${WORLD_PATH_SYMLINK}` != "${WORLD_PATH_TMP}" ]
  340.     then
  341.         as_user "rm ${WORLD_PATH_SYMLINK} > /dev/null 2>&1"
  342.     fi
  343.  
  344.     # No symlink present
  345.     if [ ! -e "${WORLD_PATH_SYMLINK}" ]
  346.     then
  347.         echo "${L_TIMESTAMP} [INFO] copy_save_files_from_game_dir_to_tmp(): (Re-)Creating a symlink for the world directory to ${WORLD_PATH_TMP}" | tee -a ${EVENT_LOG_FILE}
  348.         as_user "ln -s ${WORLD_PATH_TMP} ${WORLD_PATH_SYMLINK} > /dev/null 2>&1"
  349.     fi
  350.  
  351.     echo "${L_TIMESTAMP} [INFO] copy_save_files_from_game_dir_to_tmp(): Going to copy the save files to the temp directory..." | tee -a ${EVENT_LOG_FILE}
  352.  
  353.     # Copy the save files from the "real save path" on disk to the temporary location (in RAM).
  354.     # Note: -u to only copy newer files!
  355.     as_user "rsync -au ${WORLD_PATH_REAL}/ ${WORLD_PATH_TMP} > /dev/null 2>&1"
  356.     #as_user "rsync -auhv ${WORLD_PATH_REAL}/ ${WORLD_PATH_TMP}"
  357.  
  358.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  359.     echo "${L_TIMESTAMP} [INFO] copy_save_files_from_game_dir_to_tmp(): Done copying files" | tee -a ${EVENT_LOG_FILE}
  360. }
  361.  
  362.  
  363. copy_save_files_from_tmp_to_game_dir() {
  364.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  365.     echo "${L_TIMESTAMP} [INFO] copy_save_files_from_tmp_to_game_dir(): Going to copy the save files from the temp directory to the instance directory" | tee -a ${EVENT_LOG_FILE}
  366.  
  367.     if [ -z "${WORLD_PATH_TMP}" ] || [ -z "${WORLD_PATH_REAL}" ]
  368.     then
  369.         echo "EMPTY PATH"
  370.         exit 1
  371.     fi
  372.  
  373.     if [ ! -e "${WORLD_PATH_TMP}" ]
  374.     then
  375.         echo "${L_TIMESTAMP} [INFO] copy_save_files_from_tmp_to_game_dir: temp directory '${WORLD_PATH_TMP}' does not exist, nothing to do"
  376.         return 0
  377.     fi
  378.  
  379.  
  380.     # Sync any changed files from the temp location to the real save path (the -u flag only copies newer files)
  381.     as_user "rsync -au ${WORLD_PATH_TMP}/ ${WORLD_PATH_REAL} > /dev/null"
  382.     #as_user "rsync -auhv ${WORLD_PATH_TMP}/ ${WORLD_PATH_REAL}"
  383.     local RET=$?
  384.  
  385.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  386.  
  387.     if [ $RET -ne 0 ]; then
  388.         echo "${L_TIMESTAMP} [INFO] copy_save_files_from_tmp_to_game_dir(): Error copying save files!" | tee -a ${EVENT_LOG_FILE}
  389.         return 1
  390.     else
  391.         echo "${L_TIMESTAMP} [INFO] copy_save_files_from_tmp_to_game_dir(): Done copying files" | tee -a ${EVENT_LOG_FILE}
  392.     fi
  393. }
  394.  
  395.  
  396. remove_save_files_from_tmp_location() {
  397.     if [ -n ${INSTANCE_PATH_TMP} ]
  398.     then
  399.         L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  400.         echo "${L_TIMESTAMP} [INFO] remove_save_files_from_tmp_location(): Removing the save files from the temp directory..." | tee -a ${EVENT_LOG_FILE}
  401.  
  402.         as_user "rm -r --preserve-root ${INSTANCE_PATH_TMP} > /dev/null"
  403.  
  404.         L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  405.         echo "${L_TIMESTAMP} [INFO] remove_save_files_from_tmp_location(): Done removing files" | tee -a ${EVENT_LOG_FILE}
  406.  
  407.         # World path is a symlink
  408.         #if [ -h "${WORLD_PATH_SYMLINK}" ]
  409.         #then
  410.         #   echo "${L_TIMESTAMP} [INFO] remove_save_files_from_tmp_location(): Changing the symlink to point to the real world" | tee -a ${EVENT_LOG_FILE}
  411.         #   as_user "rm ${WORLD_PATH_SYMLINK} > /dev/null 2>&1"
  412.         #   as_user "cd ${INSTANCE_PATH}"
  413.         #   local WORLDNAME=`basename ${WORLD_PATH_REAL}`
  414.         #   as_user "ln -s ${WORLDNAME} ${WORLD_PATH_SYMLINK} > /dev/null 2>&1"
  415.         #fi
  416.     fi
  417. }
  418.  
  419.  
  420. mc_backup() {
  421.     TIMESTAMP_TAR=`date '+%Y-%m-%d_%H.%M.%S'`
  422.     TIMESTAMP_GIT=`date '+%Y-%m-%d %H:%M'`
  423.  
  424.     if [ "${USE_TEMP_LOCATION}" = "true" ] && [ -e "${WORLD_PATH_TMP}" ]
  425.     then
  426.         # Copy the save files from the temp directory (in RAM) into the actual server directory
  427.         copy_save_files_from_tmp_to_game_dir
  428.         local RET=$?
  429.  
  430.         if [ $RET -ne 0 ]; then
  431.             L_PREFIX=`date "+%Y-%m-%d %H:%M:%S"`
  432.             echo "${L_PREFIX} [INFO] mc_backup(): Failed to copy save files from temp to game dir for instance '${INSTANCE}'" | tee -a ${EVENT_LOG_FILE} > /dev/null
  433.             echo "${L_PREFIX} [INFO] mc_backup(): Failed to copy save files from temp to game dir for instance '${INSTANCE}'" | tee -a ${BACKUP_LOG_FILE} > /dev/null
  434.         fi
  435.     fi
  436.  
  437.     if [ "${BACKUP_USING_TAR}" = "true" ]
  438.     then
  439.         L_PREFIX=`date "+%Y-%m-%d %H:%M:%S"`
  440.  
  441.         if [ "${BACKUP_WORLD_ONLY}" = "true" ]
  442.         then
  443.             echo "${L_PREFIX} [INFO] mc_backup(): Starting a backup of the world of '${INSTANCE}' using tar" | tee -a ${EVENT_LOG_FILE} > /dev/null
  444.             echo "${L_PREFIX} [INFO] mc_backup(): Starting a backup of the world of '${INSTANCE}' using tar" | tee -a ${BACKUP_LOG_FILE} > /dev/null
  445.  
  446.             as_user "cd ${INSTANCE_PATH} && tar --exclude '.git' -czpf ${BACKUP_PATH}/${BACKUP_PREFIX}_world_${TIMESTAMP_TAR}.tar.gz ${WORLD_NAME} | tee -a ${BACKUP_LOG_FILE} > /dev/null 2>&1"
  447.         else
  448.             echo "${L_PREFIX} [INFO] mc_backup(): Starting a backup of the server '${INSTANCE}' using tar" | tee -a ${EVENT_LOG_FILE} > /dev/null
  449.             echo "${L_PREFIX} [INFO] mc_backup(): Starting a backup of the server '${INSTANCE}' using tar" | tee -a ${BACKUP_LOG_FILE} > /dev/null
  450.  
  451.             as_user "cd `dirname ${INSTANCE_PATH}` && tar --exclude '.git' -czpf ${BACKUP_PATH}/${BACKUP_PREFIX}_${TIMESTAMP_TAR}.tar.gz `basename ${INSTANCE_PATH}` | tee -a ${BACKUP_LOG_FILE} > /dev/null 2>&1"
  452.         fi
  453.     fi
  454.  
  455.     if [ "${BACKUP_USING_GIT}" = "true" ]
  456.     then
  457.         # Do we have an additional commit message?
  458.         if [ -n "${1}" ]
  459.         then
  460.             MSG=" ${1}"
  461.         else
  462.             MSG=""
  463.         fi
  464.  
  465.         # Backup before a version update, allow empty commits, to record the special commit message
  466.         if [ "${2}" = "true" ]
  467.         then
  468.             local GIT_PRM_ALLOW_EMPTY="--allow-empty"
  469.         fi
  470.  
  471.         L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  472.         L_PREFIX="${L_TIMESTAMP}"
  473.  
  474.         if [ "${BACKUP_WORLD_ONLY}" = "true" ]
  475.         then
  476.             echo "${L_PREFIX} [INFO] mc_backup(): Starting a backup of the world of '${INSTANCE}' using git" | tee -a ${EVENT_LOG_FILE} > /dev/null
  477.             echo "${L_PREFIX} [INFO] mc_backup(): Starting a backup of the world of '${INSTANCE}' using git" | tee -a ${BACKUP_LOG_FILE} > /dev/null
  478.  
  479.             as_user "cd ${WORLD_PATH_SYMLINK} && git add -A . | tee -a ${BACKUP_LOG_FILE} && git commit ${GIT_PRM_ALLOW_EMPTY} -m \"${TIMESTAMP_GIT}${MSG}\" | tee -a ${BACKUP_LOG_FILE}"
  480.         else
  481.             echo "${L_PREFIX} [INFO] mc_backup(): Starting a backup of the server '${INSTANCE}' using git" | tee -a ${EVENT_LOG_FILE} > /dev/null
  482.             echo "${L_PREFIX} [INFO] mc_backup(): Starting a backup of the server '${INSTANCE}' using git" | tee -a ${BACKUP_LOG_FILE} > /dev/null
  483.  
  484.             as_user "cd ${INSTANCE_PATH} && git add -A . | tee -a ${BACKUP_LOG_FILE} && git commit ${GIT_PRM_ALLOW_EMPTY} -m \"${TIMESTAMP_GIT}${MSG}\" | tee -a ${BACKUP_LOG_FILE}"
  485.         fi
  486.     fi
  487.  
  488.     if [ "${BACKUP_SERVICE}" = "true" ]
  489.     then
  490.         L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  491.         L_PREFIX="${L_TIMESTAMP}"
  492.  
  493.         echo "${L_PREFIX} [INFO] mc_backup(): Backing up service ${SERVICE}" | tee -a ${EVENT_LOG_FILE} > /dev/null
  494.         as_user "cd ${INSTANCE_PATH} && cp -pn ${SERVICE} ${BACKUP_PATH}/${BACKUP_PREFIX}_${TIMESTAMP_TAR}.jar > /dev/null 2>&1"
  495.     fi
  496.  
  497.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  498.     L_PREFIX="${L_TIMESTAMP}"
  499.     echo "${L_PREFIX} [INFO] mc_backup(): Backup of server instance '${INSTANCE}' complete" | tee -a ${EVENT_LOG_FILE} > /dev/null
  500.     echo "${L_PREFIX} [INFO] mc_backup(): Backup of server instance '${INSTANCE}' complete" | tee -a ${BACKUP_LOG_FILE} > /dev/null
  501. }
  502.  
  503.  
  504. mc_backup_wrapper() {
  505.     if [ "${DISABLE_AUTOSAVE}" == "true" ]
  506.     then
  507.         mc_say "§dSERVER BACKUP STARTING"
  508.         mc_saveall
  509.         sleep ${SAVEALL_BACKUP_INTERVAL}
  510.  
  511.         mc_backup "${1}"
  512.  
  513.         mc_say "§dSERVER BACKUP FINISHED"
  514.     else
  515.         mc_say "§dSERVER BACKUP STARTING, server going read-only"
  516.         mc_saveoff
  517.         mc_saveall
  518.         sleep ${SAVEALL_BACKUP_INTERVAL}
  519.  
  520.         mc_backup "${1}"
  521.  
  522.         mc_say "§dSERVER BACKUP FINISHED, server going read-write"
  523.         mc_saveon
  524.     fi
  525. }
  526.  
  527.  
  528. mc_startifcrashed_basic() {
  529.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  530.     L_PREFIX="${L_TIMESTAMP}"
  531.  
  532.     # If the state file is present, then the server is supposed to be running
  533.     if [ -f "${STATE_FILE}" ]
  534.     then
  535.         # If the state file was present, but the process is not found, we assume that the server has crashed
  536.         if ! pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  537.         then
  538.             echo "${L_PREFIX} [WARNING] mc_startifcrashed_basic(): ${SERVICE} is not running (crashed?)" | tee -a ${EVENT_LOG_FILE}
  539.             echo "${L_PREFIX} [INFO] mc_startifcrashed_basic(): Attempting to start ${SERVICE}..." | tee -a ${EVENT_LOG_FILE}
  540.  
  541.             mc_start
  542.  
  543.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  544.             L_PREFIX="${L_TIMESTAMP}"
  545.  
  546.             if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  547.             then
  548.                 echo "${L_PREFIX} [INFO] mc_startifcrashed_basic(): ${SERVICE} is now running" | tee -a ${EVENT_LOG_FILE}
  549.             else
  550.                 echo "${L_PREFIX} [ERROR] mc_startifcrashed_basic(): Could not start ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  551.             fi
  552.         else
  553.             echo "${L_PREFIX} [INFO] mc_startifcrashed_basic(): ${SERVICE} is running" | tee -a ${EVENT_LOG_FILE}
  554.         fi
  555.     else
  556.         echo "${L_PREFIX} [INFO] mc_startifcrashed_basic(): ${SERVICE} has been stopped gracefully" | tee -a ${EVENT_LOG_FILE}
  557.     fi
  558. }
  559.  
  560.  
  561. mc_startifcrashed_ping() {
  562.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  563.     L_PREFIX="${L_TIMESTAMP}"
  564.  
  565.     # If the state file is present, then the server is supposed to be running
  566.     if [ -f "${STATE_FILE}" ]
  567.     then
  568.         # If the state file was present, and the process is found, we try to ping the server
  569.         if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  570.         then
  571.             echo "${L_PREFIX} [INFO] mc_startifcrashed_ping(): ${SERVICE} is running, pinging it..." | tee -a ${EVENT_LOG_FILE}
  572.  
  573.             # We try the ping a maximum of PING_RETRY_COUNT times, to try and avoid false positive failures and restarts
  574.             COUNTER=0
  575.             while [ ${COUNTER} -lt ${PING_RETRY_COUNT} ]; do
  576.  
  577.                 if ${PING_INVOCATION} | grep ': OK' > /dev/null
  578.                 then
  579.                     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  580.                     L_PREFIX="${L_TIMESTAMP}"
  581.  
  582.                     echo "${L_PREFIX} [INFO] mc_startifcrashed_ping(): ${SERVICE} responded to ping" | tee -a ${EVENT_LOG_FILE}
  583.                     break
  584.                 fi
  585.  
  586.                 sleep ${PING_RETRY_DELAY}
  587.             done
  588.  
  589.             # If the ping failed every time, then we assume that the server has crashed and we will kill it and start it up again
  590.             if [ ${COUNTER} -ge ${PING_RETRY_COUNT} ]
  591.             then
  592.                 L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  593.                 L_PREFIX="${L_TIMESTAMP}"
  594.  
  595.                 echo "${L_PREFIX} [WARNING] mc_startifcrashed_ping(): ${SERVICE} doesn't respond to ping (crashed?)" | tee -a ${EVENT_LOG_FILE}
  596.                 echo "${L_PREFIX} [INFO] mc_startifcrashed_ping(): Killing and (re-)starting ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  597.  
  598.                 mc_kill
  599.                 sleep ${KILL_TO_START_DELAY}
  600.                 mc_start
  601.  
  602.                 L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  603.                 L_PREFIX="${L_TIMESTAMP}"
  604.  
  605.                 if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  606.                 then
  607.                     echo "${L_PREFIX} [INFO] mc_startifcrashed_ping(): ${SERVICE} is now running" | tee -a ${EVENT_LOG_FILE}
  608.                 else
  609.                     echo "${L_PREFIX} [ERROR] mc_startifcrashed_ping(): Could not start ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  610.                 fi
  611.             fi
  612.         else
  613.             # Process not found (crashed?), we try to start the server
  614.             echo "${L_PREFIX} [WARNING] mc_startifcrashed_ping(): ${SERVICE} not running (crashed?)" | tee -a ${EVENT_LOG_FILE}
  615.             echo "${L_PREFIX} [INFO] mc_startifcrashed_ping(): Attempting to start ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  616.  
  617.             mc_start
  618.  
  619.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  620.             L_PREFIX="${L_TIMESTAMP}"
  621.  
  622.             if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  623.             then
  624.                 echo "${L_PREFIX} [INFO] mc_startifcrashed_ping(): ${SERVICE} is now running" | tee -a ${EVENT_LOG_FILE}
  625.             else
  626.                 echo "${L_PREFIX} [ERROR] mc_startifcrashed_ping(): Could not start ${SERVICE}" | tee -a ${EVENT_LOG_FILE}
  627.             fi
  628.         fi
  629.     else
  630.         # No state file present, the server is supposed to be down
  631.         echo "${L_PREFIX} [INFO] mc_check(): ${SERVICE} has been stopped gracefully" | tee -a ${EVENT_LOG_FILE}
  632.     fi
  633. }
  634.  
  635.  
  636. mc_kill() {
  637.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  638.     L_PREFIX="${L_TIMESTAMP}"
  639.  
  640.     if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  641.     then
  642.         echo "${L_PREFIX} [INFO] mc_kill(): Killing minecraft server instance '${INSTANCE}'..." | tee -a ${EVENT_LOG_FILE}
  643.  
  644.         as_user "pkill -SIGKILL -u ${USERNAME} -f ${SERVICE}"
  645.         sleep ${POST_KILL_DELAY}
  646.  
  647.         COUNTER=0
  648.         while [ ${COUNTER} -lt ${KILL_GRACE_DELAY} ]; do
  649.             if ! pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  650.             then
  651.                 L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  652.                 L_PREFIX="${L_TIMESTAMP}"
  653.                 echo "${L_PREFIX} [INFO] mc_kill(): Server instance '${INSTANCE}' has been killed" | tee -a ${EVENT_LOG_FILE}
  654.  
  655.                 # FIXME: Could this be done cleaner?
  656.                 screen -wipe
  657.  
  658.                 if [ "${USE_TEMP_LOCATION}" = "true" ]
  659.                 then
  660.                     # Copy the save files from the temp directory (in RAM) into the actual server directory
  661.                     copy_save_files_from_tmp_to_game_dir
  662.                     local RET=$?
  663.  
  664.                     if [ $RET -eq 0 ]; then
  665.                         remove_save_files_from_tmp_location
  666.                     fi
  667.                 fi
  668.  
  669.                 break
  670.             fi
  671.  
  672.             sleep 1
  673.  
  674.             ((COUNTER += 1))
  675.         done
  676.  
  677.         if [ ${COUNTER} -ge ${KILL_GRACE_DELAY} ]
  678.         then
  679.             echo "${L_PREFIX} [ERROR] mc_kill(): Unable to kill minecraft server '${INSTANCE}'" | tee -a ${EVENT_LOG_FILE}
  680.         fi
  681.     else
  682.         echo "${L_PREFIX} [WARNING] mc_kill(): Minecraft server instance '${INSTANCE}' was not running" | tee -a ${EVENT_LOG_FILE}
  683.     fi
  684. }
  685.  
  686.  
  687. mc_download_server() {
  688.     # Only do something if we have a version argument
  689.     if [ -n "${1}" ]
  690.     then
  691.         VERSION="${1}"
  692.         # If the requested server version doesn't yet exist, we attempt to download it
  693.         if [ ! -f "${SERVER_FILES_PATH}/${SERVER_FILE_PREFIX}${VERSION}.jar" ]
  694.         then
  695.             MC_SERVER_URL="https://s3.amazonaws.com/Minecraft.Download/versions/${VERSION}/minecraft_server.${VERSION}.jar"
  696.             as_user "wget -q -O \"${SERVER_FILES_PATH}/${SERVER_FILE_PREFIX}${VERSION}.jar\" ${MC_SERVER_URL}"
  697.  
  698.             # If wget returns 0
  699.             if [ "$?" -eq "0" ]
  700.             then
  701.                 L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  702.                 L_PREFIX="${L_TIMESTAMP}"
  703.                 echo "${L_PREFIX} [INFO] mc_download_server(): Downloaded ${MC_SERVER_URL} to ${SERVER_FILES_PATH}/${SERVER_FILE_PREFIX}${VERSION}.jar" | tee -a ${EVENT_LOG_FILE}
  704.             else
  705.                 # Download failed
  706.                 echo "${L_PREFIX} [ERROR] mc_download_server(): Download of ${MC_SERVER_URL} failed" | tee -a ${EVENT_LOG_FILE}
  707.                 # Remove the empty file just created
  708.                 as_user "rm ${SERVER_FILES_PATH}/${SERVER_FILE_PREFIX}${VERSION}.jar"
  709.                 exit 1
  710.             fi
  711.         fi
  712.     fi
  713. }
  714.  
  715.  
  716. mc_update() {
  717.     CURRENT_VERSION=`readlink -f "${INSTANCE_PATH}/${SERVICE}" | sed "s/.*\/${SERVER_FILE_PREFIX}\(.*\)\.jar/\1/"`
  718.  
  719.     # Any version change via this script on this server instance is forbidden
  720.     if [ "${ALLOW_UPDATES}" = 'false' ] && [ "${ALLOW_DOWNGRADES}" = 'false' ]
  721.     then
  722.         L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  723.         L_PREFIX="${L_TIMESTAMP}"
  724.         echo "${L_PREFIX} [ERROR] mc_update(): Any updates or downgrades of '${SERVICE}' are not allowed (@ ${CURRENT_VERSION})" | tee -a ${EVENT_LOG_FILE}
  725.         return
  726.     fi
  727.  
  728.     if [ -z "${1}" ]
  729.     then
  730.         # No version specified, assume we want the latest stable release
  731.         VERSIONS_URL="https://launchermeta.mojang.com/mc/game/version_manifest.json"
  732.         # Old URL: VERSIONS_URL="https://s3.amazonaws.com/Minecraft.Download/versions/versions.json"
  733.  
  734.         as_user "wget -q -O ${VERSIONS_JSON} ${VERSIONS_URL}"
  735.  
  736.         # Get the latest[release] value
  737.         VERSION=`tr '\r\n' ' ' < ${VERSIONS_JSON} | sed "s/ //g" | sed 's/.*\("latest[^}]\+}\).*/\1/' | sed 's/.*"release":"\([^"]\+\)"}/\1/'`
  738.     else    # Use the version given as an argument
  739.         VERSION="${1}"
  740.     fi
  741.  
  742.     echo "Target version: ${VERSION}, Current version: ${CURRENT_VERSION}"
  743.     #exit
  744.  
  745.     # If the requested version is different from the current version
  746.     if [ "${CURRENT_VERSION}" != "${VERSION}" ]
  747.     then
  748.         # Check that the requested version change is permitted
  749.         # FIXME This won't work correctly with alpha, beta etc. prefixed versions
  750.         if [[ "${VERSION}" < "${CURRENT_VERSION}" ]] && [ "${ALLOW_DOWNGRADES}" != 'true' ]
  751.         then
  752.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  753.             L_PREFIX="${L_TIMESTAMP}"
  754.             echo "${L_PREFIX} [ERROR] mc_update(): Downgrades of '${SERVICE}' are not allowed (${CURRENT_VERSION} -> ${VERSION})" | tee -a ${EVENT_LOG_FILE}
  755.             return
  756.         fi
  757.         # FIXME This won't work correctly with alpha, beta etc. prefixed versions
  758.         if [[ "${VERSION}" > "${CURRENT_VERSION}" ]] && [ "${ALLOW_UPDATES}" != 'true' ]
  759.         then
  760.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  761.             L_PREFIX="${L_TIMESTAMP}"
  762.             echo "${L_PREFIX} [ERROR] mc_update(): Updates of '${SERVICE}' are not allowed (${CURRENT_VERSION} -> ${VERSION})" | tee -a ${EVENT_LOG_FILE}
  763.             return
  764.         fi
  765.  
  766.         # If the server is currently running, broadcast a message before starting the update process
  767.         if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  768.         then
  769.             RUNNING="true"
  770.  
  771.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  772.             L_PREFIX="${L_TIMESTAMP}"
  773.             echo "${L_PREFIX} [INFO] mc_update(): ${SERVICE} is running, stopping it and starting the update" | tee -a ${EVENT_LOG_FILE}
  774.  
  775.             mc_say "§d=== SERVER UPDATE STARTING ==="
  776.             sleep 5
  777.             mc_stop "broadcast"
  778.         else
  779.             RUNNING="false"
  780.  
  781.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  782.             L_PREFIX="${L_TIMESTAMP}"
  783.             echo "${L_PREFIX} [INFO] mc_update(): ${SERVICE} is stopped, starting the update" | tee -a ${EVENT_LOG_FILE}
  784.         fi
  785.  
  786.         mc_backup "Backup before updating to version ${VERSION} (@ ${CURRENT_VERSION})" "true"
  787.         mc_download_server ${VERSION}
  788.  
  789.         # If the requested server file exists, update the symlink
  790.         if [ -f "${SERVER_FILES_PATH}/${SERVER_FILE_PREFIX}${VERSION}.jar" ]
  791.         then
  792.             as_user "ln -fs \"${SERVER_FILES_PATH}/${SERVER_FILE_PREFIX}${VERSION}.jar\" \"${INSTANCE_PATH}/${SERVICE}\""
  793.  
  794.             if [ "${RUNNING}" = "true" ]
  795.             then
  796.                 L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  797.                 L_PREFIX="${L_TIMESTAMP}"
  798.                 echo "${L_PREFIX} [INFO] mc_update(): Updated '${SERVICE}' to version '${VERSION}', starting ${SERVICE}..." | tee -a ${EVENT_LOG_FILE}
  799.  
  800.                 mc_start
  801.             else
  802.                 L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  803.                 L_PREFIX="${L_TIMESTAMP}"
  804.                 echo "${L_PREFIX} [INFO] mc_update(): Updated '${SERVICE}' to version '${VERSION}'" | tee -a ${EVENT_LOG_FILE}
  805.             fi
  806.         else
  807.             # Download failed
  808.             L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  809.             L_PREFIX="${L_TIMESTAMP}"
  810.             echo "${L_PREFIX} [ERROR] mc_update(): Update of '${SERVICE}' to version '${VERSION}' failed" | tee -a ${EVENT_LOG_FILE}
  811.         fi
  812.     else
  813.         L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  814.         L_PREFIX="${L_TIMESTAMP}"
  815.         echo "${L_PREFIX} [WARNING] mc_update(): '${SERVICE}' is already at version '${VERSION}', not doing anything" | tee -a ${EVENT_LOG_FILE}
  816.     fi
  817. }
  818.  
  819.  
  820. mc_say() {
  821.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  822.     L_PREFIX="${L_TIMESTAMP}"
  823.  
  824.     if [ -n "${1}" ]
  825.     then
  826.         MSG="${1}"
  827.  
  828.         if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  829.         then
  830. #           echo "${L_PREFIX} [INFO] mc_say(): ${SERVICE} is running, saying '${MSG}'" | tee -a ${EVENT_LOG_FILE}
  831.             as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"say ${MSG}\"\015'"
  832. #       else
  833. #           echo "${L_PREFIX} [WARNING] mc_say(): ${SERVICE} is not running" | tee -a ${EVENT_LOG_FILE}
  834.         fi
  835.     else
  836.         echo "${L_PREFIX} [ERROR] mc_say(): You must specify a message" | tee -a ${EVENT_LOG_FILE}
  837.     fi
  838. }
  839.  
  840.  
  841. mc_command() {
  842.     L_TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
  843.     L_PREFIX="${L_TIMESTAMP}"
  844.  
  845.     if [ "${1}" ]
  846.     then
  847.         command="${1}"
  848.  
  849.         if pgrep -u ${USERNAME} -f ${SERVICE} > /dev/null
  850.         then
  851.             echo "${L_PREFIX} [INFO] mc_command(): ${SERVICE} is running, executing command '${command}'" | tee -a ${EVENT_LOG_FILE}
  852.             as_user "screen -p 0 -S ${SCREEN_SESSION} -X eval 'stuff \"${command}\"\015'"
  853.         fi
  854.     else
  855.         echo "${L_PREFIX} [ERROR] mc_command(): You must specify a server command" | tee -a ${EVENT_LOG_FILE}
  856.     fi
  857. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement