新建一个lvm磁盘,这里我建的lv为mydatalv,挂载到了/data下
1
2
3
4
5
6
|
[root@localhost ~] # lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mydatalv mydata -wi-ao---- 1.00g
[root@localhost ~] # df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/mydata-mydatalv 976M 2.6M 907M 1% /data
|
将原数据库文件复制到/data目录下
[root@localhost ~]# cp -a /var/lib/mysql /data/
修改配置文件,将mysql数据库文件放在lvm盘中,二进制文件放在非lvm盘的/var/lib/mysql/目录下
1
2
3
4
5
6
7
8
|
[root@ns1 ~] # vim /etc/my.cnf
[mysqld]
log_bin= /var/lib/mysql/mysql-bin
datadir= /data/mysql
[root@localhost ~] # service mariadb restart
[root@localhost ~] # ls /data/mysql
aria_log.00000001 ibdata1 ib_logfile1 mysql-bin.000001 mysql-bin.000003 performance_schema
aria_log_control ib_logfile0 mysql mysql-bin.000002 mysql-bin.index test
|
可以看到重启后数据库文件已存放在了/data/mysql目录中了
对mysql进行锁表备份
1
2
3
4
5
|
[root@localhost ~] # mysql -e 'flush tables with read lock;' 锁表
[root@localhost ~] # mysql -e 'flush logs;' 对日志进行滚动,
[root@localhost ~] # mysql -e 'show master status;' > /root/back.$(date +%F+%T)
[root@localhost ~] # ls
back.2016-07-13+10:14:29
|
对lv创建快照
[root@localhost ~]# lvcreate -L 1G -n mysqlback -p r -s /dev/mydata/mydatalv
释放锁
[root@localhost ~]# mysql -e 'unlock tables;'
在别的磁盘上创建备份目录,只读挂载快照后备份至备份目录
1
2
3
|
[root@localhost ~] # mkdir /myback
[root@localhost ~] # mount -r /dev/mydata/mysqlback /mnt
[root@localhost ~] # cp -a /mnt/mysql /myback
|
修改表内容,然后删除掉数据库文件内容即/data/mysql中的内容
1
2
3
4
|
[root@localhost ~] # mysql
MariaDB [hellodb]> use hellodb;
MariaDB [hellodb]> insert into classes (class,numofstu) values ( 'xxoo' ,39);
[root@localhost ~] # rm -rf /data/*
|
修改配置文件中二进制日志和数据库文件的位置
1
2
3
4
|
[root@localhost ~] # vim /etc/my.cnf
[mysqld]
log_bin= /data/mysql/mysql-bin
datadir= /data/mysql
|
利用/myback/中的内容还原
1
2
|
[root@localhost ~] # cp -a /myback/* /data/
[root@localhost ~] # service mariadb restart
|
利用二进制日志还原快照后的操作,由下面这个文件来查看快照执行时二进制日志的位置
1
2
3
|
[root@localhost ~] # cat back.2016-07-13+10\:14\:29
File Position Binlog_Do_DB Binlog_Ignore_DB
mysql-bin.000014 245
|
将000014中245之后的操作做成sql文件,进行还原
1
2
|
[root@localhost ~] # mysqlbinlog --start-position=245 /var/lib/mysql/mysql-bin.000014 > binlog.sql
[root@localhost ~] # mysql < /root/binlog.sql
|
查看恢复情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@localhost ~] # mysql
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> select * from classes;
+---------+----------------+----------+
| ClassID | Class | NumOfStu |
+---------+----------------+----------+
| 1 | Shaolin Pai | 10 |
| 2 | Emei Pai | 7 |
| 3 | QingCheng Pai | 11 |
| 4 | Wudang Pai | 12 |
| 5 | Riyue Shenjiao | 31 |
| 6 | Lianshan Pai | 27 |
| 7 | Ming Jiao | 27 |
| 8 | Xiaoyao Pai | 15 |
| 9 | xxoo | 39 |
+---------+----------------+----------+
9 rows in set (0.00 sec)
|