Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # /etc/init.d/minecraft
- # Misc settings
- SERVICE='craftbukkit-0.0.1-SNAPSHOT.jar'
- USERNAME="minecraft"
- MCPATH='/home/minecraft/bukkit'
- 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"
- STAMP=`date +%Y%m%d_%H%M`
- # Backup
- BACKUP_PATH='/home/minecraft/bukkit/backups'
- WORLDS='world1;Anvil' # semicolon-separated list of worlds to backup
- OLDBACKUPS=10 # in days
- SQLDB='bukkit'
- SQLPASS="derptastic"
- # pigmap
- PIGMAP_PATH='/home/minecraft/bukkit/pigmap'
- PIGMAP_OUTPUT_PATH='/var/www/map'
- PIGMAP_WORLDS='world1;Anvil'
- # Don't change anything beyond this point
- ME=`whoami`
- as_user() {
- START=$(date +%s.%N)
- if [ $ME == $USERNAME ] ; then
- bash -c "$1"
- else
- su $USERNAME -c "$1"
- fi
- END=$(date +%s.%N)
- DIFF=$(echo "$END - $START" | bc | awk '{printf("%.3f",$1 + 0.0005)}')
- }
- as_root() {
- if [[ $EUID -ne 0 ]] ; then
- echo "Command must be run as root"
- else
- bash -c "$1"
- fi
- }
- is_running() {
- if ! ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null; then
- return 1
- fi
- }
- cmd_as_user() {
- if is_running; then
- as_user "screen -p 0 -S minecraft -X eval 'stuff \"$1\"\015'"
- fi
- }
- mc_start() {
- if is_running; then
- echo "Tried to start but $SERVICE was already running!"
- else
- echo "$SERVICE was not running... starting."
- cd $MCPATH
- as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION"
- sleep 7
- if is_running; then
- echo "$SERVICE is now running."
- else
- echo "Could not start $SERVICE."
- fi
- fi
- }
- mc_saveoff() {
- if is_running; then
- echo "$SERVICE is running... suspending saves"
- cmd_as_user "save-off"
- cmd_as_user "save-all"
- sync
- sleep 10
- else
- echo "$SERVICE was not running. Not suspending saves."
- fi
- }
- mc_saveon() {
- if is_running; then
- echo "$SERVICE is running... re-enabling saves"
- cmd_as_user "save-on"
- else
- echo "$SERVICE was not running. Not resuming saves."
- fi
- }
- mc_stop() {
- if is_running; then
- echo "$SERVICE is running... stopping."
- cmd_as_user "say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map..."
- cmd_as_user "save-all"
- sleep 10
- cmd_as_user "stop"
- sleep 15
- else
- echo "$SERVICE was not running."
- fi
- if is_running; then
- echo "$SERVICE could not be shut down... still running."
- else
- echo "$SERVICE is shut down."
- if [ ! -d "$MCPATH/logs" ]; then
- as_user "mkdir $MCPATH/logs"
- fi
- as_user "mv $MCPATH/server.log $MCPATH/logs/server-$STAMP.log"
- fi
- }
- mc_dosave() {
- if is_running; then
- echo "$SERVICE is running... doing save-all"
- cmd_as_user "save-all"
- #cmd_as_user "say Saved worlds! Took ${DIFF}s"
- sync
- sleep 10
- else
- echo "$SERVICE was not running. Not doing save-all."
- fi
- }
- mc_backup() {
- cd $BACKUP_PATH
- WORLDS=$(echo $WORLDS | tr ";" "\n")
- cmd_as_user "say Server backup starting in 5s, going readonly..."
- sleep 5
- echo "Dumping SQL database '$SQLDB'"
- as_root "nice -n 19 mysqldump -u root -h localhost -p$SQLPASS $SQLDB > $SQLDB-db.sql"
- mc_saveoff
- for WORLD in $WORLDS; do
- echo "Backing up world '$WORLD'"
- as_user "nice -n 19 rsync -a --delete $MCPATH/$WORLD/ $WORLD"
- done
- mc_saveon
- if [ "$1" == "no-snap" ]; then
- echo "Skipping snapshot creation"
- else
- echo "Deleting snapshots older than $OLDBACKUPS days"
- as_user "find snapshot-* -type d -mtime +$OLDBACKUPS | xargs rm -rf"
- echo "Creating snapshot $STAMP"
- FINALDIR="snapshot-$STAMP"
- as_user "mkdir $FINALDIR"
- for WORLD in $WORLDS; do
- as_user "nice -n 19 tar -czf $FINALDIR/$WORLD.tar.gz $WORLD"
- done
- as_user "nice -n 19 rsync -a --delete $MCPATH/plugins/ plugins && tar -czf $FINALDIR/plugins.tar.gz plugins"
- as_user "nice -n 19 gzip -c $SQLDB-db.sql > $FINALDIR/$SQLDB-db.sql.gz"
- echo "Backup complete"
- fi
- cmd_as_user "say Server backup ended, going read-write"
- }
- mc_pigmap() {
- cd $PIGMAP_PATH
- PIGMAP_WORLDS=$(echo $PIGMAP_WORLDS | tr ";" "\n")
- if [ "$1" ]; then
- if ! (for i in ${PIGMAP_WORLDS[@]}; do if [[ $i == ${1} ]]; then return 0; fi; done; return 1); then
- echo "Invalid world"
- exit 1
- fi
- PIGMAP_WORLDS=($1)
- fi
- mc_saveoff
- for WORLD in $PIGMAP_WORLDS; do
- as_user "nice -n 19 rsync -va $MCPATH/$WORLD/ $WORLD > $WORLD.regionlist"
- done
- mc_saveon
- if [ "$2" == "full" ]; then
- echo "Rendering full world map '$1'"
- cmd_as_user "say Rendering full world map '$1'..."
- as_user "nice -n 19 ./pigmap -B 6 -T 1 -i $1 -o $PIGMAP_OUTPUT_PATH/$1 -h 1"
- TOTAL=$DIFF
- as_user "mv $PIGMAP_OUTPUT_PATH/$1/pigmap-default.html $PIGMAP_OUTPUT_PATH/$1/index.html"
- else
- cmd_as_user "say Rendering world maps..."
- TOTAL=0
- for WORLD in $PIGMAP_WORLDS; do
- echo "Rendering world map '$WORLD'"
- as_user "nice -n 19 ./pigmap -i $WORLD -o $PIGMAP_OUTPUT_PATH/$WORLD -r $WORLD.regionlist -h 1 -x"
- TOTAL=$(echo "$TOTAL + $DIFF" | bc)
- as_user "mv $PIGMAP_OUTPUT_PATH/$WORLD/pigmap-default.html $PIGMAP_OUTPUT_PATH/$WORLD/index.html"
- done
- fi
- cmd_as_user "say Render completed in ${TOTAL}s. Visit crazycraft.org/map"
- }
- mc_status() {
- if is_running; then
- echo "$SERVICE is running."
- else
- echo "$SERVICE is not running."
- fi
- }
- # Commands here
- case "$1" in
- start)
- mc_start
- ;;
- stop)
- mc_stop
- ;;
- restart)
- mc_stop
- mc_start
- ;;
- save)
- mc_dosave
- ;;
- backup)
- mc_backup $2
- ;;
- status)
- mc_status
- ;;
- pigmap)
- mc_pigmap $2 $3
- ;;
- *)
- echo "Usage: /etc/init.d/minecraft {start|stop|update|backup [no-snap]|status|restart|save|pigmap [world [full]]}"
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement