Mysql备份方案分为以下三种
1.mysqldump+binlog:(推荐)
完全备份,通过备份二进制日志实现增量备份
2.xtrabckup:
对InnoDB:热备,支持完全备份和增量备份
对MyISAM:温备,支持完全备份
3.lvm2快照+binlog:几乎热备,物理备份
1.mysqldump+binlog:(推荐)
完全备份,通过备份二进制日志实现增量备份
2.xtrabckup:
对InnoDB:热备,支持完全备份和增量备份
对MyISAM:温备,支持完全备份
3.lvm2快照+binlog:几乎热备,物理备份
mysql备份工具
mysqldump:适用于所有存储引擎,适用于温备,能实现完全备份,部分备份;对InnoDB存储支持热备;
cp,tar等文件系统工具:物理备份,适用于所有引擎;用于冷备,能实现完全备份,部分备份;
lvm2的快照:几乎热备;借助于文件系统工具实现物理备份;
mysqlhotcopy:几乎热备;仅适用于 MyISAM存储引擎;但事实上mysqlhotcopy只支持冷备
先来介绍一下mysqldump+binlog的语法格式
mysqldump [options] database [tables]:备份单个库,或库指定的一个或多个表
mysqldump [options] --database [options] DB1 [DB2 DB3...]: 备份一个或多个库
mysqldump [options] --all-databases [options] :备份所有
实验:使用mysqldump+binlog备份与恢复
1、准备备份目录
[root@@mysql-master~]#mkdir -pv /backup/binlog
mkdir: created directory ‘/backup’
mkdir: created directory ‘/backup/binlog’
[root@@mysql-master~]#chown -R mysql.mysql /backup/
2、准备备份数据库及表
MariaDB [(none)]> create database class;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> use class;
Database changed
MariaDB [class]> create table tb(id int);
Query OK, 0 rows affected (0.10 sec)
3、进行完整备份
mysqldump -p123456 --all-databases --lock-all-tables --flush-log --master-data=2 > /backup/`date +%F_%T`-all.sql;
说明:这里的密码是之前装mariadb的时候设置的,虽然现在是重新装的mariadb但是只要你装过mariadb,你对它所有的设置都在
/var/lib/mysql下有记录,当时对数据库设置了密码,这个密码就存放在/var/lib/mysql/mysql下的user表中;如果你现在不想要密码了
你就把/var/lib/mysql下的大小全部删除,当你再次重启mariadb时这个目录下就会自动生成mariadb所需要的文件,不需要的就没有了,是
不是很神奇没有密码在完整备份的时候就是mysqldump --all-databases --lock-all-tables --flush-log --master-data=2 >
/backup/`date +%F_%T`-all.sql;
4、向表中插入数据
MariaDB [(none)]> show master status\G;
MariaDB [class]> create table tb1(id int);
Query OK, 0 rows affected (0.29 sec)
MariaDB [class]> insert into tb1 values (10),(20),(30);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [class]> select * from tb1;
+------+
| id |
+------+
| 10 |
| 20 |
| 30 |
+------+
3 rows in set (0.00 sec)
MariaDB [class]>
5、进行增量备份,备份二进制日志
先查看一下二进制日志文件中的position位置,以便在进行增量恢复的时候指定start position和stop position的位置
[root@mysql-master /var/lib/mysql]#ls
aria_log.00000001 class ib_logfile0 mysql mysql-bin.000002 mysql-bin.000004 mysql.sock test
aria_log_control ibdata1 ib_logfile1 mysql-bin.000001 mysql-bin.000003 mysql-bin.index performance_schema
[root@mysql-master /var/lib/mysql]#mysqlbinlog mysql-bin.000004
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#171226 21:27:16 server id 73 end_log_pos 245 Start: binlog v 4, server v 5.5.52-MariaDB created 171226 21:27:16
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
NE5CWg9JAAAA8QAAAPUAAAABAAQANS41LjUyLU1hcmlhREIAbG9nAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAYE0+vw==
'/*!*/;
# at 245
#171226 21:30:15 server id 73 end_log_pos 333 Query thread_id=18 exec_time=0 error_code=0
use `class`/*!*/;
SET TIMESTAMP=1514295015/*!*/;
SET @@session.pseudo_thread_id=18/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create table tb1(id int)
/*!*/;
# at 333
#171226 21:30:18 server id 73 end_log_pos 402 Query thread_id=18 exec_time=0 error_code=0
SET TIMESTAMP=1514295018/*!*/;
BEGIN
/*!*/;
# at 402
#171226 21:30:18 server id 73 end_log_pos 503 Query thread_id=18 exec_time=0 error_code=0
SET TIMESTAMP=1514295018/*!*/;
insert into tb1 values (10),(20),(30)
/*!*/;
# at 503
#171226 21:30:18 server id 73 end_log_pos 530 Xid = 335
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@mysql-master /var/lib/mysql]#mysqlbinlog --start-position=333 --stop-position=530 /var/lib/mysql/mysql-bin.000004 > /backup/binlog-`date +%F_%T`.sql
注意:position一定要指对位置
6、继续插入数据,在没备份的情况下删除数据库,模拟误操作
MariaDB [(none)]> use class;
Database changed
MariaDB [class]> insert into tb1 values (40),(50);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [class]> drop database class;
Query OK, 2 rows affected (0.02 sec)
MariaDB [(none)]>
此时查看数据库发现class就没有了
7、数据恢复,由于最后我们没有备份就删除了数据库,所以我们首先 需要保护最后的二进制日志,查看删除操作之前的position值
mysqlbinlog /mydata/data/mysql-bin.000004
[root@mysql-master ~]#mysqlbinlog /var/lib/mysql/mysql-bin.000004
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#171226 21:27:16 server id 73 end_log_pos 245 Start: binlog v 4, server v 5.5.52-MariaDB created 171226 21:27:16
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
NE5CWg9JAAAA8QAAAPUAAAABAAQANS41LjUyLU1hcmlhREIAbG9nAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAYE0+vw==
'/*!*/;
# at 245
#171226 21:30:15 server id 73 end_log_pos 333 Query thread_id=18 exec_time=0 error_code=0
use `class`/*!*/;
SET TIMESTAMP=1514295015/*!*/;
SET @@session.pseudo_thread_id=18/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create table tb1(id int)
/*!*/;
# at 333
#171226 21:30:18 server id 73 end_log_pos 402 Query thread_id=18 exec_time=0 error_code=0
SET TIMESTAMP=1514295018/*!*/;
BEGIN
/*!*/;
# at 402
#171226 21:30:18 server id 73 end_log_pos 503 Query thread_id=18 exec_time=0 error_code=0
SET TIMESTAMP=1514295018/*!*/;
insert into tb1 values (10),(20),(30)
/*!*/;
# at 503
#171226 21:30:18 server id 73 end_log_pos 530 Xid = 335
COMMIT/*!*/;
# at 530
#171226 21:52:48 server id 73 end_log_pos 599 Query thread_id=43 exec_time=0 error_code=0
SET TIMESTAMP=1514296368/*!*/;
BEGIN
/*!*/;
# at 599
#171226 21:52:48 server id 73 end_log_pos 695 Query thread_id=43 exec_time=0 error_code=0
SET TIMESTAMP=1514296368/*!*/;
insert into tb1 values (40),(50)
/*!*/;
# at 695
#171226 21:52:48 server id 73 end_log_pos 722 Xid = 343
COMMIT/*!*/;
# at 722
#171226 21:53:03 server id 73 end_log_pos 805 Query thread_id=43 exec_time=0 error_code=0
SET TIMESTAMP=1514296383/*!*/;
drop database class
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
8、将最后操作的二进制日志备份
[root@mysql-master ~]#mysqlbinlog --stop-position=722 /var/lib/mysql/mysql-bin.000004 > /backup/binlog-`date +%F_%T`.sql
9、导入之前的所有备份
[root@mysql-master /backup]#ls
2017-12-26_21:27:16-all.sql binlog binlog-2017-12-26_21:49:12.sql
binlog-2017-12-26_21:57:42.sql
[root@mysql-master /backup]#mysql < /backup/2017-12-26_21:27:16-all.sql
[root@mysql-master /backup]#mysql < binlog-2017-12-26_21:49:12.sql
[root@mysql-master /backup]#mysql < binlog-2017-12-26_21:57:42.sql
10、查看数据库及数据
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| class |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [test]> use class;
Database changed
MariaDB [class]> select * from class.tb1;
+------+
| id |
+------+
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
+------+
5 rows in set (0.00 sec)
MariaDB [class]>
到此数据成功全部恢复!
下一篇博客将介绍另一种数据库恢复
工具xtrabackup的使用