#!/bin/bash
BEGINTIME=`date +”%Y-%m-%d %H:%M:%S”`
format_time=`date +”%Y-%m-%d_%H:%M:%S”`
week=`date +%Y-%m-%d`
backupbin=/usr/bin
backdir=/database/backup
file_cnf=/etc/my.cnf
user_name=backup
password=”backup”
out_log=$backdir/xtrabackup_log_$format_time
time_cost=$backdir/xtrabackup_time.txt
if [ -d "$backdir/incr5" ];then
gzip -cr $backdir >$backdir.$week.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 $backdir/full 1> $out_log 2>&1
break;
elif [ ! -d "$backdir/incr0" ];then
echo “#####start 0 incremental backup at $BEGINTIME to directory incr0″ >>$time_cost
$backupbin/innobackupex –defaults-file=$file_cnf –no-timestamp –user=$user_name –password=$password –slave-info –incremental –incremental-basedir=$backdir/full $backdir/incr0 1> $out_log 2>&1
break;
elif [ ! -d "$backdir/incr1" ];then
echo “#####start 1 incremental backup at $BEGINTIME to directory incr1″ >>$time_cost
$backupbin/innobackupex –defaults-file=$file_cnf –no-timestamp –user=$user_name –password=$password –slave-info –incremental –incremental-basedir=$backdir/incr0 $backdir/incr1 1> $out_log 2>&1
break;
elif [ ! -d "$backdir/incr2" ];then
echo “#####start 2 incremental backup at $BEGINTIME to directory incr2″ >>$time_cost
$backupbin/innobackupex –defaults-file=$file_cnf –no-timestamp –user=$user_name –password=$password –slave-info –incremental –incremental-basedir=$backdir/incr1 $backdir/incr2 1> $out_log 2>&1
break;
elif [ ! -d "$backdir/incr3" ];then
echo “#####start 3 incremental backup at $BEGINTIME to directory incr3″ >>$time_cost
$backupbin/innobackupex –defaults-file=$file_cnf –no-timestamp –user=$user_name –password=$password –slave-info –incremental –incremental-basedir=$backdir/incr2 $backdir/incr3 1> $out_log 2>&1
break;
elif [ ! -d "$backdir/incr4" ];then
echo “#####start 4 incremental backup at $BEGINTIME to directory incr4″ >>$time_cost
$backupbin/innobackupex –defaults-file=$file_cnf –no-timestamp –user=$user_name –password=$password –slave-info –incremental –incremental-basedir=$backdir/incr3 $backdir/incr4 1> $out_log 2>&1
break;
elif [ ! -d "$backdir/incr5" ];then
echo “#####start 5 incremental backup at $BEGINTIME to directory incr5″ >>$time_cost
$backupbin/innobackupex –defaults-file=$file_cnf –no-timestamp –user=$user_name –password=$password –slave-info –incremental –incremental-basedir=$backdir/incr4 $backdir/incr5 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
MySQL数据库服务器 192.168.17.80:
1、零级备份:
mysql> select * from test.compary;
+—-+——-+————+———+——+
| id | color | birthday | contact | vip |
+—-+——-+————+———+——+
| 5 | blue | 1988-06-27 | 186 | 1 |
| 6 | blue | 1988-06-27 | 186 | 1 |
| 7 | blue | 1988-06-27 | 186 | 1 |
| 8 | blue | 1988-06-27 | 186 | 1 |
| 9 | blue | 1988-06-27 | 186 | 1 |
| 10 | blue | 1988-06-27 | 186 | 1 |
sh /root/xtrabackup.sh
ls –lt
drwxr-xr-x 5 root root 4096 Dec 10 09:24 full
-rw-r–r– 1 root root 4134 Dec 10 09:24 xtrabackup_log_2013-12-10_09:24:09
-rw-r–r– 1 root root 110 Dec 10 09:24 xtrabackup_time.txt
2、一级增量备份:
mysql> insert into test.compary(`id`,`color`,`birthday`,`contact`,`vip`)values (’21′,’red’,’1988-08-08′,’188′,’2′);
mysql> select * from test.compary;
+—-+——-+————+———+——+
| id | color | birthday | contact | vip |
+—-+——-+————+———+——+
| 5 | blue | 1988-06-27 | 186 | 1 |
| 6 | blue | 1988-06-27 | 186 | 1 |
| 7 | blue | 1988-06-27 | 186 | 1 |
| 8 | blue | 1988-06-27 | 186 | 1 |
| 9 | blue | 1988-06-27 | 186 | 1 |
| 10 | blue | 1988-06-27 | 186 | 1 |
| 21 | red | 1988-08-08 | 188 | 2 |
sh /root/xtrabackup.sh
ll
drwxr-xr-x 5 root root 4096 Dec 10 09:24 full
drwxr-xr-x 5 root root 4096 Dec 10 09:28 incr0
-rw-r–r– 1 root root 4134 Dec 10 09:24 xtrabackup_log_2013-12-10_09:24:09
-rw-r–r– 1 root root 4235 Dec 10 09:28 xtrabackup_log_2013-12-10_09:28:34
-rw-r–r– 1 root root 230 Dec 10 09:28 xtrabackup_time.txt
3、二级增量备份:
mysql> insert into test.compary(`id`,`color`,`birthday`,`contact`,`vip`)values (’22′,’black’,’1988-09-09′,’188′,’3′);
Query OK, 1 row affected (0.01 sec)
mysql> select * from test.compary;
+—-+——-+————+———+——+
| id | color | birthday | contact | vip |
+—-+——-+————+———+——+
| 5 | blue | 1988-06-27 | 186 | 1 |
| 6 | blue | 1988-06-27 | 186 | 1 |
| 7 | blue | 1988-06-27 | 186 | 1 |
| 8 | blue | 1988-06-27 | 186 | 1 |
| 9 | blue | 1988-06-27 | 186 | 1 |
| 10 | blue | 1988-06-27 | 186 | 1 |
| 21 | red | 1988-08-08 | 188 | 2 |
| 22 | black | 1988-09-09 | 188 | 3 |
sh /root/xtrabackup.sh
ll
drwxr-xr-x 5 root root 4096 Dec 10 09:24 full
drwxr-xr-x 5 root root 4096 Dec 10 09:28 incr0
drwxr-xr-x 5 root root 4096 Dec 10 09:31 incr1
-rw-r–r– 1 root root 4134 Dec 10 09:24 xtrabackup_log_2013-12-10_09:24:09
-rw-r–r– 1 root root 4235 Dec 10 09:28 xtrabackup_log_2013-12-10_09:28:34
-rw-r–r– 1 root root 4205 Dec 10 09:31 xtrabackup_log_2013-12-10_09:31:38
-rw-r–r– 1 root root 350 Dec 10 09:31 xtrabackup_time.txt
4、全量恢复测试:
假定我们现在需要还原到第一次插入数据时状态。
具体操作。
打包上传文件到远程服务器
tar czvf full.tar full/
tar -czvf incr0.tar incr0/
tar -czvf incr0.tar incr1/
scp full.tar james@192.168.17.82:/databackup
scp incr0.tar james @192.168.17.82:/databackup
scp incr1.tar james@192.168.17.82:/databackup
192.168.17.82:
[root@host3 databackup]# ll
total 824
-rw-r–r– 1 root root 377468 Dec 10 09:37 full.tar
-rw-r–r– 1 root root 222011 Dec 10 09:38 incr0.tar
-rw-r–r– 1 root root 222034 Dec 10 09:39 incr1.tar
/etc/init.d/mysqld stop
cd /usr/local/mysql/
mv data/* databackup/
cd /databackup
tar xzvf full.tar
tar xzvf incr0.tar
tar zxvf incr1.tar
[root@host3 databackup]# ll
total 836
drwxr-xr-x 5 root root 4096 Dec 10 09:24 full
-rw-r–r– 1 root root 377468 Dec 10 09:37 full.tar
drwxr-xr-x 5 root root 4096 Dec 10 09:28 incr0
-rw-r–r– 1 root root 222011 Dec 10 09:38 incr0.tar
drwxr-xr-x 5 root root 4096 Dec 10 09:31 incr1
-rw-r–r– 1 root root 222034 Dec 10 09:39 incr1.tar
innobackupex –apply-log –redo-only –user=backupuser –password=’123456′–defaults-file=/etc/my.cnf /databackup/full/
innobackupex –apply-log –redo-only –user=backupuser –password=’123456′ –defaults-file=/etc/my.cnf /databackup/full/ –incremental-dir=/databackup/incr0/
innobackupex –copy-back –user=backupuser –password=’123456′ –defaults-file=/etc/my.cnf /databackup/full/
[root@host3 data]# cd data
[root@host3 data]# chown -R mysql.mysql ./
[root@host3 data]# /etc/init.d/mysqld start
[root@host3 data]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.11-log MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> select * from test.compary;
+—-+——-+————+———+——+
| id | color | birthday | contact | vip |
+—-+——-+————+———+——+
| 5 | blue | 1988-06-27 | 186 | 1 |
| 6 | blue | 1988-06-27 | 186 | 1 |
| 7 | blue | 1988-06-27 | 186 | 1 |
| 8 | blue | 1988-06-27 | 186 | 1 |
| 9 | blue | 1988-06-27 | 186 | 1 |
| 10 | blue | 1988-06-27 | 186 | 1 |
| 21 | red | 1988-08-08 | 188 | 2 |
+—-+——-+————+———+——+
7 rows in set (0.18 sec)
至此确认零一级恢复正常
6、增量二级恢复测试:
下面测试还原到第二次插入数据时状态
具体操作
192.168.17.82上:
[root@host3 data]# cd /databackup/
[root@host3 databackup]# ll
drwxr-xr-x 5 root root 4096 Dec 10 10:55 full
-rw-r–r– 1 root root 377468 Dec 10 10:51 full.tar
drwxr-xr-x 5 root root 4096 Dec 10 10:54 incr0
-rw-r–r– 1 root root 222011 Dec 10 10:52 incr0.tar
drwxr-xr-x 5 root root 4096 Dec 10 10:55 incr1
-rw-r–r– 1 root root 222034 Dec 10 10:52 incr1.tar
由于第一次的还原数据已写入FULL,INCR0目录下。所以,此时的这两个目录文件不能再使用了。需要删除,重新解压TAR包。
[root@host3 mysql]# /etc/init.d/mysqld stop
rm –rf /usr/local/mysql/databackup/*
mv data/* databackup/
innobackupex –apply-log –redo-only –user=backupuser –password=’123456′ –defaults-file=/etc/my.cnf /databackup/full/
innobackupex –apply-log –redo-only –user=backupuser –password=’123456′ –defaults-file=/etc/my.cnf /databackup/full/ –incremental-dir=/databackup/incr0/
innobackupex –apply-log –redo-only –user=backupuser –password=’123456′ –defaults-file=/etc/my.cnf /databackup/full/ –incremental-dir=/databackup/incr1/
innobackupex –copy-back –user=backupuser –password=’123456′ –defaults-file=/etc/my.cnf /databackup/full/
cd /usr/local/mysql/data
chown -R mysql.mysql ./
/etc/init.d/mysqld start
mysql> select * from test.compary;
+—-+——-+————+———+——+
| id | color | birthday | contact | vip |
+—-+——-+————+———+——+
| 5 | blue | 1988-06-27 | 186 | 1 |
| 6 | blue | 1988-06-27 | 186 | 1 |
| 7 | blue | 1988-06-27 | 186 | 1 |
| 8 | blue | 1988-06-27 | 186 | 1 |
| 9 | blue | 1988-06-27 | 186 | 1 |
| 10 | blue | 1988-06-27 | 186 | 1 |
| 21 | red | 1988-08-08 | 188 | 2 |
| 22 | black | 1988-09-09 | 188 | 3 |
+—-+——-+————+———+——+
8 rows in set (0.00 sec)
此时二级恢复正确