Backup MySql database

Following code can be used to backup MySql database.

In code, we are using following words.
[username] Your database username
[pass] The password for your database ( there is no space between -p and the password)
[dbname] The name of your database
[backupfile.sql] The filename for your database backup
[–opt] The mysqldump option

// 1. take backup of a database
mysqldump -u [username] -p[pass] [dbname] > [backupfile.sql]

// 2. take backup of multiple database
mysqldump -u [username] -p[pass] [dbname1] [dbname2] [dbname3] > [backupfile.sql]

// 3. take backup of all database
mysqldump -u [username] -p[pass] –all-databases > [backupfile.sql]

// 4. backup specific table of a database
mysqldump -u [username] -p[pass] [dbname] [table_name] > [backupfile.sql]

// 5. To restore databases just use 'mysql' instead of 'mysqldump' and '<' instead of '>' 
mysql -u [username] -p[pass] [dbname] < [dump_file.sql]

// 6. To compress dump, use gzip at the end, e.g
$cmd = 'mysqldump -u '.$db_user.' -h '.$db_host.' --password='.$db_pass.' '.$db_name.' | gzip > ' .$dump_file_name;

// 7. To extract this zipped dump, use gunzip.
$ gunzip [backupfile.sql.gz]

// 8. To restore compressed backup files you can do the following:
gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]

// 9. If you need to restore a database that already exists, you'll need to use mysqlimport command. The syntax for mysqlimport is as follows:
mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]