博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Mysql】xtrabackup全量增量备份脚本
阅读量:5780 次
发布时间:2019-06-18

本文共 5111 字,大约阅读时间需要 17 分钟。

全量备份脚本

#!/bin/sh# add ling INNOBACKUPEX=innobackupexINNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEXTODAY=`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.cnfMYSQL=/usr/local/mariadb/bin/mysqlMYSQLADMIN=/usr/local/mariadb/bin/mysqladminBACKUPDIR=/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 proceedingif [ ! -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 outputecho "----------------------------"echoecho "$0: MySQL backup script"echo "started: `date`"echo # Create full and incr backup directories if they not exist.for i in $FULLBACKUPDIR $INCRBACKUPDIRdo        if [ ! -d $i ]; then                mkdir -pv $i        fidone # 压缩上传前一天的备份echo "压缩前一天的备份,scp到远程主机"cd $BACKUPDIRtar -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>&1else  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 1fi# 这里获取这次备份的目录 THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`echo "THISBACKUP=$THISBACKUP"#rm -f $TMPFILEecho "Databases backed up successfully to: $THISBACKUP"# Cleanupecho "delete tar files of 3 days ago"find $BACKUPDIR/ -mtime +3 -name "*.tar.gz"  -exec rm -rf {} \; echoecho "completed: `date`"exit 0

增量备份脚本

#!/bin/sh# add ling INNOBACKUPEX=innobackupexINNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEXTODAY=`date +%Y%m%d%H%M`USEROPTIONS="--user=user --password=123456"TMPFILE="/logs/mysql/incr_$TODAY.$$.tmp"MYCNF=/etc/my.cnfMYSQL=/usr/local/mariadb/bin/mysqlMYSQLADMIN=/usr/local/mariadb/bin/mysqladminBACKUPDIR=/backup/mysql # 备份的主目录FULLBACKUPDIR=$BACKUPDIR/full # 全库备份的目录INCRBACKUPDIR=$BACKUPDIR/incr # 增量备份的目录############################################################################## Display error message and exit#############################################################################error(){    echo "$1" 1>&2    exit 1} # Check options before proceedingif [ ! -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 outputecho "----------------------------"echoecho "$0: MySQL backup script"echo "started: `date`"echo # Create full and incr backup directories if they not exist.for i in $FULLBACKUPDIR $INCRBACKUPDIRdo        if [ ! -d $i ]; then                mkdir -pv $i        fidone # Find latest full backupLATEST_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_FULLmkdir -p $TMPINCRDIRBACKTYPE="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_FULLelse  INCRBASEDIR=$LATEST_INCRfiecho "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 1fi# 这里获取这次备份的目录 THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`echo "THISBACKUP=$THISBACKUP"echoecho "Databases backed up successfully to: $THISBACKUP"echoecho "incremental completed: `date`"exit 0

转载地址:http://pxuyx.baihongyu.com/

你可能感兴趣的文章
洛谷——P2176 [USACO14FEB]路障Roadblock
查看>>
详解区块链中EOS的作用。
查看>>
我的友情链接
查看>>
mysql-error 1236
查看>>
sshd_config设置参数笔记
查看>>
循序渐进Docker(一)docker简介、安装及docker image管理
查看>>
jsp页面修改后浏览器中不生效
查看>>
大恶人吉日嘎拉之走火入魔闭门造车之.NET疯狂架构经验分享系列之(四)高效的后台权限判断处理...
查看>>
Oracle HRMS,PeopleSoft HR,SAP HR区别
查看>>
信号量实现进程同步
查看>>
Spring4-自动装配Beans-通过构造函数参数的数据类型按属性自动装配Bean
查看>>
iPhone图标
查看>>
hdu 3308 LCIS
查看>>
Anterior and posterior commissures
查看>>
win10.64位wnmp-nginx1.14.0 + PHP 5. 6.36 + MySQL 5.5.59 环境配置搭建 结合Thinkphp3.2.3
查看>>
字符串-操作
查看>>
如何查看python selenium的api
查看>>
Python_Mix*random模块,time模块,sys模块,os模块
查看>>
iframe刷新问题
查看>>
数据解码互联网行业职位
查看>>