Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backing Up Views with Mysql Dump

I want to back up only the Views with mysqldump.

Is this possible?

If so, how?

like image 964
Charles Faiga Avatar asked Nov 01 '09 20:11

Charles Faiga


People also ask

How do I backup a view in MySQL?

To generate the backup using mysqldump, 'Select' to dump the tables, 'Show View' for views, 'Trigger' for the triggers. If you are not using —single-transaction option, then 'Lock Tables' privileges must be granted to the user. -p [password]: The valid password of the MySQL user.

Is MySQL dump safe?

MySQL's unencrypted port is not secure. If you're running mysqldump on your VPS host, and only transferring the resulting dump file to your PC, then you can do this securely. If you can ssh to your VPS, you should be able to use scp too. This gives you the ability to transfer files securely.

What does MySQL dump do?

It dumps one or more MySQL databases for backup or transfer to another SQL server. The mysqldump command can also generate output in CSV, other delimited text, or XML format.


2 Answers

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).

like image 125
2 revs Avatar answered Oct 12 '22 14:10

2 revs


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' 
like image 35
olliiiver Avatar answered Oct 12 '22 16:10

olliiiver