#!/bin/sh
#set -x
RUNDIR="`dirname $0`"
cd $RUNDIR
RUNDIR=`pwd`
. ${RUNDIR}/backupdb_t.cfg
########################################################################
# Global Settings;
########################################################################
# 日志文件切换值,单位:B;
LogFileSize=10240000
# 日志目录;
LOGPATH="$RUNDIR/log"
# 日志文件名;
LOGFILE="${LOGPATH}/backupdb_t.log"
# 日志文件备份名称;
LOGFILESOLD="${LOGPATH}/backupdb_t.log.bak"
# 进程ID,用于标识临时文件,防止多个任务时删除同名临时文件;
UniqPID=$$
########################################################################
# Common Functions;
########################################################################
################################################################
###使用帮助
################################################################
Usage()
{
echo "Usage:"
echo "==============================================================================================="
echo "db_backup_t.sh dbname"
echo "dbname: The database name of backup database"
echo "rootpassword : The password of mysql super user root"
echo "================================================================================================"
exit 0
}
####################################################################
# 函数:write_log函数
# 目的:写日志
# 输入:日志信息
####################################################################
write_log()
{
NowTime="`date '+%Y-%m-%d %H:%M:%S'`"
echo -e "*[$NowTime]: $@" |tee -a "$LOGFILE"
}
########################################################################
# Initialize;
########################################################################
###创建日志目录###
if [ ! -d ${LOGPATH} ];then
mkdir -p ${LOGPATH}
fi
# 检查日志文件大小;
if [ -f $LOGFILE ];then
logsize=`ls -l $LOGFILE|awk '{print $5}'`
if [ $logsize -gt ${LogFileSize} ];then
mv $LOGFILE $LOGFILESOLD
fi
fi
touch $LOGFILE
####输入参数检查#####
if [ $# -eq 1 ];then
dbname=`echo $1`
BACKUPDB="${dbname}"
rootpswd="$MYSQLROOTPSWD"
elif [ $# -eq 2 ];then
dbname=`echo $1`
BACKUPDB="${dbname}"
rootpswd=`echo $2`
else
write_log "[Error],wrong parameters $*, please input the correct parameters."
Usage
fi
######配置数据库备份数据存放的路径######
####默认情况下存放到/opt/DB_BACKUP目录下#####
if [ "x"${DBBACKDEFDST} = "x" ];then
DBBACKDEFDST="/opt/MySQL_DB_Backup"
fi
##mysql database variables
##是否存在mysql用户
IFMYSQLUSER=`cat /etc/passwd|grep -c -i "^${MYSQLUSER}"`
if [ $IFMYSQLUSER -gt 0 ];then
MYSQLUSEREXIST="yes"
else
#否则不存在mysql用户
MYSQLUSEREXIST="no"
echo "[Warnning], The server do not install any mysql database,even the mysql user does not exist."
exit 100
fi
########################################################################
# Functions;
########################################################################
###获取当前主机的主机名称###
HOSTNAME=`hostname`
##########################################################################################
# 函数: check_kp_dbnode
# 功能: 判断当前主机是否为主节点
# 输入:
# 输出:0,1--0运行在当前主机上,1没有运行在当前主机上
##########################################################################################
check_kp_dbnode()
{
kpCfgFile="/etc/keepalived/keepalived.conf"
kpInitScript="/etc/init.d/keepalived.init"
if [ ! -f ${kpCfgFile} ];then
##不存在keepalived软件,非双机模式,返回成功,进行备份
return 0
else
if [ -f ${kpInitScript} ];then
runFlag=`${kpInitScript} status|grep "running"|wc -l`
if [ ${runFlag} -lt 1 ];then
##keepalived未正常运行,非双机模式或者双机异常停止,返回成功,进行备份
return 0
else
vritualIP=`cat ${kpCfgFile} |grep "virtual_server"|awk '{print $2}'`
ip address|grep -q "inet $vritualIP"
retCode=$?
if [ ${retCode} -eq 0 ];then
return 0
else
return 1
fi
fi
else
##不存在keepalived启动脚本,非双机模式,返回成功,进行备份
return 0
fi
fi
}
##########################################################################################
# 函数: check_mysqlsvr_status
# 功能: 检查数据库实例是否打开
# 输入:
# 输出: 是则返回0,否则返回1
##########################################################################################
check_mysqlsvr_status()
{
if [ -x /etc/init.d/mysql ];then
/etc/init.d/mysql status >/dev/null 2>&1
if [ $? -ne 0 ];then
write_log "[Error],the mysql instance is not running, backup database failed."
return 200
else
##检查mysql实例运行状态,uptime为实例运行时长##
checkStatusSql="show status like '%Uptime%';"
runtimes=`echo "${checkStatusSql}" | ${MYSQLBIN} -u root -p${rootpswd} -S /mysql/mysql.sock 2>/dev/null | grep -iw "Uptime" | awk '{print $2}'`
if [ x"${runtimes}" != "x" ];then
if [ ${runtimes} -gt 0 ];then
write_log "[Info],the mysql instance is runing normally."
return 0
else
write_log "[Error],the mysql instance is not running, get the status Uptime failed."
return 200
fi
else
write_log "[Error],the mysql instance is not running, get the status Uptime failed."
return 200
fi
fi
else
write_log "[Error],the mysql database not installed."
return 200
fi
}
###########################################################################################
## 函数: checkdbstaus
## 功能: 检查要备份的数据库是否正常创建运行
## 输入: 备份的数据库名称
## 输出:
###########################################################################################
checkdbstaus()
{
DBNAME="$1"
checkDBStatusSql="select 'dbisrunning' as status;"
echo "${checkDBStatusSql}" | ${MYSQLBIN} -u root -p${rootpswd} -S /mysql/mysql.sock ${DBNAME} 2>/dev/null | grep -iwq "dbisrunning"
retCode=$?
if [ ${retCode} -eq 0 ];then
write_log "[Info],the mysql database ${DBNAME} is runing normally."
return 0
else
write_log "[Error],the mysql database ${DBNAME} not exist or not runing."
return 200
fi
}
##检查数据库是否双机和当前是否在主机上运行
check_kp_dbnode
retCode=$?
if [ ${retCode} -ne 0 ];then
write_log "[Info],DataBase is running as Dual Mode, and current node is not primary node, no need backup the database."
exit ${retCode}
fi
##检查数据库实例是否运行##
#check_mysqlsvr_status
#retCode=$?
#if [ ${retCode} -ne 0 ];then
# write_log "[Error],check mysql server status failed."
# exit ${retCode}
#fi
##传入要检查的数据库名称##
checkdbstaus ${BACKUPDB}
retCode=$?
if [ ${retCode} -ne 0 ];then
write_log "[Error],check mysql database ${BACKUPDB} status failed."
exit ${retCode}
fi
write_log "==================================================================================================="
##先检查是否已有mysqldump程序在执行,有则需要等待mysqldump完成##
while [ 1 = 1 ];
do
write_log "check exist mysqldump process running or not"
ps auxf --cols 1024|grep -v "grep"|grep -q "mysqldump"
retCode=$?
if [ ${retCode} -ne 0 ];then
write_log "[OK] not find any mysqldump process.."
break;
else
write_log "mysqldump process is running, wait for 60s......"
sleep 60
fi
done
write_log "==================================================================================================="
write_log "==================================================================================================="
write_log "[Info],new database backup process starting...."
########�
backup.tar.gz
需积分: 5 132 浏览量
2022-11-26
19:01:31
上传
评论
收藏 122KB GZ 举报
![avatar](https://profile-avatar.csdnimg.cn/e787a8026eda469b80202d3af6d0df53_qq_67657776.jpg!1)
小可爱不在家54
- 粉丝: 0
- 资源: 1
最新资源
- 前端面试题之Html相关题集.zip
- Python智能推荐系统案例介绍: 基于用户行为数据和机器学习算法,设计一个个性化推荐系统,如电影推荐、音乐推荐等
- Animation engine for explanatory math videos 用于解释数学视频的动画引擎
- Python自然语言处理应用案例介绍: 开发一个能够进行文本情感分析、语义理解或机器翻译等任务的自然语言处理应用
- Python基于深度学习的图像识别系统案例介绍: 利用深度学习技术,设计一个能够识别物体、人脸或文字等图像内容的系统
- 海信智能电视刷机数据 LED43K300U(0000) 生产用软件数据 务必确认机编一致 强制刷机 整机USB升级程序
- zookeeper-api基础.docx
- matplotlib是一个用于绘制图表和可视化数据的 Python 库.docx
- comfyui 提示词 参考;来源GitHub
- 一款零配置、无骨架、极小化的Hyperf发行版,通过Nano可以让您仅仅通过1个PHP文件即可快速搭建一个Hyperf应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)