Backup prefixed tables in MySQL without PHP

Just a quick note which took me some time and advice asking to find out. I have a database with two drupal installations separated by a table prefix, say drupal1_ and drupal2_. Lets call my database simply myDB.

If I'd like to backup only certain tables to an SQL file using mysqldump, I'd have to list all the table names separately. This is not nice at least since module installations and upgrades might add prefixed tables to the database dynamically over time. The other catch is that I want to do this without PHP.

One solution which works for me to backup only drupal1_ prefixed tables is to run the following script on the command line (in one line):

mysql -u myUsername -p myPassword myDB -e "show tables like 'drupal1_%';"
| sed '1d' 
| xargs mysqldump -u myUsername -p myPassword myBD 
> drupal1_tables_dump.sql

If you have a better solution feel free to comment. I usually leave out the myPassword part and enter the password (twice) when the script asks for it.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options