在生产环境中,我们一般采取mysqldump全库备份,但这对于大型数据库是不可用的,因为mysqldump是逻辑备份,备份和恢复的效率缓慢,根据个人经验给出一个临界值的话,500G以下的数据库可以用mysqldump来进行全库备份,500G以上的数据库则需要物理全库备份,甚至是增量备份。Xtrabackup是物理备份,其备份速度也因其物理特性远超逻辑备份的速度,而且Xtrabackup就是为了逻辑备份而生的,接下来演示一下Xtrabackup是如何完成对数据库的全备和增备的。
一:首先我们安装xtrabackup软件
该tar包可自行百度下载。
[[email protected] ~]# tar xvf percona-xtrabackup-2.1.8-733-Linux-x86_64.tar.gz [[email protected] ~]# mkdir -p /usr/local/xtrabackup [[email protected] ~]#cp -rp percona-xtrabackup-2.1.8-Linux-x86_64/* /usr/local/xtrabackup 将上述目录添加到系统环境变量 [[email protected] bin]# echo export PATH=$PATH:/usr/local/xtrabackup/bin >>/etc/profile [[email protected] bin]# source /etc/profile [[email protected] yum.repos.d]# yum install perl-Time-HiRes [[email protected] bin]# yum install perl-DBD-MySQL*
二:创建备份目录
[[email protected] ~]#mkdir -p
/databackup/xtrabackup
[[email protected] ~]#mkdir
-p /databackup/xtrabackuplog
三:数据库授予备份用户相关权限
mysql > grant SELECT,RELOAD,SHOW DATABASES,SUPER,LOCK TABLES,REPLICATION CLIENT,SHOW VIEW,EVENT,FILE on *.* to [email protected]‘localhost‘ identified by ‘MANAGER‘;
四:执行本文的自动化的备份脚本
[[email protected] ~]#vi xtrabackup.sh #backup.sh #!/bin/sh #on xtrabackup 2.1.8 # 第一次执行它的时候它会检查是否有完全备份,否则先创建一个全库备份 # 当你再次运行它的时候,它会根据脚本中的设定来基于之前的全备或增量备份进行增量备份 ################## #author [email protected]# ################## INNOBACKUPEX_PATH=innobackupex #INNOBACKUPEX的命令 INNOBACKUPEXFULL=/usr/local/xtrabackup/bin/$INNOBACKUPEX_PATH #INNOBACKUPEX的命令路径 #mysql目标服务器以及用户名和密码 MYSQL_CMD="--host=localhost --user=backup --password=MANAGER --port=3306" MYSQL_UP=" --user=backup --password=‘MANAGER‘ --port=3306" #mysqladmin的用户名和密码 TMPLOG="/tmp/innobackupex.$$.log" MY_CNF=/etc/my.cnf #mysql的配置文件 MYSQL=/usr/local/mysql/bin/mysql MYSQL_ADMIN=/usr/local/mysql/bin/mysqladmin BACKUP_DIR=/databackup/xtrabackup # 备份的主目录 FULLBACKUP_DIR=$BACKUP_DIR/full # 全库备份的目录 INCRBACKUP_DIR=$BACKUP_DIR/incre # 增量备份的目录 FULLBACKUP_INTERVAL=82800 # 全库备份的间隔周期,时间:秒 KEEP_FULLBACKUP=3 # 至少保留几个全库备份 logfiledate=backup.`date +%Y%m%d%H%M`.txt #开始时间 STARTED_TIME=`date +%s` ############################################################################# # 显示错误并退出 ############################################################################# error() { echo "$1" 1>&2 exit 1 } # 检查执行环境 if [ ! -x $INNOBACKUPEXFULL ]; then error "$INNOBACKUPEXFULL未安装或未链接到/usr/bin." fi if [ ! -d $BACKUP_DIR ]; then error "备份目标文件夹:$BACKUP_DIR不存在." fi mysql_status=`netstat -nl | awk ‘NR>2{if ($4 ~ /.*:3306/) {print "Yes";exit 0}}‘` if [ "$mysql_status" != "Yes" ];then error "MySQL 没有启动运行." fi if ! `echo ‘exit‘ | $MYSQL -s $MYSQL_CMD` ; then error "提供的数据库用户名或密码不正确!" fi # 备份的头部信息 echo "----------------------------" echo echo "$0: MySQL备份脚本" echo "开始于: `date +%F‘ ‘%T‘ ‘%w`" echo #新建全备和差异备份的目录 mkdir -p $FULLBACKUP_DIR mkdir -p $INCRBACKUP_DIR #查找最新的完全备份 LATEST_FULL_BACKUP=`find $FULLBACKUP_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1` # 查找最近修改的最新备份时间 LATEST_FULL_BACKUP_CREATED_TIME=`stat -c %Y $FULLBACKUP_DIR/$LATEST_FULL_BACKUP` #如果全备有效进行增量备份否则执行完全备份 if [ "$LATEST_FULL_BACKUP" -a `expr $LATEST_FULL_BACKUP_CREATED_TIME + $FULLBACKUP_INTERVAL + 5` -ge $STARTED_TIME ] ; then # 如果最新的全备未过期则以最新的全备文件名命名在增量备份目录下新建目录 echo -e "完全备份$LATEST_FULL_BACKUP未过期,将根据$LATEST_FULL_BACKUP名字作为增量备份基础目录名" echo " " NEW_INCRDIR=$INCRBACKUP_DIR/$LATEST_FULL_BACKUP mkdir -p $NEW_INCRDIR # 查找最新的增量备份是否存在.指定一个备份的路径作为增量备份的基础 LATEST_INCR_BACKUP=`find $NEW_INCRDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1` if [ ! $LATEST_INCR_BACKUP ] ; then INCRBASEDIR=$FULLBACKUP_DIR/$LATEST_FULL_BACKUP echo -e "增量备份将以$INCRBASEDIR作为备份基础目录" echo " " else INCRBASEDIR=$INCRBACKUP_DIR/${LATEST_FULL_BACKUP}/${LATEST_INCR_BACKUP} echo -e "增量备份将以$INCRBASEDIR作为备份基础目录" echo " " fi echo "使用$INCRBASEDIR作为基础本次增量备份的基础目录." $INNOBACKUPEXFULL --defaults-file=$MY_CNF --use-memory=4G $MYSQL_CMD --incremental $NEW_INCRDIR --incremental-basedir $INCRBASEDIR > $TMPLOG 2>&1 #保留一份备份的详细日志 cat $TMPLOG>/databackup/xtrabackuplog/$logfiledate if [ -z "`tail -1 $TMPLOG | grep ‘innobackupex: completed OK!‘`" ] ; then echo "$INNOBACKUPEX命令执行失败:"; echo echo -e "---------- $INNOBACKUPEX_PATH错误 ----------" cat $TMPLOG rm -f $TMPLOG exit 1 fi THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"‘\" ) ; print p[2] }" $TMPLOG` rm -f $TMPLOG echo -n "数据库成功备份到:$THISBACKUP" echo # 提示应该保留的备份文件起点 LATEST_FULL_BACKUP=`find $FULLBACKUP_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1` NEW_INCRDIR=$INCRBACKUP_DIR/$LATEST_FULL_BACKUP LATEST_INCR_BACKUP=`find $NEW_INCRDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1` RES_FULL_BACKUP=${FULLBACKUP_DIR}/${LATEST_FULL_BACKUP} RES_INCRE_BACKUP=`dirname ${INCRBACKUP_DIR}/${LATEST_FULL_BACKUP}/${LATEST_INCR_BACKUP}` echo echo -e ‘\e[31m NOTE:---------------------------------------------------------------------------------.\e[m‘ #红色 echo -e "必须保留$KEEP_FULLBACKUP份全备即全备${RES_FULL_BACKUP}和${RES_INCRE_BACKUP}目录中所有增量备份." echo -e ‘\e[31m NOTE:---------------------------------------------------------------------------------.\e[m‘ #红色 echo else echo "*********************************" echo -e "正在执行全新的完全备份...请稍等..." echo "*********************************" $INNOBACKUPEXFULL --defaults-file=$MY_CNF --use-memory=4G $MYSQL_CMD $FULLBACKUP_DIR > $TMPLOG 2>&1 #保留一份备份的详细日志 cat $TMPLOG>/databackup/xtrabackuplog/$logfiledate if [ -z "`tail -1 $TMPLOG | grep ‘innobackupex: completed OK!‘`" ] ; then echo "$INNOBACKUPEX命令执行失败:"; echo echo -e "---------- $INNOBACKUPEX_PATH错误 ----------" cat $TMPLOG rm -f $TMPLOG exit 1 fi THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"‘\" ) ; print p[2] }" $TMPLOG` rm -f $TMPLOG echo -n "数据库成功备份到:$THISBACKUP" echo # 提示应该保留的备份文件起点 LATEST_FULL_BACKUP=`find $FULLBACKUP_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1` RES_FULL_BACKUP=${FULLBACKUP_DIR}/${LATEST_FULL_BACKUP} echo echo -e ‘\e[31m NOTE:---------------------------------------------------------------------------------.\e[m‘ #红色 echo -e "无增量备份,必须保留$KEEP_FULLBACKUP份全备即全备${RES_FULL_BACKUP}." echo -e ‘\e[31m NOTE:---------------------------------------------------------------------------------.\e[m‘ #红色 echo fi #删除过期的全备 echo -e "find expire backup file...........waiting........." echo -e "寻找过期的全备文件并删除">>/databackup/xtrabackuplog/$logfiledate find $FULLBACKUP_DIR -type d -name "201*" -mtime +2| xargs rm -fr {}; #删除过期的增备 echo -e "寻找过期的增备文件并删除" >>/databackup/xtrabackuplog/$logfiledate find $INCRBACKUP_DIR -type d -name "201*" -mtime +2| xargs rm -fr {}; echo echo "完成于: `date +%F‘ ‘%T‘ ‘%w`" exit 0
五:验证结果
脚本执行完毕后,则会在/databackup/xtrabackup下创建两个文件夹,分别是full代表全库备份和incre增量备份
第一次执行,则创建全库备份,该脚本每天创建一分全库备份,增量备份则根据执行脚本的间隔来执行。
六:计划任务参考
这里给出一个事例仅供参考:
编辑crontab计划任务
00 */1 * * *
/root/shell/xtrabackup.sh 1>/dev/null
2>&1 &
这里的意思是每一小时自动执行一次脚本,增量备份的间隔是1小时
时间: 2024-10-08 19:52:15