【Mysql】xtrabackup全量增量备份脚本
来源:SegmentFault
时间:2023-02-16 15:32:58 149浏览 收藏
本篇文章向大家介绍《【Mysql】xtrabackup全量增量备份脚本》,主要包括MySQL、shell,具有一定的参考价值,需要的朋友可以参考一下。
全量备份脚本
#!/bin/sh # add ling INNOBACKUPEX=innobackupex INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX TODAY=`date +%Y%m%d%H%M` YESTERDAY=`date -d"yesterday" +%Y%m%d%H%M` USEROPTIONS="--user=user --password=123456" TMPFILE="/logs/mysql/innobackup_$TODAY.$$.tmp" MYCNF=/etc/my.cnf MYSQL=/usr/local/mariadb/bin/mysql MYSQLADMIN=/usr/local/mariadb/bin/mysqladmin BACKUPDIR=/backup/mysql # 备份的主目录 FULLBACKUPDIR=$BACKUPDIR/full # 全库备份的目录 INCRBACKUPDIR=$BACKUPDIR/incr # 增量备份的目录 KEEP=1 # 保留几个全库备份 # Grab start time ############################################################################# # Display error message and exit ############################################################################# error() { echo "$1" 1>&2 exit 1 } # Check options before proceeding if [ ! -x $INNOBACKUPEXFULL ]; then error "$INNOBACKUPEXFULL does not exist." fi if [ ! -d $BACKUPDIR ]; then error "Backup destination folder: $BACKUPDIR does not exist." fi if [ -z "`$MYSQLADMIN $USEROPTIONS status | grep 'Uptime'`" ] ; then error "HALTED: MySQL does not appear to be running." fi if ! `echo 'exit' | $MYSQL -s $USEROPTIONS` ; then error "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)." fi # Some info output echo "----------------------------" echo echo "$0: MySQL backup script" echo "started: `date`" echo # Create full and incr backup directories if they not exist. for i in $FULLBACKUPDIR $INCRBACKUPDIR do if [ ! -d $i ]; then mkdir -pv $i fi done # 压缩上传前一天的备份 echo "压缩前一天的备份,scp到远程主机" cd $BACKUPDIR tar -zcvf $YESTERDAY.tar.gz ./full/ ./incr/ scp -P 8022 $YESTERDAY.tar.gz root@192.168.10.46:/data/backup/mysql/ if [ $? = 0 ]; then rm -rf $BACKUPDIR/full $BACKUPDIR/incr echo "Running new full backup." innobackupex --defaults-file=$MYCNF $USEROPTIONS $FULLBACKUPDIR > $TMPFILE 2>&1 else echo "Error with scp." fi if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then echo "$INNOBACKUPEX failed:"; echo echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------" # cat $TMPFILE # rm -f $TMPFILE exit 1 fi # 这里获取这次备份的目录 THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE` echo "THISBACKUP=$THISBACKUP" #rm -f $TMPFILE echo "Databases backed up successfully to: $THISBACKUP" # Cleanup echo "delete tar files of 3 days ago" find $BACKUPDIR/ -mtime +3 -name "*.tar.gz" -exec rm -rf {} \; echo echo "completed: `date`" exit 0
增量备份脚本
#!/bin/sh # add ling INNOBACKUPEX=innobackupex INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX TODAY=`date +%Y%m%d%H%M` USEROPTIONS="--user=user --password=123456" TMPFILE="/logs/mysql/incr_$TODAY.$$.tmp" MYCNF=/etc/my.cnf MYSQL=/usr/local/mariadb/bin/mysql MYSQLADMIN=/usr/local/mariadb/bin/mysqladmin BACKUPDIR=/backup/mysql # 备份的主目录 FULLBACKUPDIR=$BACKUPDIR/full # 全库备份的目录 INCRBACKUPDIR=$BACKUPDIR/incr # 增量备份的目录 ############################################################################# # Display error message and exit ############################################################################# error() { echo "$1" 1>&2 exit 1 } # Check options before proceeding if [ ! -x $INNOBACKUPEXFULL ]; then error "$INNOBACKUPEXFULL does not exist." fi if [ ! -d $BACKUPDIR ]; then error "Backup destination folder: $BACKUPDIR does not exist." fi if [ -z "`$MYSQLADMIN $USEROPTIONS status | grep 'Uptime'`" ] ; then error "HALTED: MySQL does not appear to be running." fi if ! `echo 'exit' | $MYSQL -s $USEROPTIONS` ; then error "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)." fi # Some info output echo "----------------------------" echo echo "$0: MySQL backup script" echo "started: `date`" echo # Create full and incr backup directories if they not exist. for i in $FULLBACKUPDIR $INCRBACKUPDIR do if [ ! -d $i ]; then mkdir -pv $i fi done # Find latest full backup LATEST_FULL=`find $FULLBACKUPDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n"` echo "LATEST_FULL=$LATEST_FULL" # Run an incremental backup if latest full is still valid. # Create incremental backups dir if not exists. TMPINCRDIR=$INCRBACKUPDIR/$LATEST_FULL mkdir -p $TMPINCRDIR BACKTYPE="incr" # Find latest incremental backup. LATEST_INCR=`find $TMPINCRDIR -mindepth 1 -maxdepth 1 -type d | sort -nr | head -1` echo "LATEST_INCR=$LATEST_INCR" # If this is the first incremental, use the full as base. Otherwise, use the latest incremental as base. if [ ! $LATEST_INCR ] ; then INCRBASEDIR=$FULLBACKUPDIR/$LATEST_FULL else INCRBASEDIR=$LATEST_INCR fi echo "Running new incremental backup using $INCRBASEDIR as base." innobackupex --defaults-file=$MYCNF $USEROPTIONS --incremental $TMPINCRDIR --incremental-basedir $INCRBASEDIR > $TMPFILE 2>&1 if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then echo "$INNOBACKUPEX failed:"; echo echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------" exit 1 fi # 这里获取这次备份的目录 THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE` echo "THISBACKUP=$THISBACKUP" echo echo "Databases backed up successfully to: $THISBACKUP" echo echo "incremental completed: `date`" exit 0
理论要掌握,实操不能落!以上关于《【Mysql】xtrabackup全量增量备份脚本》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
335 收藏
-
467 收藏
-
303 收藏
-
176 收藏
-
368 收藏
-
475 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习