Friday, July 19, 2013

Taking Backup of MySQL Database with stored procedures and functions.

mysqldump is a utility to take a backup of MySQL database. With MySQL 5 new features like stored procedures and triggers were introduced. mysqldump by default backups only the triggers and not the stored procedures or functions. This behaviour can however be controlled by the following two parameters:

1. routines (--routines)
2. triggers (--triggers)

routines option is by default false while triggers option is by default true.

An example to take a backup of your database along with stored procedures, functions and triggers:
    # mysqldump --routines -u 'username' -p'password' > mysqldummpfile.sql

Let’s assume we want to backup only the stored procedures,functions and triggers and not the mysql tables and data , then we should run something like this:

   # mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt -u 'username' -p'password' > mysqldumpwithoutanytablesanddata.sql

This will backup only the procedures triggers and functions of the Database.

For importing this backup procedure remains the same (irrespective of whether the backup was taken with or without routines):

   mysql -u 'username' -p'password' < mysqldumpwithoutanytablesanddata.sql