日期:2014-05-16 浏览次数:20995 次
#!/bin/sh # set -x ## this script is for auto mysql backup ## the backup files will be keeped for 10 days ## backup dir: /opt/alu/data/backup/mysql/ ## log file: /opt/alu/logs/3rd_party/mysql/backup.log DB_NAME=test MYSQL_USER=nbiopsuser MYSQL_PWD=nbiopsuser BACKUP_PATH=/opt/alu/data/backup/mysql LOG_FILE=/opt/alu/logs/3rd_party/mysql/backup.log DUMP_FILE=`date +"%Y%m%d"`.sql TGZ_FILE=`date +"%Y%m%d"`.tgz SHELL_DIR=/opt/alu/shell/sysmgt BAK_DAY=7 echo >> ${LOG_FILE} echo "-------------Backup-------------" >> ${LOG_FILE} echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOG_FILE} echo "-------------Backup-------------" >> ${LOG_FILE} echo >> ${LOG_FILE} ## check mysql pid, and kill it checkProcess(){ PIDS=`ps -ef|grep mysqld|grep -v grep|grep 3306|awk '{print $2}'` if [ -n ${PIDS} ]; then for pid in ${PIDS} do kill -9 ${pid} done fi } ## check mysql service, make sure it's alive `mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} ping >>/dev/null 2>&1` if [ $? != 0 ]; then checkProcess echo "mysql is not alive,will be start now!" >> ${LOG_FILE} ${SHELL_DIR}/mysql_supervise.sh start >> /dev/null 2>&1 fi # mysql not running ok, exit if [ $? != 0 ]; then echo "Mysql error" exit 1 fi ## delete old files find ${BACKUP_PATH} -mtime +${BAK_DAY} | xargs rm -rf >> /dev/null 2>&1 ## tgz file today exists,delete it if [ -f ${BACKUP_PATH}/${TGZ_FILE} ]; then echo "[${BACKUP_PATH}/${TGZ_FILE}] Backup file is exists,will be backup as .bak" >> ${LOG_FILE} cp ${BACKUP_PATH}/${TGZ_FILE} ${BACKUP_PATH}/${TGZ_FILE}".bak" >> ${LOG_FILE} rm -f ${BACKUP_PATH}/${TGZ_FILE} >> /dev/null 2>&1 fi cd ${BACKUP_PATH} mysqldump -u${MYSQL_USER} -p${MYSQL_PWD} --opt ${DB_NAME} > ${DUMP_FILE} tar -czvf ${TGZ_FILE} ${DUMP_FILE} >> ${LOG_FILE} 2>&1 echo "[${TGZ_FILE}] Backup success!" >> ${LOG_FILE} rm -rf ${DUMP_FILE} >> /dev/null 2>&1 echo "Done"
#!/bin/sh #set -x ## it's for mysql restore ## if no file specify,it will use the backup script BACKUP_PATH=/opt/alu/data/backup/mysql LOG_FILE=/opt/alu/logs/3rd_party/mysql/backup.log SHELL_DIR=/opt/alu/shell/sysmgt DB_NAME=test MYSQL_USER=nbiopsuser MYSQL_PWD=nbiopsuser ## make sure if mysql's status is OK check_status(){ `mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} ping >>/dev/null 2>&1` if [ $? != 0 ]; then PIDS=`ps -ef|grep mysqld|grep -v grep|grep 3306|awk '{print $2}'` if [ -n ${PIDS} ]; then for pid in ${PIDS} do kill -9 ${pid} done fi echo "Mysql is not alive,will be start now!" >> ${LOG_FILE} ${SHELL_DIR}/mysql_supervise.sh start >> /dev/null 2>&1 fi } # mysql not running ok, exit if [ $? != 0 ]; then echo "Mysql error" exit 1 fi p_echo(){ echo >> ${LOG_FILE} echo "------------Restore------------" >> ${LOG_FILE} echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOG_FILE} echo "------------Restore------------" >> ${LOG_FILE} echo >> ${LOG_FILE} } ## when no parameter specify,list all