简体   繁体   中英

Recover database from many mysql bin log files

I have some MYSQL binary log files as well as the as the binary index file ( https://dev.mysql.com/doc/refman/5.6/en/point-in-time-recovery.html ).

The files look like so: log-bin.000001 , log-bin.000002 , log-bin.000003 .

I also have a log-bin.index file that looks like so:

/path/to/mysql-bin-dump-logs/log-bin.000001
/path/to/mysql-bin-dump-logs/log-bin.000002
/path/to/mysql-bin-dump-logs/log-bin.000003

According to the docs, the way to recover my database using these log files is like so:

mysqlbinlog log-bin.000001 log-bin.000002 log-bin.000003 | mysql -u root -p

This is not so bad when recovering a small number of files, but a major inconvenience when recovering from many files (eg 1000 log files).

How can I recover my database without explicitly stating every file?

Make backups periodically.

You only have to replay binary logs back to the one that was current at the time you made the backup.

Backup tools have an option to record the binary log file and position that was current at the time the backup runs. For example:

mysqldump --master-data=1 ...

Using only binary logs to recover your database is inefficient, especially if your history of database changes includes some updates and deletes, alter table, drop table, etc. Because the binary log replays all changes, even those that later get reversed or deleted.

If you use database backups, it only records the current state of data, skipping all the hundreds of days of changes that were made prior to the backup.

Restoring a backup is a lot faster, and will not require you to keep thousands of binary log files forever.


To answer your question literally—how to run mysqlbinlog against 1000 files conveniently—you could use find and xargs :

find . -name 'log-bin.[0-9]*' | sort | xargs mysqlbinlog | mysql -u root -p

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM