#!/bin/bash #set script variables BACKUP_DIR="/tmp" #path to back up DB's to, make sure there is enough diskspace MYSQL_ROOT_USER="root" #user with read privleges to all DB's in MySQL MYSQL_ROOT_PASSWORD="" #password for the MySQL user specified for $MYSQL_ROOT_USER MYSQL_BIN_PATH="/usr/local/groundwork/mysql/bin" #where to find MySQL binaries MYSQL_DATA_PATH="/usr/local/groundwork/mysql/data" #where to find MySQL dbs MYSQL_INIT_SCRIPT="/usr/local/groundwork/mysql/scripts/ctl.sh" GROUNDWORK_STOP_CMD="/etc/init.d/groundwork stop" GROUNDWORK_START_CMD="/etc/init.d/groundwork start" CLEANUP=0 PROMPT_FOR_PASSWORD=0 use_legacy_vars() { MYSQL_BIN_PATH="/usr/bin" MYSQL_DATA_PATH="/var/lib/mysql" MYSQL_INIT_SCRIPT="/etc/init.d/mysql" HTTP_INIT_SCRIPT="/etc/init.d/httpd" NAGIOS_INIT_SCRIPT="/etc/init.d/nagios" SNMPTRAPD_INIT_SCRIPT="/etc/init.d/snmptrapd" GWSERVICE_INIT_SCRIPT="/etc/init.d/gwservices" GROUNDWORK_STOP_CMD="${HTTP_INIT_SCRIPT} stop; ${NAGIOS_INIT_SCRIPT} stop; ${SNMPTRAPD_INIT_SCRIPT} stop; ${GWSERVICE_INIT_SCRIPT} stop; ${MYSQL_INIT_SCRIPT} stop;" GROUNDWORK_START_CMD="${MYSQL_INIT_SCRIPT} start; ${NAGIOS_INIT_SCRIPT} start; ${SNMPTRAPD_INIT_SCRIPT} start; ${GWSERVICE_INIT_SCRIPT} start; ${HTTP_INIT_SCRIPT} start;" } usage() { cat << EOF usage: $0 options This script works with GroundWork Monitor. It dumps the dbs, stops mysql, removes the ib* files and associated DB files, restarts mysql, and reloads the db dump. OPTIONS: -h Show this message -c Cleanup. Delete db dump when finished -b {path} Set backup directory, make sure there is enough diskspace default=/tmp -l Force legacy variable. Use this flag if GroundWork is older than version 5.3 and the script doesn't detect this. -u {user} Set the mysql user, default=root -p {password} Set the mysql password, default="" -a Prompt for mysql password, overrides '-p' setting EOF } #if Info.txt not exists, load legacy vars if [ ! -e /usr/local/groundwork/Info.txt ]; then echo -e "\n****\n" echo -e "Pre 5.3 version of Groundwork detected" echo -e "AUTO USING LEGACY VARIABLES" echo -e "\n****\n" use_legacy_vars fi #parse command line options while getopts "hacb:lu:p:" ARG do case $ARG in h) usage exit 1 ;; a) PROMPT_FOR_PASSWORD=1 ;; c) CLEANUP=1 ;; b) BACKUP_DIR=$OPTARG ;; #legacy flag, force use with GroundWork versions prior to 5.3 l) echo -e "\n****\n" echo -e "FORCING USE OF LEGACY VARIABLES" echo -e "\n****\n" use_legacy_vars ;; u) MYSQL_ROOT_USER=$OPTARG ;; p) MYSQL_ROOT_PASSWORD=$OPTARG ;; esac done #set binary paths for mysql & mysqldump MYSQL_BIN="${MYSQL_BIN_PATH}/mysql" MYSQLDUMP_BIN="${MYSQL_BIN_PATH}/mysqldump" MYSQL_START_CMD="${MYSQL_INIT_SCRIPT} start" MYSQL_STOP_CMD="${MYSQL_INIT_SCRIPT} stop" #make sure we can run this script USERNAME=`whoami` if [[ $USERNAME != "root" ]]; then echo -e "ERROR: script must be run as root\n" exit 3 fi #make sure backup dir exists if [ ! -d $BACKUP_DIR ]; then echo -e "ERROR: DB Backup Dir '${BACKUP_DIR}' does not exist.\n" usage exit 1 fi BACKUP_FILE=${BACKUP_DIR}/db-backup-`date +"%F"`.sql #make sure we are trying to run the correct tools, if not "-l" might need to be set or unset if [[ ! -x $MYSQL_BIN || ! -x $MYSQLDUMP_BIN || ! -x $MYSQL_INIT_SCRIPT ]]; then echo "ERROR: Binary paths not set properly. Consider setting or unsetting '-l'\n" usage exit 1 fi #Build Credential string for MySQL if [ "x${MYSQL_ROOT_USER}" != "x" ]; then MYSQL_ROOT_USER="-u${MYSQL_ROOT_USER}" fi if [ "x${MYSQL_ROOT_PASSWORD}" != "x" ]; then MYSQL_ROOT_PASSWORD="-p${MYSQL_ROOT_PASSWORD}" fi if [ $PROMPT_FOR_PASSWORD -eq 1 ]; then MYSQL_ROOT_PASSWORD="-p" fi MYSQL_CREDENTIALS="${MYSQL_ROOT_USER} ${MYSQL_ROOT_PASSWORD}" #DB's to reset DB=`echo "show databases" | ${MYSQL_BIN} ${MYSQL_CREDENTIALS} | grep -v Database | grep -v "lost+found" | grep -v mysql | grep -v "information_schema" | grep -v test` if [ $? -ne 0 ]; then echo "ERROR: cannot connect to mysql. aborting." exit 2 fi #only have mysql running eval $GROUNDWORK_STOP_CMD eval $MYSQL_START_CMD echo "waiting for mysql to finish starting..." sleep 20 #dump DB's to $BACKUP_DIR echo "backing up dbs to ${BACKUP_FILE}" ${MYSQLDUMP_BIN} ${MYSQL_CREDENTIALS} --databases ${DB}> ${BACKUP_FILE} if [[ $? -ne 0 || ! -s ${BACKUP_FILE} ]]; then echo -e "ERROR: databases not successfully backed up. aborting." exit 2 fi #remove innodb files from filesystem eval $MYSQL_STOP_CMD while [ `pgrep mysql | wc -l` -gt 0 ]; do eval $MYSQL_STOP_CMD sleep 10 done echo "cleaning up filesystem" rm -f ${MYSQL_DATA_PATH}/ib* for i in ${DB}; do rm -Rf ${MYSQL_DATA_PATH}/${i}; done #restore DB's from backup eval $MYSQL_START_CMD echo "waiting for mysql to finish starting..." sleep 60 echo "restoring db's from backup" ${MYSQL_BIN} ${MYSQL_CREDENTIALS} < ${BACKUP_FILE} if [ $? -ne 0 ]; then echo "MySQL didn't start properly restarting and trying again" eval $MYSQL_STOP_CMD eval $MYSQL_START_CMD sleep 60 ${MYSQL_BIN} ${MYSQL_CREDENTIALS} < ${BACKUP_FILE} if [ $? -ne 0 ]; then echo "ERROR: databases not successfully restored. db backup can be found at ${BACKUP_FILE}." echo " Please restart mysql, restore the backup, and start groundwork by running the" echo " following commands:" echo " " echo " $MYSQL_START_CMD" echo " ${MYSQL_BIN} ${MYSQL_CREDENTIALS} < ${BACKUP_FILE}" echo " $GROUNDWORK_START_CMD" exit 2 fi fi #start groundwork again eval $GROUNDWORK_START_CMD #cleanup db backup if flag set if [ $CLEANUP -eq 1 ]; then rm -f ${BACKUP_FILE} fi echo -e "\n****\n" echo "Reload completed successfully. You may begin using Groundwork again." echo -e "\n****\n" exit 0