Restoring MySQL dumps
Now you've automated the running of MySQL backups with our wonderful solution [:-)]....
But now... you need to restore a specifical database from an entire mysqldump.sql file.
This solution is a bit tricky, but it's simple and uses standard commands.
First let's say we need to restore the database MyDbLost and it is in the middle of the dump file. You don't know where it is so... you need first a few variables.
With
wc -l mysqldump.sqlyou'll get the number of lines of the file. The output should look like:
33815 mysqldump.sqlWrite down the 33815 number. Then you have to identify where your MyDbLost begins:
grep -in "USE MyDbLost;" mysqldump.sqlAnd you get another line number:
5913:USE MyDbLost ;If you don't get any result please try the following:
grep -in "USE \`MyDbLost\`;" mysqldump.sqlNow it's time to calc the lines you have to throw away from the head of your file:
33815 - 5913 + 10 = 27912
(the 10 more lines are necessary to get a little "air" between cuts)
then you can type in:
tail -n 27912 mysqldump.sql > mysqldump.1.sqland you will get a new file called mysqldump.1.sql which starts approximately with the declarations of your database.
Now you have to trim out the queue of your file. You must identify where a new database starts:
grep -n "USE " mysqldump.1.sql | moreyou'll get an output similar to:
10:USE MyDbLost ;1339:USE MyDb1 ;
1467:USE MyDb2 ;
Take the number of lines in which is specified the second database and type
head -n 1339 mysqldump.1.sql > mysqldump.2.sqlWith a little edit on mysqldump.2.sql you'll have exactly what you're looking for: the MyDbLost schema and datas!