I want to back up only the Views with mysqldump.
Is this possible?
If so, how?
Thanks for this - very useful.
One hiccup though - perhaps as I have a slightly convoluted set of views that reference other views etc:
I found that the "definer" user needs to exist and have the right permissions on the target schema, otherwise mysql will not generate the views that reference other views as it things definitions are insufficient.
In the generated:
/*!50013 DEFINER=<user>
@<host>
SQL SECURITY DEFINER */
--> ensure <user>
@<host>
is ok on your target instance, or replace this string with a user that does.
Thanks Thorstein
I modified Andomar's excellent answer to allow the database (and other settings) to only be specified once:
#!/bin/bash -e
mysql --skip-column-names --batch -e \
"select table_name from information_schema.views \
where table_schema = database()" $* |
xargs --max-args 1 mysqldump $*
I save this as mysql-dump-views.sh
and call it via:
$ mysql-dump-views.sh -u user -ppassword databasename >dumpfile.sql
NOTE: This answer from Ken moved from suggested edit to own answer.
here's a full command line example using a variant of the above
mysql -u username INFORMATION_SCHEMA
--skip-column-names --batch
-e "select table_name from tables where table_type = 'VIEW'
and table_schema = 'database'"
| xargs mysqldump -u username database
> views.sql
This extracts all of the view names via a query to the INFORMATION_SCHEMA database, then pipes them to xargs to formulate a mysqldump command. --skip-column-names and --batch are needed to make the output xargs friendly. This command line might get too long if you have a lot of views, in which case you'd want to add some sort of additional filter to the select (e.g. look for all views starting with a given character).
By backup, I'm assuming you mean just the definition without the data.
It seems that right now mysqldump doesn't distinguish between VIEWs and TABLEs, so perhaps the best thing to do is to either specify the VIEWs explicitly on the command line to mysqldump or figure out this list dynamically before mysqldump and then passing it down like before.
You can get all the VIEWs in a specific database using this query:
SHOW FULL TABLES WHERE table_type='view';
I would stick as closely as possible to the output of mysqldump like the OP asked, since it includes a slew of information about the view that can't be reconstructed with a simple query from the INFORMATION_SCHEMA.
This is how I create a deployment view script from my source database:
SOURCEDB="my_source_db"
mysql $SOURCEDB --skip-column-names -B -e \
"show full tables where table_type = 'view'" \
| awk '{print $1}' \
| xargs -I {} mysqldump $SOURCEDB {} > views.sql
Backing up views over multiple databases can be done by just using information_schema:
mysql --skip-column-names --batch -e 'select CONCAT("DROP TABLE IF EXISTS ", TABLE_SCHEMA, ".", TABLE_NAME, "; CREATE OR REPLACE VIEW ", TABLE_SCHEMA, ".", TABLE_NAME, " AS ", VIEW_DEFINITION, "; ") table_name from information_schema.views'