[BATCH] Trinity Control Bash Script

I whipped this up last night, I was pretty bored after reading through as much of the Drupal handbook that my brain could handle. Yea, there are a few bugs here and there, mostly for directory detection, but it works well enough.

The only requirements are screen and git. I’m sure everyone has their own way of doing this, but like I said I was bored and decided to share mine.

Configuration: Fill in the values for the variables at the top of the script, and stop when you hit the do not edit warning.


#!/bin/bash

# TrinityCore Master Control Script

# Author: Kotori

# Requirements: screen, git


#####################################

#### YOU CAN CHANGE THESE VALUES ####

#####################################

# Location of the TrinityCore source code, git will be run inside this directory.

PATH_TO_TC_SRC=/home/trinity_user/TrinityCoreSource


# Location of the TrinityCore build directory, object build files will be placed here.

PATH_TO_TC_SRC_BUILD=/home/trinity_user/TrinityCoreBuild


# Location to install TrinityCore to.

PATH_TO_SERVER=/home/trinity_user/server


# Location of the worldserver and authserver bin files.

PATH_TO_TC_BIN=$PATH_TO_SERVER/bin


# Location of TrinityCore's logfiles.

PATH_TO_TC_LOG=$PATH_TO_SERVER/logs


#### DATABASE STUFF ####

MYSQL_TC_USER='mysqluser!' # TrinityCore MySQL username

MYSQL_TC_PW='mysqlpassword!' # TrinityCore MySQL password


# Location to backup TrinityCore's database files to.

PATH_TO_DB_BACKUP=$PATH_TO_SERVER/sql_backup


WORLD_DB='world' # Name of world database.

REALM_DB='auth' # Name of realmd database.

CHAR_DB='characters' # Name of characters database.


###########################################################

#### EVERYTHING BELOW HERE DOES NOT NEED TO BE EDITED #####

###########################################################

MY_CMD=$1 # Holds the value of whatever the user passed during startup.

DATE_STAMP=`date +%Y-%m-%d` # current date.

STATUS=0 # mysqldump error code return value.

COMPLETE_CMD='[ \E[32;40mCOMPLETE!\033[0m ]' # Color output for success.

FAILED_CMD='[ \E[31;40mFAILED!\033[0m ]' # Color output for failure.


function StartTrinityCoreServer {

  cd $PATH_TO_TC_BIN


  echo -en "Starting Auth Server..."

  screen -dmS AUTH_SERVER ./authserver

  STATUS=${?}

  if [ $STATUS -eq 0 ]; then

        echo -e $COMPLETE_CMD

  else

        echo -e $FAILED_CMD

  fi

  echo -en "Starting World Server..."

  screen -dmS WORLD_SERVER ./worldserver

  STATUS=${?}

  if [ $STATUS -eq 0 ]; then

        echo -e $COMPLETE_CMD

  else

        echo -e $FAILED_CMD

  fi

}


function StopTrinityCoreServer {


  echo -en "Stopping Auth Server..."

  screen -XS AUTH_SERVER quit

  STATUS=${?}

  if [ $STATUS -eq 0 ]; then

	echo -e $COMPLETE_CMD

  else

        echo -e $FAILED_CMD

  fi


  echo -en "Stopping World Server..."

  screen -XS WORLD_SERVER quit

  STATUS=${?}

  if [ $STATUS -eq 0 ]; then

        echo -e $COMPLETE_CMD

  else

        echo -e $FAILED_CMD

  fi

}


function BuildTrinityCoreServer {

  cd $PATH_TO_TC_SRC

  echo

  echo "##########################################################"

  echo -e '\E[32;40mUpdating TrinityCore Source Code.\033[0m'

  echo "##########################################################"

  echo


  git pull -u

  CURRENT_VERSION=`git log --pretty=format:'' | wc -l`


  cd $PATH_TO_TC_SRC_BUILD

  cmake $PATH_TO_TC_SRC -DPREFIX=$PATH_TO_SERVER -DWITH_SQL=1


  echo

  echo "###########################################"

  echo " BUILDING SERVER v.$CURRENT_VERSION"

  echo "###########################################"

  echo


  make


  echo

  echo "###########################################"

  echo " INSTALLING SERVER v.$CURRENT_VERSION"

  echo "###########################################"

  echo


  # Stop the server if its running so we can replace files.

  StopTrinityCoreServer


  cd $PATH_TO_TC_SRC_BUILD

  make install


  # Once compilation and installation is complete, we can startup the server again.

  StartTrinityCoreServer

}


function BackupTrinityCoreDB {

  # BACKUP auth DATABASE.

  echo "Beginning DUMP of Trinity Database"


  mkdir -p $PATH_TO_DB_BACKUP/$DATE_STAMP

  cd $PATH_TO_DB_BACKUP/$DATE_STAMP


  echo -en "Backing up auth database...\t\t"

  mysqldump -u$MYSQL_TC_USER -p$MYSQL_TC_PW $REALM_DB > auth.sql

  STATUS=${?}


  if [ $STATUS -eq 0 ]; then

    echo -e $COMPLETE_CMD


    # BACKUP characters DATABASE.

    echo -en "Backing up characters database...\t"

    mysqldump -u$MYSQL_TC_USER -p$MYSQL_TC_PW $CHAR_DB > characters.sql

    STATUS=${?}


    if [ $STATUS -eq 0 ]; then

      echo -e $COMPLETE_CMD


      # BACKUP world DATABASE.

      echo -en "Backing up world database...\t\t"

      mysqldump -u$MYSQL_TC_USER -p$MYSQL_TC_PW $WORLD_DB > world.sql

      STATUS=${?}


      if [ $STATUS -eq 0 ]; then

              echo -e $COMPLETE_CMD

      else

              echo -e $FAILED_CMD

      fi


    else

            echo -e $FAILED_CMD

    fi

  else

          echo -e $FAILED_CMD

  fi


  if [ $STATUS -eq 0 ]; then

     echo -en 'Compressing SQL files into bz2 files...\t'

      bzip2 -z9qf *.sql

      STATUS=${?}


      if [ $STATUS -eq 0 ]

        then echo -e $COMPLETE_CMD


      else

        echo -e $FAILED_CMD

      fi


    echo -e "Backups can be found in: " $PATH_TO_DB_BACKUP/$DATE_STAMP/

  else

    echo -e 'Backup routines \E[31;40mfailed!\033[0m'

    exit 1

  fi

}


while true

do

  case $MY_CMD in

    backup)

      # Backup the entire database to a specific location.

      BackupTrinityCoreDB



      break

    ;;

    build)

      # Build the latest version from the git repository.

      BuildTrinityCoreServer


      break

    ;;

    start)

      # Call the start script.

      StartTrinityCoreServer


      break

    ;;

    stop)

      # Call the stop script.

      StopTrinityCoreServer


      break

    ;;

    restart)

      # Stop the application

      StopTrinityCoreServer

      echo ""

      # Start the application

      StartTrinityCoreServer


      break

    ;;

    *)

# Error tolerance.

      echo 'Please enter one of the following parameters: '

      echo -e 'build\t Builds the latest version of TrinityCore from the git repository'

      echo -e 'backup\t Creates a backup of the auth, characters, and world databases'

      echo -e 'start\t Starts the Auth and World servers in screen sessions'

      echo -e 'stop\t Stops the Auth and World server screen sessions'

      echo -e 'restart\t Restarts the Auth and World servers'

      echo -n '>'

      read MY_CMD

    ;;

  esac

done

nice thank you