1
|
[root@www~]
# mysqldump --all-databases --lock-all-tables --routines --triggers --master-data=2 --flush-logs > /backup/`date +%F_%T`all.sql
|
1
|
[root@www~]
# mysqlbinlog --start-datetime '2014-08-15 13:16:33' --stop-datetime '2014-08-15 13:32:52' /mydata/data/mysql-bin.* > /backup/binlog-`date +%F_%T`.sql
|
1
2
3
4
|
[root@www ~]
# cd /usr/local/mysql/
[root@www mysql]
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
rm
/mydata/data/
*
#下边产生了所悟日志,我们不需要,删除
[root@www ~]
# service mysqld start #启动mysql
|
1
|
[root@localhost data]
# mysql < /backup/2014-09-02_01\:04\:05all.sql
|
1
|
[root@localhost data]
# mysql < /backup/binlog-2014-09-02_01\:38\:16.sql
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@localhost ~]
# fdisk /dev/sdb
命令(输入 m 获取帮助):n
Select (default p): p
分区号 (3,4,默认 3):3
起始 扇区 (20973568-41943039,默认为 20973568):
将使用默认值 20973568
Last 扇区, +扇区 or +size{K,M,G} (20973568-41943039,默认为 41943039):+2G
分区 3 已设置为 Linux 类型,大小设为 2 GiB
命令(输入 m 获取帮助):p
设备 Boot Start End Blocks Id System
/dev/sdb1
2048 10487807 5242880 83 Linux
/dev/sdb2
10487808 20973567 5242880 83 Linux
/dev/sdb3
20973568 25167871 2097152 83 Linux
命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):3
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):w
[root@localhost ~]
# kpartx -af /dev/sdb #同步至内核
[root@localhost ~]
#partx -a /dev/sdb
|
1
2
3
4
5
|
[root@localhost ~]
# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created
[root@localhost ~]
# vgcreate myvg2 /dev/sdb3 Volume group "myvg2" successfully created
[root@localhost ~]
# lvcreate -L 1.5G -n mydata2 myvg2 Logical volume "mydata2" created
[root@localhost ~]
# mke2fs -t ext4 /dev/myvg2/mydata2 #格式化
[root@localhost ~]
# mount /dev/myvg2/mydata2 /mydata/data/ #将LVM分区挂载在MySQL的数据目录下上。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@localhost data]
# cd /usr/local/mysql/
[root@localhost mysql]
# ls
bin data INSTALL-BINARY mysql-
test
share COPYING docs lib README sql-bench COPYING.LESSER include
man
scripts support-files
[root@localhost mysql]
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
[root@localhost mysql]
# ls /mydata/data/ #查看数据目录是否初始化成功
aria_log.00000001 lost+found mysql-bin.000001 mysql-bin.index
test
aria_log_control mysql mysql-bin.000002 performance_schema
[root@localhost mysql]
# service mysqld start #启动mysql Starting MySQL SUCCESS!
[root@localhost mysql]
# mysql -uroot -p #登陆MySQL
mysql> create database aolens;
mysql> use aolens
Database changed
mysql> show tables;
Empty
set
(0.00 sec)
mysql> create table tb1 (
id
int);
#创建表
Query OK, 0 rows affected (0.22 sec)
mysql> insert into tb1(
id
) values (10),(20);
#添加数据
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [aolens]>
select
* from tb1;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
mysql> flush tables with
read
lock;
#对数据库施加锁
Query OK, 0 rows affected (0.01 sec)
mysql> flush master logs;
#滚动一下二进制日志。一般与做增量备份
且千万不要退出,已退出就释放锁了。所以另起一个
ssh
[root@node2 ~]
# lvcreate -L 200M -n mydata-snap /dev/myvg2/mydata2 -s -p r #创建快照,只读,快照名为mydata-snap Logical volume "mydata-snap" created
mysql> unlock tables;
#释放锁
Query OK, 0 rows affected (0.00 sec)
下边我们就可以从容的备份文件了!
[root@node2 ~]
# mkdir /snap
[root@node2 ~]
# mount /dev/myvg2/mydata-snap /snap/ #挂载快照到/snap/下 mount: block device /dev/mapper/myvg2-mydata--snap is write-protected, mounting read-only
[root@node2 ~]
# cd /snap/
[root@node2 snap]
# rsync -a aolens /backup/aolens-`date +%F-%T` #删除快照:
[root@node2 ~]
# lvremove /dev/myvg2/mydata-snap
[root@node2 ~]
# insert into tb1(id) values (30);
|
1
|
[root@www~]
# mysqlbinlog /mydata/data/mysql-bin.000004 > /backup/binlog-`date +%F_%T`.sql 备份二进制增量。
|
1
2
3
|
[root@node2 data]
# service mysqld stop #mysql数据库出现问题首先要停掉数据库。 Stopping mysqld: [ OK ]
[root@node2 data]
# cp -a /backup/aolens-2014-09-08-20\:11\:06/* /mydata/data/ #恢复数据
[root@node2 data]
# service mysqld start #启动MySQL Starting mysqld: [ OK ]
|
1
|
[root@node2 data]
# mysql < /backup/binlog-2014-09-08-20:11:06.sql
|
1
2
3
4
|
mysql> CREATE USER ’bkpuser’@’localhost’ IDENTIFIED BY ’s3cret’;
mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM ’bkpuser’;
mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO ’bkpuser’@’localhost’;
mysql> FLUSH PRIVILEGES;
|
1
2
3
4
|
[root@node2 ~]
# innobackupex --user=root --password=aolens /backup/
InnoDB: Error: log
file
.
/ib_logfile0
is of different size 5242880 bytes
InnoDB: than specified
in
the .cnf
file
50331648 bytes!
innobackupex: Error: The xtrabackup child process has died at
/usr/bin/innobackupex
line 2672.
|
1
2
3
4
5
|
[root@node2 ~]
# innobackupex --user=root --password=aolens /backup/
innobackupex: Backup created
in
directory
'/backup/2014-09-09_01-05-21'
innobackupex: MySQL binlog position: filename
'mysql-bin.000004'
, position 245
140909 01:05:24 innobackupex: Connection to database server closed
140909 01:05:24 innobackupex: completed OK!
|
1
2
3
4
5
|
[root@node2 ~]
# innobackupex --apply-log /backup/2014-09-09_01-05-21/ #提交未提交的数据
InnoDB: FTS optimize thread exiting.
InnoDB: Starting
shutdown
...
InnoDB: Shutdown completed; log sequence number 1602582
140909 02:50:01 innobackupex: completed OK!
|
1
2
3
4
5
6
|
[root@node2 data]
# innobackupex --copy-back /backup/2014-09-09_01-05-21/
innobackupex: Error: Original data directory
'/mydata/data'
is not empty! at
/usr/bin/innobackupex
line 2163.
#报错数据目录不为空!
[root@node2 data]
# rm -rf * #删除数据目录下的所有文件。
[root@node2 data]
# innobackupex --copy-back /backup/2014-09-09_01-05-21/
innobackupex: Finished copying back files.
140909 02:58:36 innobackupex: completed OK!
|
1
2
3
4
5
|
MariaDB [aolens]> create table tb2(
id
int);
MariaDB [aolens]> insert into tb2(
id
) values(111),(222);
[root@node2 ~]
# innobackupex --incremental /backup/ --incremental-basedir=/backup/2014-09-09_01-05-21/ #相对于完全备份做增量备份。
140909 03:49:46 innobackupex: Connection to database server closed
140909 03:49:46 innobackupex: completed OK!
|
1
2
3
4
5
6
7
8
9
|
MariaDB [(none)]> use aolens;
Database changed
MariaDB [aolens]> create table tb3(
id
int);
Query OK, 0 rows affected (0.01 sec)
MariaDB [aolens]> insert into tb3(
id
) values(888);
Query OK, 1 row affected (0.05 sec)
[root@node2 ~]
# innobackupex --incremental /backup/ --incremental-basedir=/backup/2014-09-09_03-49-40/ #这一次增量式相对于上次增量做增量备份。
140909 04:00:11 innobackupex: Connection to database server closed
140909 04:00:11 innobackupex: completed OK!
|
1
2
3
4
5
6
7
8
|
[root@node2 data]
# rm -rf * #删除数据目录下的文件,模拟数据目录坏掉、记住要将二级制分开存放。
[root@node2 data]
# innobackupex --apply-log redo-only /backup/2014-09-09_04-10-25/ #准备完全备份,只提交未提交的。不回滚。
[root@node2 data]
# innobackupex --apply-log --redo-only /backup/2014-09-09_01-05-21/ --incremental-dir=/backup/2014-09-09_03-49-40/ #准备第一个增量备份。
innobackupex: Copying
'/backup/2014-09-09_03-49-40/aolens/tb2.frm'
to
'/backup/2014-09-09_01-05-21/aolens/tb2.frm'
140909 04:18:17 innobackupex: completed OK!
[root@node2 data]
# innobackupex --apply-log --redo-only /backup/2014-09-09_01-05-21/ --incremental-dir=/backup/2014-09-09_04-00-08/ #合并第二个增量
innobackupex: Copying
'/backup/2014-09-09_04-00-08/aolens/tb2.frm'
to
'/backup/2014-09-09_01-05-21/aolens/tb2.frm'
140909 04:20:36 innobackupex: completed OK!
|
1
2
3
4
5
6
|
[root@node2 data]
# innobackupex --copy-back /backup/2014-09-09_01-05-21/
innobackupex: Finished copying back files.
140909 04:24:21 innobackupex: completed OK!
进入到数据目录下
[root@node2 data]
# chown -R mysql.mysql * #修改回复回来数据的属组属主。
service mysqld start
|