HTS备份脚本

#!/bin/bash
#program:用于升级前后备份;看时间戳
#author:sundz
#version:v1 20220610 创建脚本 备份mysql以及aboss程序
#version:v2 20220720 判断mysql进程是否存在?备份mysql
#version:v3 20220805 适配HTS_chk_ver.sh 改了log文件名为backup.log
#version:v4 20221129 log_msg/err_msg/log_chk函数 打印颜色 日志检查

log_msg()
{
	time=`date +%Y-%m-%d' '%H:%M:%S.%N | cut -b 1-23`
	echo "$time [LOG] $1$2"
}
err_msg()
{
	time=`date +%Y-%m-%d' '%H:%M:%S.%N | cut -b 1-23`
	echo -e "\033[41;33m$time [ERROR] $1$2 \033[0m"
}
log_chk()
{
	echo "日志检查输出..."
	more ${log_file} | fgrep "ERROR" > ${err_file}
	[ -s ${err_file} ] && more ${err_file} || echo "日志无报错,备份结束"
}
#定义文件夹和日志文件
date=$(date +%Y%m%d)
log_time=$(date +%H:%M:%S)
backup_floder=/home/aboss/backup/${date}
file_time=$(date +%Y%m%d%H%M%S)
#mysql_file=mysql_bak_$(date "+%Y_%m_%d")
log_file=/home/aboss/backup/${date}/backup.log
err_file=/home/aboss/backup/${date}/backup.err
#检测当天文件夹是否存在
[ ! -d ${backup_floder} ] && mkdir -p ${backup_floder}
#检测当天日志文件是否存在
[ ! -e ${log_file} ] && touch ${log_file}
#检测mysql进程是否存在
mysql_chk=`ps x | grep 3306 | grep -v 'grep' | wc -l`
log_msg "excuting...."
if [ "${mysql_chk}" == 1 ]; then
	log_msg "check mysql is running" >> ${log_file}
	#备份mysql文件
	log_msg " begin mysqldump" >> ${log_file}
	cd /home/aboss/mysql/bin
	#下一行屏幕不打印的话 加2>>${log_file}
	./mysqldump -uroot -pPASSWORD -h127.0.0.1 -P3306 --all-databases --flush-privileges --single-transaction --master-data=2 --flush-logs --triggers --routines --events --default-character-set=gbk --hex-blob > all_A_${file_time}.sql
	[ "$?" == 0 ] && log_msg "mysqldump is done " >> ${log_file} || err_msg "mysqldump is wrong " >> ${log_file}
	[ "$?" == 0 ] && log_msg "mysqldump is done " || err_msg "mysqldump is wrong " 
	#压缩文件
	log_msg "begin tar" >> ${log_file}
	tar zcvf all_A_${file_time}.sql.tgz all_A_${file_time}.sql >> ${log_file} 2>&1
	log_msg "over tar" >> ${log_file}
	mv all_A_${file_time}.sql.tgz ${backup_floder} 2>>${log_file} 
	rm all_A_${file_time}.sql 2>>${log_file}
else 
	err_msg "check mysql is not running" >> ${log_file}
	log_chk
fi

#开始备份aboss程序
cd /home/aboss/aboss5
log_msg "excuting aboss5 backup..."
log_msg "begin tar aboss5" >> ${log_file}
tar zcvf aboss5_${file_time}.tgz --exclude=bin/*simexchange* --exclude=bin/core.* bin svc cfg trancom >> ${log_file} 2>&1
log_msg "over tar aboss5" >> ${log_file}
log_msg "begin move aboss5" >> ${log_file}
mv aboss5_${file_time}.tgz ${backup_floder} 2>>${log_file} 
log_msg "over move aboss5" >> ${log_file}
log_msg "over"
#日志检查
log_chk
posted @ 2022-08-05 10:05  孙犯困  阅读(58)  评论(0编辑  收藏  举报