Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. #!/bin/bash
  2. # /etc/init.d/minecraft
  3.  
  4. # Misc settings
  5. SERVICE='craftbukkit-0.0.1-SNAPSHOT.jar'
  6. USERNAME="minecraft"
  7. MCPATH='/home/minecraft/bukkit'
  8. INVOCATION="java -server -d64 -Xmn384M -Xms1536M -Xmx1536M -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -XX:+UseAdaptiveGCBoundary -XX:MaxGCPauseMillis=500 -XX:-UseGCOverheadLimit -XX:SurvivorRatio=16 -Xnoclassgc -XX:UseSSE=3 -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=3 -XX:+UseCompressedOops -jar $SERVICE nogui"
  9. STAMP=`date +%Y%m%d_%H%M`
  10.  
  11. # Backup
  12. BACKUP_PATH='/home/minecraft/bukkit/backups'
  13. WORLDS='world1;Anvil' # semicolon-separated list of worlds to backup
  14. OLDBACKUPS=10 # in days
  15. SQLDB='bukkit'
  16. SQLPASS="derptastic"
  17.  
  18. # pigmap
  19. PIGMAP_PATH='/home/minecraft/bukkit/pigmap'
  20. PIGMAP_OUTPUT_PATH='/var/www/map'
  21. PIGMAP_WORLDS='world1;Anvil'
  22.  
  23. # Don't change anything beyond this point
  24. ME=`whoami`
  25. as_user() {
  26. START=$(date +%s.%N)
  27. if [ $ME == $USERNAME ] ; then
  28. bash -c "$1"
  29. else
  30. su $USERNAME -c "$1"
  31. fi
  32. END=$(date +%s.%N)
  33. DIFF=$(echo "$END - $START" | bc | awk '{printf("%.3f",$1 + 0.0005)}')
  34. }
  35.  
  36. as_root() {
  37. if [[ $EUID -ne 0 ]] ; then
  38. echo "Command must be run as root"
  39. else
  40. bash -c "$1"
  41. fi
  42. }
  43.  
  44. is_running() {
  45. if ! ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null; then
  46. return 1
  47. fi
  48. }
  49.  
  50. cmd_as_user() {
  51. if is_running; then
  52. as_user "screen -p 0 -S minecraft -X eval 'stuff \"$1\"\015'"
  53. fi
  54. }
  55.  
  56. mc_start() {
  57. if is_running; then
  58. echo "Tried to start but $SERVICE was already running!"
  59. else
  60. echo "$SERVICE was not running... starting."
  61. cd $MCPATH
  62. as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION"
  63. sleep 7
  64. if is_running; then
  65. echo "$SERVICE is now running."
  66. else
  67. echo "Could not start $SERVICE."
  68. fi
  69. fi
  70. }
  71.  
  72. mc_saveoff() {
  73. if is_running; then
  74. echo "$SERVICE is running... suspending saves"
  75. cmd_as_user "save-off"
  76. cmd_as_user "save-all"
  77. sync
  78. sleep 10
  79. else
  80. echo "$SERVICE was not running. Not suspending saves."
  81. fi
  82. }
  83.  
  84. mc_saveon() {
  85. if is_running; then
  86. echo "$SERVICE is running... re-enabling saves"
  87. cmd_as_user "save-on"
  88. else
  89. echo "$SERVICE was not running. Not resuming saves."
  90. fi
  91. }
  92.  
  93. mc_stop() {
  94. if is_running; then
  95. echo "$SERVICE is running... stopping."
  96. cmd_as_user "say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map..."
  97. cmd_as_user "save-all"
  98. sleep 10
  99. cmd_as_user "stop"
  100. sleep 15
  101. else
  102. echo "$SERVICE was not running."
  103. fi
  104.  
  105. if is_running; then
  106. echo "$SERVICE could not be shut down... still running."
  107. else
  108. echo "$SERVICE is shut down."
  109. if [ ! -d "$MCPATH/logs" ]; then
  110. as_user "mkdir $MCPATH/logs"
  111. fi
  112. as_user "mv $MCPATH/server.log $MCPATH/logs/server-$STAMP.log"
  113. fi
  114. }
  115.  
  116. mc_dosave() {
  117. if is_running; then
  118. echo "$SERVICE is running... doing save-all"
  119. cmd_as_user "save-all"
  120. #cmd_as_user "say Saved worlds! Took ${DIFF}s"
  121. sync
  122. sleep 10
  123. else
  124. echo "$SERVICE was not running. Not doing save-all."
  125. fi
  126. }
  127.  
  128. mc_backup() {
  129. cd $BACKUP_PATH
  130. WORLDS=$(echo $WORLDS | tr ";" "\n")
  131.  
  132. cmd_as_user "say Server backup starting in 5s, going readonly..."
  133. sleep 5
  134.  
  135. echo "Dumping SQL database '$SQLDB'"
  136. as_root "nice -n 19 mysqldump -u root -h localhost -p$SQLPASS $SQLDB > $SQLDB-db.sql"
  137.  
  138. mc_saveoff
  139.  
  140. for WORLD in $WORLDS; do
  141. echo "Backing up world '$WORLD'"
  142. as_user "nice -n 19 rsync -a --delete $MCPATH/$WORLD/ $WORLD"
  143. done
  144.  
  145. mc_saveon
  146.  
  147. if [ "$1" == "no-snap" ]; then
  148. echo "Skipping snapshot creation"
  149. else
  150. echo "Deleting snapshots older than $OLDBACKUPS days"
  151. as_user "find snapshot-* -type d -mtime +$OLDBACKUPS | xargs rm -rf"
  152.  
  153. echo "Creating snapshot $STAMP"
  154. FINALDIR="snapshot-$STAMP"
  155. as_user "mkdir $FINALDIR"
  156.  
  157. for WORLD in $WORLDS; do
  158. as_user "nice -n 19 tar -czf $FINALDIR/$WORLD.tar.gz $WORLD"
  159. done
  160.  
  161. as_user "nice -n 19 rsync -a --delete $MCPATH/plugins/ plugins && tar -czf $FINALDIR/plugins.tar.gz plugins"
  162. as_user "nice -n 19 gzip -c $SQLDB-db.sql > $FINALDIR/$SQLDB-db.sql.gz"
  163. echo "Backup complete"
  164. fi
  165.  
  166. cmd_as_user "say Server backup ended, going read-write"
  167. }
  168.  
  169. mc_pigmap() {
  170. cd $PIGMAP_PATH
  171. PIGMAP_WORLDS=$(echo $PIGMAP_WORLDS | tr ";" "\n")
  172.  
  173. if [ "$1" ]; then
  174. if ! (for i in ${PIGMAP_WORLDS[@]}; do if [[ $i == ${1} ]]; then return 0; fi; done; return 1); then
  175. echo "Invalid world"
  176. exit 1
  177. fi
  178. PIGMAP_WORLDS=($1)
  179. fi
  180.  
  181. mc_saveoff
  182.  
  183. for WORLD in $PIGMAP_WORLDS; do
  184. as_user "nice -n 19 rsync -va $MCPATH/$WORLD/ $WORLD > $WORLD.regionlist"
  185. done
  186.  
  187. mc_saveon
  188.  
  189. if [ "$2" == "full" ]; then
  190. echo "Rendering full world map '$1'"
  191. cmd_as_user "say Rendering full world map '$1'..."
  192. as_user "nice -n 19 ./pigmap -B 6 -T 1 -i $1 -o $PIGMAP_OUTPUT_PATH/$1 -h 1"
  193. TOTAL=$DIFF
  194. as_user "mv $PIGMAP_OUTPUT_PATH/$1/pigmap-default.html $PIGMAP_OUTPUT_PATH/$1/index.html"
  195. else
  196. cmd_as_user "say Rendering world maps..."
  197. TOTAL=0
  198. for WORLD in $PIGMAP_WORLDS; do
  199. echo "Rendering world map '$WORLD'"
  200. as_user "nice -n 19 ./pigmap -i $WORLD -o $PIGMAP_OUTPUT_PATH/$WORLD -r $WORLD.regionlist -h 1 -x"
  201. TOTAL=$(echo "$TOTAL + $DIFF" | bc)
  202. as_user "mv $PIGMAP_OUTPUT_PATH/$WORLD/pigmap-default.html $PIGMAP_OUTPUT_PATH/$WORLD/index.html"
  203. done
  204. fi
  205.  
  206. cmd_as_user "say Render completed in ${TOTAL}s. Visit crazycraft.org/map"
  207. }
  208.  
  209. mc_status() {
  210. if is_running; then
  211. echo "$SERVICE is running."
  212. else
  213. echo "$SERVICE is not running."
  214. fi
  215. }
  216.  
  217. # Commands here
  218. case "$1" in
  219. start)
  220. mc_start
  221. ;;
  222. stop)
  223. mc_stop
  224. ;;
  225. restart)
  226. mc_stop
  227. mc_start
  228. ;;
  229. save)
  230. mc_dosave
  231. ;;
  232. backup)
  233. mc_backup $2
  234. ;;
  235. status)
  236. mc_status
  237. ;;
  238. pigmap)
  239. mc_pigmap $2 $3
  240. ;;
  241. *)
  242. echo "Usage: /etc/init.d/minecraft {start|stop|update|backup [no-snap]|status|restart|save|pigmap [world [full]]}"
  243. exit 1
  244. ;;
  245. esac
  246.  
  247. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement