47

干货:MySQL增量备份脚本

 5 years ago
source link: https://www.linuxprobe.com/mysql-innobackupex.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1 、线上环境有时候考虑到需要做增量备份数据库,所以写了个简单的脚本

2 、添加自动任务每小时或者更短时间执行就好了

#!/bin/sh
#author RuM
#date 2015-07-10
BAKDIR=/tmp/mysqlbak
BAKDIR_FULL=$BAKDIR/full
BAKDIR_ADD=$BAKDIR/add
CONF=/etc/my.cnf
passwd=123456
INNOBACKUPEX=/usr/bin/innobackupex

第一次执行会做一次全备跟增备,以后执行都会是增量备份

if [ -f "$INNOBACKUPEX" ]
     then
         if  [ -d "$BAKDIR" ]
              then
            echo "is ok"
              else
            mkdir $BAKDIR_FULL -p
            mkdir $BAKDIR_ADD -p 
        fi

全备

files=`ls $BAKDIR_FULL`
        if [ -z "$files" ]
            then
                $INNOBACKUPEX  --defaults-file=$CONF  --user=root --password=$PASSWD  $BAKDIR_FULL
                FULLNAME=$(dir "$BAKDIR_FULL")
                $INNOBACKUPEX  --defaults-file=$CONF  --user=root --password=$PASSWD --incremental-basedir=$BAKDIR_FULL/$FULLNAME/ --incremental $BAKDIR_ADD
            else

增量备份

                ADDNAME=$(ls -lt $BAKDIR_ADD |sed -n 2p|awk '{print $9}')
                $INNOBACKUPEX  --defaults-file=$CONF  --user=root --password=$PASSWD --incremental-basedir=$BAKDIR_ADD/$ADDNAME/ --incremental $BAKDIR_ADD
        fi
    else
        echo "is not install innobackupex"
fi

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK