当前位置:Gxlcms > mysql > Mysql开源备份工具Xtrabackup备份部署_MySQL

Mysql开源备份工具Xtrabackup备份部署_MySQL

时间:2021-07-01 10:21:17 帮助过:21人阅读

bitsCN.com

Mysql开源备份工具Xtrabackup备份部署

Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品。

Xtrabackup有两个主要的工具:xtrabackup、innobackupex

  1、xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表

  2、innobackupex是参考了InnoDB Hotbackup的innoback脚本修改而来的.innobackupex是一个perl脚本封装,封装了xtrabackup。主要是为了方便的 同时备份InnoDB和MyISAM引擎的表,但在处理myisam时需要加一个读锁。并且加入了一些使用的选项。如slave-info可以记录备份恢 复后,作为slave需要的一些信息,根据这些信息,可以很方便的利用备份来重做slave。

Xtrabackup下载地址:

http://www.percona.com/downloads/XtraBackup/LATEST/binary/

选择合适的版本,我这里选择binary。

解压到/usr/src/目录,解压后目录名percona-xtrabackup-2.1.3,进入到bin目录,即可直接使用innobackupex命令

# pwd

/usr/src/percona-xtrabackup-2.1.3/bin

# ls -l

total 112396

-rwxr-xr-x 1 root root 110738 Jun 7 11:43 innobackupex

-rwxr-xr-x 1 root root 110738 Jun 7 11:43 innobackupex-1.5.1

-rwxr-xr-x 1 root root 2211237 Jun 7 11:43 xbcrypt

-rwxr-xr-x 1 root root 2285672 Jun 7 11:43 xbstream

-rwxr-xr-x 1 root root 13033745 Jun 7 11:43 xtrabackup

-rwxr-xr-x 1 root root 16333506 Jun 7 11:43 xtrabackup_55

-rwxr-xr-x 1 root root 80988093 Jun 7 11:43 xtrabackup_56

在开始使用的时候可能会报如下错误:

sh: xtrabackup_55: command not found

innobackupex: fatal error: no 'mysqld' group in MySQL options

解决办法xtrabackup_55复制到/usr/bin下即可。

# cp /usr/src/percona-xtrabackup-2.1.3/bin/xtrabackup_55 /usr/bin/

建立mysql备份用户:

mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'backupuser'@'localhost' identified by 'skEBfef5E2';

mysql> FLUSH PRIVILEGES;

备份脚本内容(每周一次全备,六次增量备份,保留2周的备份,并压缩):

[plain]

#!/bin/bash

BEGINTIME=`date +"%Y-%m-%d %H:%M:%S"`

format_time=`date +"%Y-%m-%d_%H:%M:%S"`

week=`date +%w`

backupbin=/usr/src/percona-xtrabackup-2.1.3/bin

backdir=/data/databasebak/bak

file_cnf=/etc/my.cnf

user_name=backupuser

password="skEBfef5E2"

db="db1 db2 db3 db4"

out_log=$backdir/xtrabackup_log_$format_time

time_cost=$backdir/xtrabackup_time.txt

if [ -f "$backdir.lastlastlastweek.gz" ];then

rm -rf $backdir.lastlastweek.gz

mv $backdir.lastweek.gz $backdir.lastlastweek.gz

fi

if [ -d "$backdir/rec5" ];then

gzip -cr $backdir >$backdir.lastweek.gz

rm -rf $backdir

mkdir $backdir

fi

#full

if [ ! -d "$backdir/full" ];then

echo "#####start full backup at $BEGINTIME to directory full" >>$time_cost

$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" $backdir/full 1> $out_log 2>&1

break;

elif [ ! -d "$backdir/rec0" ];then

echo "#####start 0 incremental backup at $BEGINTIME to directory rec0" >>$time_cost

$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/full $backdir/rec0 1> $out_log 2>&1

break;

elif [ ! -d "$backdir/rec1" ];then

echo "#####start 1 incremental backup at $BEGINTIME to directory rec1" >>$time_cost

$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec0 $backdir/rec1 1> $out_log 2>&1

break;

elif [ ! -d "$backdir/rec2" ];then

echo "#####start 2 incremental backup at $BEGINTIME to directory rec2" >>$time_cost

$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec1 $backdir/rec2 1> $out_log 2>&1

break;

elif [ ! -d "$backdir/rec3" ];then

echo "#####start 3 incremental backup at $BEGINTIME to directory rec3" >>$time_cost

$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec2 $backdir/rec3 1> $out_log 2>&1

break;

elif [ ! -d "$backdir/rec4" ];then

echo "#####start 4 incremental backup at $BEGINTIME to directory rec4" >>$time_cost

$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec3 $backdir/rec4 1> $out_log 2>&1

break;

elif [ ! -d "$backdir/rec5" ];then

echo "#####start 5 incremental backup at $BEGINTIME to directory rec5" >>$time_cost

$backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec4 $backdir/rec5 1> $out_log 2>&1

break;

fi

ENDTIME=`date +"%Y-%m-%d %H:%M:%S"`

begin_data=`date -d "$BEGINTIME" +%s`

end_data=`date -d "$ENDTIME" +%s`

spendtime=`expr $end_data - $begin_data`

echo "it takes $spendtime sec for packing the data directory" >>$time_cost

部署备份脚本;

# crontab -l

10 2 * * * /opt/cron/mysqldbbakup.sh

恢复:

#全备恢复:

innobackupex --apply-log --redo-only --defaults-file=$file_cnf --user=$user_name --password=$password $backdir/full

#增备恢复,修改增量备份目录,逐个apply-log,到第五天的增量rec4都是输出都显示innobackupex: completed OK!

innobackupex --apply-log --redo-only --defaults-file=$file_cnf --user=$user_name --password=$password $backdir/full --incremental-dir=$backdir/rec5

bitsCN.com

人气教程排行