MySQL: How do I dump all tables in a database into separate files?

There have been numerous occasions where I needed to make backups of individual tables from selected database. Usually I can achieve this by typing:

mysqldump database_name table1 > table1.sql
mysqldump database_name table2 > table2.sql

This could be very painful if you have 10’s or 100’s of tables. Until today, I never ran into a situation where I had to deal with dumping more than few tables at a time. Today I had to do a dump of 181 tables. I was not going to sit there and type in that command with 181 table names. It is not just time consuming but it is also stupid. So I wrote this script to help me with this task. We still use mysqldump command as described above, except we do it programmatically to make it easier on us:

#!/bin/bash
db=$1
if [ "$db" = "" ]; then
echo "Usage: $0 db_name"
exit 1
fi
mkdir $$
cd $$
clear
for table in `mysql $db -e 'show tables' | egrep -v 'Tables_in_' `; do
echo "Dumping $table"
mysqldump --opt -Q $db $table > $table.sql
done
if [ "$table" = "" ]; then
echo "No tables found in db: $db"
fi

You can also compress your files by adding bzip2, zip or any other compression commands after mysqldump command. Here is the same script with bzip2 command added:
#!/bin/bash
db=$1
if [ "$db" = "" ]; then
echo "Usage: $0 db_name"
exit 1
fi
mkdir $$
cd $$
clear
for table in `mysql $db -e 'show tables' | egrep -v 'Tables_in_' `; do
echo "Dumping $table"
mysqldump --opt -Q $db $table > $table.sql
bzip2 $table.sql
done
if [ "$table" = "" ]; then
echo "No tables found in db: $db"
fi

I do not recommend doing compression on a production server since most compression program put descent amount of load on the server. Also note that this will delay your dump considerably. You may also want to use different parameters for running mysqldump. Type man mysqldump in your shell to read more.

Related posts:

————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

20 thoughts on “MySQL: How do I dump all tables in a database into separate files?

  1. CrazyToon Post author

    I have in past. I didn’t like the fact I had to install perl modules to get it working. That may not be the case on all systems but since all our systems have bare minimum installed OS, it takes longer for me to get things working whereas I can just run this script and be done.

    Thanks for posting the link though. I am sure other people would appreciate it.

  2. Pingback: MySQL: How do I import individual table dump files in to MySQL using shell script? | Technology: Learn and Share

  3. Pingback: MySQL: How do you set up master/slave replication in MySQL? (CentOS, RHEL, Fedora) | Technology: Learn and Share

  4. Frank Daley

    Thanks for the script. Has been very very helpful.

    One addition I am trying to implement is to include the column names in the output TXT files.

    Is there a way to do this?

  5. Andre Bell

    Thanks for the script. Works fine for dumping the tables to files.

    Is there also a way to dump the records from the tables into separate files? I’ve been trying for months to figure out how to do this, with no luck. Seems should be a php script already in existence to export each record from a table to separate text files. But after hundreds and hundreds of searches I can’t find anything like that works.

    No real care of the naming convention, whether named according to the id field, primary key, or by date and time or some other method isn’t real important. Just as long as the data goes to separate files. Any clues how to do that?

    Thanks

  6. Andre Bell

    Sunny, thanks for posting that script. I didn’t mean for you to go to that trouble, I was curious if a way to do that had already existed 😐

    In my case, a 3rd party app required each record to be a separate text file to be able to import the records into wordpress’ posting area.

    If I were going for all 200,000 records I would have looked for a script that would directly import .sql files into wordpress’ posting area. But none exists so this 3rd party app seemed to be the next best thing.

    Thanks again for the script.

  7. Neil Telford

    Another poster asked this, but assume I dump out 5000 different tables, how do you re-import all these files without having to give a password each time?

  8. Sunny Walia Post author

    You can put a file in your home directory called .my.cnf and put following content in it:
    [mysql]
    user=username
    pass=password

  9. Pingback: 12 Days of Jesus Junk – Day 3 – Avoid Wipeouts | Heal Your Church WebSite

  10. backtick

    it would be great to see an extensive blog post that introduces shell scripting, linux etc and explains the script that you posted above.

    I have no idea what it does or how to make it run…but i definitely need to use it.

    If you have the time it would be great to see a whole introduction to linux, from the ground up …all with the ultimate goal of leaving the reader with a clear understanding of what each character in that script is doing.

    i live in hope. I don’t demand much do i? ;o)

  11. Pingback: links for 2010-11-27 | john keegan dot org

  12. Madeline Murrell

    Hi there crazytoon.com

    You want quality traffic that Buys From You or Converts into Highly Responsive Subscribers ?
    Then Buy High Quality Solo Ad

    I’ve been in the traffic space for over 5 years and my traffic is:
    * Scalable – I can send anywhere from 100 – 5,000 clicks to your offers.
    * Pre-Qualified – Only the best of the best leads see your offer.
    * Responsive – We regularly get CRAZY results for our vendors (read our reviews).
    * Unsaturated – We use a unique marketing mix to get our leads !!
    * I have a unique system that sorts and optimizes the traffic that is delivered to you.
    * My system delivered unique and highly qualified traffic to your offers ANYTIME YOU WANT!
    * And it’s always going to be fresh, I wouldn’t let you buy from me if it is not.
    * The traffic that you get with my solos is not shared with thousands of other marketers.

    We help you grow your business because we understand that our business growth directly depends on our ability to GROW YOUR BUSINESS.
    That is the only reason we have flourished for over 5 years whereas other solo ad vendors struggle to get sales.
    We maintain quality over quantity.

    IF YOU’RE INTERESTED, CONTACT ME ==> [email protected]

    Regards,
    Axyy

Leave a Reply

Your email address will not be published. Required fields are marked *