时间:2021-07-01 10:21:17 帮助过:28人阅读
#!/bin/bash
#做大备份数量
Backup_Max_Files=20
#数据库备份目录
WORKING_DIR=$(dirname "$0")
export WORKING_DIR=$(cd -P "$WORKING_DIR"/ > /dev/null; pwd)
#数据库IP地址、帐号、密码
DB_Host="XXXXXXX"
DB_User="XXXXXX"
DB_Password="XXXXXXX"
#需要备份的数据库
Databases=(
"mysql"
"zabbix"
)
#==========
echo "###########Bash Shell MySql Databases Backup Tools!"
#定义变量=当前日期
Date_Time=$(date +%F)
#定义备份文件前缀
BackData_File=zhang"_${Date_Time}"
cd $WORKING_DIR
mkdir $BackData_File
#统计需要备份数据库的个数
count=0
while [ "x${Databases[count]}" != "x" ]
do
count=$((count + 1))
echo "Databases=$count"
done
echo "[+] ${count} The database is about to be backed up"
for Databases in ${Databases[@]}
do
echo "[+] MySql-backing up: ${Databases}"
echo -n " MySql Backup Start: "
echo $(date +"%Y-%m-%d %H:%M:%S")
#使用Mysqdump工具进行逻辑备份(全备)并使用邮件脚本发送成功失败提醒
if $(mysqldump -h${DB_Host} -u${DB_User} -p${DB_Password} --single-transaction --flush-logs mysql > ${BackData_File}/${Databases}.sql )
then
echo " Success OK!: Mysql database backup success!"
# python /root/jiaoben/mail.py "y@163.com" "${Databases}数据库备份成功" "${Databases}>数据库备份成功"
else
echo " Error NO!: Failure of MySQL database backup!"
#python /root/jiaoben/mail.py "y@163.com" "${Databases}数据库备份失败" "${Databases}>数据库备份失败请查看"
fi
echo -n "Backup success: "
echo $(date +"%Y-%m-%d %H:%M:%S")
done
echo
echo "[+] Packaging and compressing folders with Tar ...."
#使用tar命令打包并压缩生成的数据库文件
cd $WORKING_DIR
tar -cv ${BackData_File} | bzip2 > ${BackData_File}.tar.bz2 && rm -rf ${BackData_File}
mysql备份脚本
标签:就是 zab zha name .sql mysql备份 变量 start 修改