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