当前位置:主页 > 数据库 > Mysql >

如何用mysqldump进行全量和时间点备份

时间:2020-12-19 11:24:39 | 栏目:Mysql | 点击:

mysqldump在mysql中用于逻辑备份,虽然速度不快,但非常灵活,有很多功能,灵活使用的化绝对是利器。

首先思考一个问题,mysql为什么要备份,主要还是数据安全性,比如主库挂了,数据不小心被删除了,所以全量备份非常重要。

是从主库还是副库进行全量备份呢?

1:从主库

主库比较重要,但其实备份的时候并不会影响数据库

mysqldump --host= --user= --password= --single-transaction --master-data=1 --flush-logs --databases >~/db.log

―single-transaction 参数能够报纸一致性读,不会锁表,也就是备份的时候不影响数据更新。

it dumps the consistent state of the database at the time when START TRANSACTION was issued without blocking any applications.

因为一致性读,能够保证coordinates点的位置,即使备份时间很久,也能得出正确的同步位置点。

While a ―single-transaction dump is in process, to ensure a valid dump file (correct table contents and binary log coordinates)

―master-data参数也很重要,导出的语句会包含CHANGE MASTER TO语句,包括备份语句同步到的二进制文件和位置点。

Use this option to dump a master replication server to produce a dump file that can be used to set upanother server as a slave of the master. It causes the dump output to include a CHANGE MASTER TO statement that indicates the binary log coordinates (file name and position) of the dumped server. These are the master server coordinates from which the slave should start replicating after you load the dump file into the slave.

―flush-logs会强制重新生成一个新的二进制文件,这样恢复的时候会比较方便。

2:从副库

感觉上从副库备份更安全。

mysqldump --host=--user= --password= --dump-slave=1 --flush-logs --apply-slave-statements --include-master-host-port --databases >~/db.log;

― dump-slave和―master-data参数很类似:

This option is similar to ―master-data except that it is used to dump a replication slave server to produce a dump file that can be used to set up another server as a slave that has the same master as the dumped server. It causes the dump output to include a CHANGE MASTER TO statement that indicates the binary log coordinates (file name and position) of the dumped slave's master. These are the master server coordinates from which the slave should start replicating.

记住一点它获取的是主库的bin log coordinates(不是备份库的)

―dump-slave causes the coordinates from the master to be used rather than those of the dumped server

dump出来的语句会包含 ― Position to start replication or point-in-time recovery from。

―apply-slave-statements会让dump语句中自动包含start和stop slave语句。―include-master-host-port包含主库的连接信息。

必须记住一点,即使有―single-transaction语句,―dump-slave也会暂停mysql同步,也就是备份库的数据是落后于主库的,所以一般自动化脚本在备份的时候会先摘除备份库。

This option causes mysqldump to stop the slave SQL thread before the dump and restart it again after.

3:如何进行时间点恢复

没有实战过,首先基于最近的一次全量备份进行恢复,然后将后续的binlog文件导入(如果这些文件还在的话),所以副库最好也备份binlog语句。

如果数据被误删除了,将备份点(―flush-logs发挥作用了)到今天凌晨的binlog语句导入进来,或者找到安全的binlog位置点进行恢复。至于如何跳过“危险语句”是比较难控制的。

您可能感兴趣的文章:

相关文章