alanthing

Linux and other things

Dump Each MySQL Table to a File

This post originally featured on the Echo & Co. blog.

Here’s a one-liner to dump each table in a database to it’s own .sql file. Crack open your shell of choice and follow along.

Replace the USER, PASSWORD, and DBNAME values with your own. If you’re not running this to connect to a local database, add —host=domain.tld after each password to connect to your remote server.

1
for i in $(mysql -uUSER -p'PASSWORD' --batch --skip-column-names DBNAME -e'show tables;'); do mysqldump -uUSER -p'PASSWORD' DBNAME $i > DBNAME-$i.sql; done

Users, passwords, and database names are case sensitive, I made them uppercase here to call them out. Leave a comment if you have a question or notice a problem or typo.

Comments