Feb
6
MySQL 5 has introduced some new interesting features, like stored procedures and triggers.
I will show in this small post how we can backup and restore these components using mysqldump.
mysqldump will backup by default all the triggers but NOT the stored procedures/functions. There are 2 mysqldump parameters that control this behavior:
–routines – FALSE by default
–triggers – TRUE by default
This means that if you want to include in an existing backup script also the triggers and stored procedures you only need to add the –routines command line parameter:
mysqldump
mysqldump –routines –no-create-info –no-data –no-create-db –skip-opt
and this will save only the procedures/functions/triggers of the
mysql
========== basic dump ============
shell> mysqldump –opt db_name > backup-file.sql
You can read the dump file back into the server like this:
shell> mysql db_name < backup-file.sql
Or like this:
shell> mysql -e “source /path-to-backup/backup-file.sql” db_name
mysqldump is also very useful for populating databases by copying data from one MySQL server to another:
shell> mysqldump –opt db_name | mysql –host=remote_host -C db_name
It is possible to dump several databases with one command:
shell> mysqldump –databases db_name1 db_name2 … > my_databases.sql
If you want to dump all databases, use the –all-databases option:
shell> mysqldump –all-databases > all_databases.sql