Extracting a Database From a mysqldump File

If you make full databases backup in one single file, you could need sometime to restore only one db from that dump file.

Here you are a easy way to extract it:

grep -n "Current Database: " mysqldumpfile.sql

19:– Current Database: `database1`
3546:– Current Database: `dabatase2`
6127:– Current Database: `database3`
6491:– Current Database: `database4`
10001:– Current Database: `database5`
10371:– Current Database: `database6`
11626:– Current Database: `database7`
11634:– Current Database: `database8`
16577:– Current Database: `database9`
18591:– Current Database: `database10`
18687:– Current Database: `mysql`
19336:– Current Database: `database11`
19589:– Current Database: `database12`
20278:– Current Database: `database13`
22557:– Current Database: `database14`
22961:– Current Database: `database15`
23320:– Current Database: `database16`
23719:– Current Database: `database17`

The previous output show you the database name and in which line it start. You only have to take the lines between that and the next database start minus one. If we want to extract database9:

sed -n '16577,18590p' mysqldumpfile.sql > database9.sql

With that you will get a dump only with database9 data.

Could be you don’t want to extract one database from the dump file, but you want all databases but one. The process it is the same, you have to know where each database start, and run the sed command but this time, it will be everything but the indicated. Let say we want to extract everything from the mysqldump file but mysql database:

sed -n '18687,19335!p' mysqldumpfile.sql > mysqldumpfilewithoutmysqldb.sql