mysql数据库全量备份方案

2022-03-11 09:23:20 浏览数 (1)

全量备份脚本dbback.sh

代码语言:javascript复制
#!/bin/bash
##备份数据库脚本
##email: 1940728253@qq.com
##xwzy1130
#MySQL User Information
HOST=127.0.0.1
USERNAME=root
PASSWORD=*****
#Date Format
DATE=`date  %Y%m%d`
#Back directory
DAYS=20
#备份文件存放位置
BACKUP_DIR=/data/db-backup/
#MySQL directory
MYSQL_DIR=/usr/bin/
#Go to the backup directory
if [ -d ${BACKUP_DIR} ];then
cd ${BACKUP_DIR}
else
mkdir -p ${BACKUP_DIR}
fi
#The first instance of the backup
while read dbname
do
if [ -f ${dbname}_${DATE}.gz ]; then
echo "MySQL Database ${dbname}_${DATE}.gz already exists."
else
${MYSQL_DIR}/mysqldump -u${USERNAME} -p${PASSWORD} -h${HOST} --default-character-set=utf8 --master-data=2 --single-transaction  ${dbname} | /bin/gzip > ${BACKUP_DIR}/${dbname}_${DATE}.gz
fi
done < /opt/ly-traffic/db-backup/dbs.txt


##删除旧备份文件
find  ${BACKUP_DIR} -name "*.gz" -type f -mtime  22|tee -a ${BACKUP_DIR}/del.log|xargs rm -r 2>>${BACKUP_DIR}/err.log


exit 0

本文共 28 个字数,平均阅读时长 ≈ 1分钟

0 人点赞