简介-邱月涛
公司新项目上线,今天建立全网备份,现在这记录的是mysql部分
备份方式:全备+binlog
备份地点:本地+异地
备份时间:每日0点,切割binlog日志
1,环境介绍:
[root@jm1j-node1 scripts]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@jm1j-node1 scripts]# mysql -V
mysql Ver 14.14 Distrib 5.6.35, for linux-glibc2.5 (x86_64) using EditLine wrapper
2,规范脚本存放目录
mkdir -p /server/scripts
3,编写脚本内容如下
#!/bin/bash
#---------------------------------------------------------
# $Name: mysqlbackup.sh
# $Version: v1.0
# $Author: qiuyuetao
# $organization: https://blog.51cto.com/qiuyt
# $Create Date: 2017-12-13
# $Description: Mysql backup script
#---------------------------------------------------------
#source /etc/init.d/functions
DAY=`date +%Y-%m-%d\-%H-%M`
BACK_DIR="/data/backup/mysql/dump"
DBUSER="root" #数据用户
DBPASSWD="***数据库密码"
BIN_PATH="/application/mysql/bin" #全局变量,可以使用which获取
LOG="/var/log/mysqlback.log" #备份日志
CKLOG="/var/log/bkmd5-"$DAY".log" #md5加密日志,后期发送邮件
## Close all tables and refresh log ####
"$BIN_PATH"/mysql -u "$DBUSER" -p"$DBPASSWD" -e "flush logs"
#"$BIN_PATH"/mysql -u "$DBUSER" -p"$DBPASSWD" -e "flush tables with read lock"
echo "mysqldump start `date +%F\ %H\:%M\:%S`" >> "$LOG"
## Each backup database to the target location ####
for i in `"$BIN_PATH"/mysql -u "$DBUSER" -p"$DBPASSWD" -e "show databases" |grep -vE "Database|information_schema|performance_schema"`
do
if [ ! -d "$BACK_DIR"/"$i" ]; then
mkdir -p "$BACK_DIR"/"$i"
fi
"$BIN_PATH"/mysqldump -u "$DBUSER" -p"$DBPASSWD" --default-character-set=utf8 --events --master-data=2 --lock-all-tables -f --log-error="$LOG" -B "$i"|gzip > "$BACK_DIR"/"$i"/"$DAY".sql.gz
md5sum "$BACK_DIR"/"$i"/"$(date +%F --date='0 days ago')"-*.sql.gz >>$CKLOG
done
echo "mysqldump stop `date +%F\ %H\:%M\:%S`" >> "$LOG"
echo "" >>"$LOG"
## Delete old 10day backup files 保留最近10天的数据####
for RM in `"$BIN_PATH"/mysql -u "$DBUSER" -p"$DBPASSWD" -e "show databases" |grep -vE "Database|information_schema|performance_schema"`
do
rm -rf "$BACK_DIR"/"$RM"/$(date +%F --date='10 days ago')*.sql.gz
done
echo 生产数据库备份完整性MD5初始值 |mail -s 备份校验 -a "$CKLOG" qiuyt@*****.com ##自己的邮箱
4,添加执行权限
[root@jm1j-node1 scripts]# chmod +x mysqlbackup.sh `
5,在命令行手动测试
查看备份结果,
#如果没有发送邮件,请检查服务是否安装,启动,25端口是否启用等等,然后手动发送邮件测试
6,配置邮箱
1)查看是否安装mailx
[root@jm1j-node1 scripts]# rpm -qa|grep mailx
mailx-12.4-8.el6_6.x86_64
2)修改mail配置
[root@yilonghc-m01 ~]# tail /etc/mail.rc ##t添加如下内容
set bsdcompat
set from=outlokk@163.com
set smtp=smtp.163.com
set smtp-auth-user=*****@163.com #自己的邮箱
set smtp-auth-password=***** ##授权码
set smtp-auth=login
6,定时备份 (正常可以加入crontab定时任务,每天0点执行)
上面没有找到,下面命令安装
yum install -y vixie-cron
添加定时任务
crontab -e
#mysqlbackup by qyt at 2017-12-13
00 00 * * * /bin/sh /server/scripts/mysqlbackup.sh >/dev/null 2>&1
##异地备份,有多重方式,为了减少服务器压力,我使用rsync 客户端拉取方式
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。