测试前准备:

1 确保数据库已安装好,我的mysql版本mysql-5.1.52
2 准备安装包percona-xtrabackup-2.0.2-461.tar.gz  官网:http://www.percona.com/
 
[root@acong tools]# tar zxf percona-xtrabackup-2.0.2-461.tar.gz 
[root@acong tools]# cd percona-xtrabackup-2.0.2/bin/
[root@acong bin]# ll
total 32280
-rwxr-xr-x 1 root root    98778 Aug  8 00:09 innobackupex
lrwxrwxrwx 1 root root       12 Sep  2 19:59 innobackupex-1.5.1 -> innobackupex
-rwxr-xr-x 1 root root  2031711 Aug  8 00:09 xbstream
-rwxr-xr-x 1 root root  9795839 Aug  8 00:06 xtrabackup
-rwxr-xr-x 1 root root  8363860 Aug  8 00:09 xtrabackup_51
-rwxr-xr-x 1 root root 12693712 Aug  8 00:04 xtrabackup_55 
[root@acong bin]# cp innobackupex /usr/bin/innobackupex
[root@acong bin]# cp xtrabackup /usr/bin/xtrabackup
[root@acong bin]# cp xtrabackup_51 /usr/bin/xtrabackup_51
 
[root@acong bin]# mysql
mysql> set password=password('123456');    ##为mysql设置密码
Query OK, 0 rows affected (0.00 sec)
 
 
 
   
  1. ######################################### 
  2. 整库备份及恢复的思路 
  3. 1 mysql中创建数据 
  4. 2 innobackpx 备份 
  5. 3 /usr/local/mysql/data下删除所有文件 
  6. 4 恢复 
  7. 整库备份直接用innobackupex-1.5.1脚本。 
  8. 只对InnoDB或xtradb表,进行备份直接用xtrabackup。 
  9. ######################################### 
[root@acong bin]# mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.52 Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)
 
mysql> show engines;       ##查看默认的引擎
+------------+---------+
| Engine     | Support |
+------------+---------+
| MRG_MYISAM | YES     |
| CSV        | YES     |
| MyISAM     | DEFAULT |
| InnoDB     | YES     |
| MEMORY     | YES     |
+------------+---------+
 
 
 
   
  1. [root@acong bin]# vim /etc/my.cnf      ##在mysqld模块中添加如下两行 
  2. [mysqld] 
  3. datadir=/usr/local/mysql/data 
  4. default_table_type = InnoDB 
 
[root@acong bin]# /etc/init.d/mysqld restart      ##重启数据库后查看引擎
Shutting down MySQL....                                    [  OK  ]
Starting MySQL.                                            [  OK  ]
 
[root@acong bin]# mysql -uroot -p123456
 
mysql> show variables like '%storage_engine%'; 
+----------------+--------+
| Variable_name  | Value  |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+
1 row in set (0.00 sec)
 
mysql> create database acong;
Query OK, 1 row affected (0.00 sec)
 
mysql> use acong;
Database changed
mysql> create table acong(id int);
Query OK, 0 rows affected (0.01 sec)
 
mysql> insert into acong value(1);
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into acong value(2);
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into acong value(3);
Query OK, 1 row affected (0.01 sec)
 
mysql> insert into acong value(4);
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into acong value(5);
Query OK, 1 row affected (0.01 sec)
 
mysql> insert into acong value(6);
Query OK, 1 row affected (0.01 sec)
 
mysql> select * from acong;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
+------+
6 rows in set (0.00 sec)
mysql> \q
Bye
[root@acong bin]# mkdir /acong            ##在根下建立acong目录
[root@acong bin]# ll /acong/
total 0
[root@acong bin]# cd /usr/local/mysql/data/
[root@acong data]# ll
total 20540
drwx------ 2 mysql mysql     4096 Sep  2 20:18 acong
-rw-rw---- 1 mysql root      2270 Sep  2 20:15 acong.err
-rw-rw---- 1 mysql mysql        6 Sep  2 20:15 acong.pid
-rw-rw---- 1 mysql mysql 10485760 Sep  2 20:19 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Sep  2 20:19 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Sep  2 19:49 ib_logfile1
drwx------ 2 mysql root      4096 Sep  2 19:47 mysql
drwx------ 2 mysql root      4096 Sep  2 19:47 test 
[root@acong data]# cd acong
[root@acong acong]# ll
total 16
-rw-rw---- 1 mysql mysql 8556 Sep  2 20:18 acong.frm
-rw-rw---- 1 mysql mysql   65 Sep  2 20:18 db.opt
 
整库备份:
 
 
   
  1. [root@acong acong]# innobackupex --user=root --password=123456 --defaults-file=/etc/my.cnf /acong/ 
[root@acong acong]# ll /acong        ##查看备份好了没有
total 4
drwxr-xr-x 5 root root 4096 Sep  2 20:25 2012-09-02_20-25-08
[root@acong acong]# ll /acong/2012-09-02_20-25-08/    ##备份这里了,是不是和/usr/local/mysql/data/下的文件一样
total 10288
drwxr-xr-x 2 root root     4096 Sep  2 20:25 acong
-rw-r--r-- 1 root root      188 Sep  2 20:25 backup-my.cnf
-rw-r----- 1 root root 10485760 Sep  2 20:25 ibdata1
drwxr-xr-x 2 root root     4096 Sep  2 20:25 mysql
drwxr-xr-x 2 root root     4096 Sep  2 20:25 test
-rw-r--r-- 1 root root       13 Sep  2 20:25 xtrabackup_binary
-rw-r--r-- 1 root root        4 Sep  2 20:25 xtrabackup_binlog_info
-rw-r----- 1 root root       79 Sep  2 20:25 xtrabackup_checkpoints
-rw-r----- 1 root root     2560 Sep  2 20:25 xtrabackup_logfile
##############################################################################
整库的恢复:
1 先停掉数据库
[root@acong acong]# /etc/init.d/mysqld stop
Shutting down MySQL.                                     [  OK  ]
[root@acong acong]# netstat -lnt        ##查看是否关闭成功
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 :::22                       :::*                        LISTEN
2 删除数据库目录下的所有数据库文件。
[root@acong data]# pwd
/usr/local/mysql/data
[root@acong data]# ll
total 20536
drwx------ 2 mysql mysql     4096 Sep  2 20:18 acong
-rw-rw---- 1 mysql root      2692 Sep  2 20:40 acong.err
-rw-rw---- 1 mysql mysql 10485760 Sep  2 20:40 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Sep  2 20:40 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Sep  2 19:49 ib_logfile1
drwx------ 2 mysql root      4096 Sep  2 19:47 mysql
drwx------ 2 mysql root      4096 Sep  2 19:47 test
[root@acong data]# rm -rf *
[root@acong data]# ll
total 0
 
 
   
  1. [root@acong data]# innobackupex --apply-log --defaults-file=/etc/my.cnf /acong/2012-09-02_20-25-08/ 
  2. [root@acong data]# innobackupex --copy-back --defaults-file=/etc/my.cnf /acong/2012-09-02_20-25-08/ 
[root@acong data]# pwd
/usr/local/mysql/data
[root@acong data]# ll
total 10268
drwxr-xr-x 2 root root     4096 Sep  2 20:48 acong
-rw-r----- 1 root root 10485760 Sep  2 20:46 ibdata1
drwxr-xr-x 2 root root     4096 Sep  2 20:48 mysql
drwxr-xr-x 2 root root     4096 Sep  2 20:48 test
 
有文件了,检查下是否恢复成功。
[root@acong data]# /etc/init.d/mysqld start
Starting MySQL.Manager of pid-file quit without updating fi       [FAILED]
 mysql启动失败了,我们看到/usr/local/mysql/data下文件属主和属组都是root
 
[root@acong data]# mysql -uroot -p123456
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| acong              |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)
 
mysql> use acong;
Database changed
 
mysql> show tables;
+-----------------+
| Tables_in_acong |
+-----------------+
| acong           |
+-----------------+
1 row in set (0.00 sec)
 
mysql> select * from acong;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
+------+
6 rows in set (0.01 sec)
 
 
Xtrabackup 整库备份和恢复就完成了,如有问题欢迎留言。