Extracting a single database from a mysqldump
Some days ago a friend asked me how to extract a single database from a complete database dump. He created the dump using mysqldump and only wanted to restore a single database, without having go through >80k lines.
After a little thinking I came up with a little script that does the work:
I used grep to find the line of where the database is created and cut to extract the line number. Sed can be used to extract lines or a range of lines from a text file.
sed -n 1,10p file
This extracts lines 1 to 10 from a file. It's pretty amazing what sed can be used for..
Comments
Thank you for sharing such relevant topic with us. I really love all the great stuff you provide. Thanks again and keep it coming.
Buyviagraonline
http://sasfxdsa.blogspot.com/
Buyviagraonline!!!
http://www.love-charms.net
# Conrad's version (that actually works for Conrad)
# usage: ./grep_db.sh dump.sql dbname > output.sql
line=`grep -m 1 -n "Current Database: .$2" $1 | cut -d ":" -f 1`
next=`sed 1,${line}d $1|grep -m 1 -n "Current Database" | cut -d ":" -f 1`
end=$(($line + $next -1))
sed -n ${line},${end}p $1
#Thanks Fabian!
This is a very helpful script indeed! Thank you for sharing it
Work with saving database is very difficult process during which you should all time check the correctness information.
This work can do correctly just programmer and if he will spend some time for doing it.
the dump using mysqldump and only wanted to restore a single database, without having go through >80k lines.
This extracts lines 1 to 10 from a file. It's pretty amazing what sed can be used for..