12

How can I dump each mysql table separately with mysqldump?

Background: I want to track those dumps with git and using the pre-commit hook

Example: I have a schema with 10 tables (table1 - table10). now I want a file per table: table1.sql table2.sql ...

So how gonna this work?

Snd why stackoverflow don't like my question?

peterh
  • 11,875
  • 18
  • 85
  • 108
Nils Rückmann
  • 169
  • 1
  • 1
  • 6

2 Answers2

35

This should work in a shell:

for x in `mysql --skip-column-names -u [username] -p[password] [dbname] -e 'show tables;'`; do
     mysqldump -u [username] -p[password] [db name] $x > "$x.sql"
done
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Edson Medina
  • 9,862
  • 3
  • 40
  • 51
1
mysqldump -t -u [username] -p test mytable

will dump the table 'mytable' from the database 'test'.

If you want to automate the procedure, you will need to write a script, that selects the table_names from the schema for you and apply the operation above for each table. You can automate the git operations as well.

hovanessyan
  • 30,580
  • 6
  • 55
  • 83