博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql xtrabackup自动增量备份shell脚本
阅读量:6329 次
发布时间:2019-06-22

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

#!/bin/sh# add ling INNOBACKUPEX=innobackupexINNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEXTODAY=`date +%Y%m%d%H%M%S`USEROPTIONS="--user=root --password=root"TMPFILE="/logs/mysql/innobackup_$TODAY.$$.tmp"TMPFILEDIR="/logs/mysql/"MYCNF=/etc/my.cnfMYSQL=/usr/bin/mysqlMYSQLADMIN=/usr/bin/mysqladminBACKUPDIR=/backup # 备份的主目录FULLBACKUPDIR=$BACKUPDIR/full INCRBACKUPDIR=$BACKUPDIR/incr # 增量备份的目录# 全库备份的目录# 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.if [ ! -d $FULLBACKUPDIR ]; then        mkdir -pv $FULLBACKUPDIRfiif [ ! -d $TMPFILEDIR ];then        mkdir -pv $TMPFILEDIRfi# find latest backup fileLATEST_FULL=`find $FULLBACKUPDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n"`echo "LATEST_FULL=$LATEST_FULL" # TMPINCRDIR 以全局备份的名字作为目录存放增量备份TMPINCRDIR=$INCRBACKUPDIR/$LATEST_FULL  #/backup/incr/2018-08-12_23-06-00Fathername=`basename $TMPINCRDIR`mkdir -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   # innobackupex --defaults-file=$MYCNF $USEROPTIONS $FULLBACKUPDIR > $TMPFILE 2>&1 # 如果这里获取这次备份的目录  但是这里使用打包的方法 获取到的不是 #/backup/2018-08-11_23-28-46而是/backup是一个目录 显示的不是文件无法判断文件是否创建成功 所以用下面的方法THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`if [ $? = 0 ]   then  echo "THISBACKUP=$THISBACKUP"  Filename=`basename $THISBACKUP`  #2018-08-13_02-25-18  echo "Filename=$Filename"  # rm -f $TMPFILE  echo "Databases backed up successfully to: $THISBACKUP"else  echo "backup failed"fiif [ -n "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then echo "$INNOBACKUPEX succeed:"else echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"# cat $TMPFILErm -f $TMPFILE exit 1fi    cd $TMPINCRDIR  tar -czvf -  $Filename  | openssl des3 -salt -k 123456 -out $TMPINCRDIR/$Filename.tar.gz  # tar -zcvf $TODAY.tar.gz ./full/   scp -P 22 $TMPINCRDIR/$Filename.tar.gz root@192.168.117.130:/backup/$Fathername/$Filename.tar.gz  # rm -rf $FULLBACKUPDIR  if [ $? = 0 ]   then    echo "scp succeed"    # rm -rf $BACKUPDIR/full   else    echo "Error with scp."  fiexit 0复制代码

转载于:https://juejin.im/post/5b7382856fb9a009aa15459c

你可能感兴趣的文章
markdown编辑
查看>>
ASCII 在线转换器
查看>>
Linux内核同步:RCU
查看>>
Android逆向进阶——让你自由自在脱壳的热身运动(dex篇)
查看>>
Java设计模式之五大创建型模式(附实例和详解)
查看>>
60 Permutation Sequence
查看>>
主流的RPC框架有哪些
查看>>
Hive学习之路 (七)Hive的DDL操作
查看>>
[转]mysql使用关键字作为列名的处理方式
查看>>
awesome go library 库,推荐使用的golang库
查看>>
树形展示形式的论坛
查看>>
jdbcTemplate 调用存储过程。 入参 array 返回 cursor
查看>>
C++中的stack类、QT中的QStack类
查看>>
Linux常用基本命令[cp]
查看>>
CSS 相对|绝对(relative/absolute)定位系列(一)
查看>>
关于 Nginx 配置 WebSocket 400 问题
查看>>
Glide和Govendor安装和使用
查看>>
Java全角、半角字符的关系以及转换
查看>>
Dubbo和Zookeeper
查看>>
前端项目课程3 jquery1.8.3到1.11.1有了哪些新改变
查看>>