Поиск по этому блогу

пятница, 4 декабря 2015 г.

Extracting DB dump from archive with multiple DBs

Hello.

Today I'm going to explain how to extract mysql DB dump from a file with multiple DBs. Some time ago I used sed command to perform such tasks, but this method has some problems:

Let me show how to do this with sed:

cat/bzcat /path/to/file | sed -n '/^USE `DB_TO_RESTORE`/,/^USE /p'

to resotore TABLE_TO_RESTORE  table:
cat/bzcat /path/to/file | sed -n '/^DROP TABLE IF EXISTS `TABLE_TO_RESTORE`/,/^DROP TABLE IF EXISTS /p'

Or combine the commands above: =)

to restore DB_TO_RESTORE.TABLE_TO_RESTORE table:
cat/bzcat /path/to/file | sed -n '/^USE `DB_TO_RESTORE`/,/^USE /p' | | sed -n '/^DROP TABLE IF EXISTS `TABLE_TO_RESTORE`/,/^DROP TABLE IF EXISTS /p'

Problem with this method:
What about multiple mysql tables to restore with sed? Probably it is possible, but I'm not a sed hacker =)
What if /path/to/file is pretty big, sed will work until whole the file is processed, even if all needed data already restored...

To avoid this I created perl script that restores needed data and stops to process incoming data. Here you go: extractor.pl.

mysqldump commands will be printed to STDOUT, all information will be printed to STDERR.

Example:
cat dump.sql | extractor.pl DB table1 table2 > result.sql
db= DB, tables: table1 table2
DB # <- current="" db.="" nbsp="" p="">    table: XXX # <- current="" p="" table="">    table: XXXY
    table: ZZZZ
    table: DDD
    table: table1
    table: table2


Script stops working once it processed "DB" database.


Enjoy!






Комментариев нет:

Отправить комментарий