Advertisement
Guest User

minecraft_server overviewer log rotation

a guest
Dec 23rd, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.44 KB | None | 0 0
  1. --- minecraft_server    Sun Dec 23 15:47:12 2012
  2. +++ minecraft_server    Sun Dec 23 15:51:52 2012
  3. @@ -33,6 +33,8 @@
  4.  #   watch <world>         - Watch the log file for the Minecraft world server.
  5.  #   backup <world>        - Backup the Minecraft world.  Backup all worlds by
  6.  #                           default.
  7. +#   logrotate <world>     - Rotate the server.log file.
  8. +#                           Will rotate the server log for all worlds by default.
  9.  #   map <world>           - Run the Minecraft Overviewer mapping software on
  10.  #                           the Minecraft world.  Map all worlds by default.
  11.  #   update <software>     - Update a software package.  Update the server
  12. @@ -162,6 +164,11 @@
  13.  # Length in days that backups survive.
  14.  BACKUP_DURATION=31
  15.  
  16. +## Server Log Configuration
  17. +
  18. +# How many rotations of server.log to keep
  19. +LOG_COUNT=10
  20. +
  21.  
  22.  ## Mirror image options.
  23.  
  24. @@ -489,6 +496,56 @@
  25.     done
  26.  }
  27.  
  28. +# Rotates the world server log file.
  29. +#
  30. +# @param 1 The world server generating the log to rotate.
  31. +rotateLog() {
  32. +   local WORLD_DIR LOG_LIST LOGBASE LOGTMP LOGNEW
  33. +
  34. +   WORLD_DIR="$WORLDS_LOCATION/$1"
  35. +   # Use the mirror copy of the world directory if enabled.
  36. +   if [ $ENABLE_MIRROR -eq 1 ] && [ -d $MIRROR_PATH ]; then
  37. +       WORLD_DIR="$MIRROR_PATH/$1"
  38. +   fi
  39. +
  40. +   # Make sure that the server.log file exists.
  41. +   execute "touch $WORLD_DIR/server.log" $USER_NAME
  42. +
  43. +   # Scan the log for entires and skip rotate is none are found
  44. +   local log_lines="$(cat "$WORLD_DIR/server.log" | wc -l )"
  45. +
  46. +   if [ "$log_lines" -le '1' ]; then
  47. +       printf "No new log enteries to rotate. No changes made."
  48. +       return 0
  49. +   fi
  50. +
  51. +   # Server logfiles in chronological order
  52. +   LOGLIST=$(ls -r $WORLD_DIR/server.log* | grep -v lck)
  53. +
  54. +   # Look at all the logfiles
  55. +   for i in $LOGLIST; do
  56. +       LOGTMP=$(ls $i | cut -d "." -f 3)
  57. +       # If we're working with server.log, append .1 then compress it
  58. +       if [ -z $LOGTMP ]; then
  59. +           LOGTMP="$WORLD_DIR/server.log"
  60. +           LOGNEW="${LOGTMP}.1"
  61. +           execute "cp $WORLD_DIR/server.log $LOGNEW" $USER_NAME
  62. +           execute "gzip $LOGNEW" $USER_NAME
  63. +       # Otherwise, check if the file number is under $LOG_COUNT
  64. +       elif [ $LOGTMP -eq $LOG_COUNT ]; then
  65. +           # If so, delete it
  66. +           execute "rm -f $i" $USER_NAME
  67. +       else
  68. +           # Otherwise, add one to the number
  69. +           LOGBASE=$(ls $i | cut -d "." -f 1-2)
  70. +           LOGNEW=$LOGBASE.$(($LOGTMP+1))
  71. +           execute "cp $i $LOGNEW.gz" $USER_NAME
  72. +       fi
  73. +   done
  74. +   # Blank the existing logfile to renew it
  75. +   execute "cp \"/dev/null\" $WORLD_DIR/server.log" $USER_NAME
  76. +}
  77. +
  78.  # Watch the world server log file.
  79.  #
  80.  # @param 1 The world server generating the log to watch.
  81. @@ -907,6 +964,17 @@
  82.             exit 1
  83.         fi
  84.     ;;
  85. +   logrotate)
  86. +       # Figure out which worlds to rotate the log.
  87. +       WORLDS=$(checkOptionalArgument "$ALL_WORLDS" $0 $1 $2)
  88. +       # Backup each world requested.
  89. +       printf "Rotating Server Log:"
  90. +       for WORLD in $WORLDS; do
  91. +           printf " $WORLD"
  92. +           rotateLog $WORLD
  93. +       done
  94. +       printf "\n"
  95. +   ;;
  96.     backup)
  97.         # Figure out which worlds to backup.
  98.         WORLDS=$(checkOptionalArgument "$ALL_WORLDS" $0 $1 $2)
  99. @@ -1002,7 +1070,7 @@
  100.         printf "\n"
  101.     ;;
  102.     *)
  103. -       printf "Usage: $0 {start|stop|force-stop|restart|force-restart|status|sync|send|screen|watch|backup|update|map} {Optional: world or software package}\n"
  104. +       printf "Usage: $0 {start|stop|force-stop|restart|force-restart|status|sync|send|screen|watch|backup|update|logrotate|map} {Optional: world or software package}\n"
  105.         exit 1
  106.     ;;
  107.  esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement