Backup Commands
How to Backup MySQL Database?
To backup of MySQL database/databases, the database must exist in MySQL Instance and ID which is using to backup, must have access to it.
The format of the command would be:
# mysqldump -u [username] –p[password] [database_name] > [dump_file.sql]How to Backup a Single MySQL Database?
The parameters of the said command as follows.
[username] : A valid MySQL username.
[password] : A valid MySQL password for the user.
[database_name] : A valid Database name you want to take backup.
[dump_file.sql] : The name of backup dump file you want to generate.
To take a backup of single database, use the command as follows. The command will dump database [testdb] structure with data on to a single dump file called testdb.sql.
# mysqldump -uroot -p testdb > testdb_file.sqlHow to Backup Multiple MySQL Databases?
If you want to take backup of multiple databases, run the following command. The following example command takes a backup of databases [testdb, sysdb] structure and data in to a single file called testdb_sysdb.sql.
# mysqldump -uroot -p --databases testdb sysdb > testdb_sysdb.sqlHow to Backup All MySQL Databases?
If you want to take backup of all databases, then use the following command with option –all-database. The following command takes the backup of all databases with their structure and data into a file called all-databases.sql.
# mysqldump -u root -p --all-databases > all-databases.sqlHow to Backup MySQL Database Structure Only?
If you only want the backup of database structure without data, then use the option –no-data in the command. The below command exports database [testdb] Structure into a file testdb_structure.sql.
# mysqldump -u root -p --no-data testdb > testdb_structure.sqlHow to Backup MySQL Database Data Only?
To backup database Data only without structure, then use the option –no-create-info with the command. This command takes the database [testdb] Data into a file testdb_data.sql.
# mysqldump -u root -p --no-create-db --no-create-info testdb > testdb_data.sqlHow to Backup Single Table of Database?
With the below command you can take backup of single table or certain tables of your database. For example, the following command only take backup of wp_posts table from the database testdb.
# mysqldump -u root -p testdb tbl_test > testdb_test.sqlHow to Backup Multiple Tables of Database?
If you want to take backup of multiple or certain tables from the database, then separate each table with space.
# mysqldump -u root -p testdb tbl_test tbl_emp > testdb_tbl.sqlHow to Backup Remote MySQL Database?
The below command takes the backup of remote server [192.10.92.6] database [testdb] into a local server.
# mysqldump -h 192.10.92.6 -u root -p testdb > testdb.sql
Restore Commands
How to Restore MySQL Database?
In the above tutorial we have seen the how to take the backup of databases, tables, structures and data only, now we will see how to restore them using following format.
# # mysql -u [username] –p[password] [database_name] < [dump_file.sql]How to Restore Single MySQL Database?
To restore a database, you must create an empty database on the target machine and restore the database using msyql command. For example the following command will restore the testdb.sql file to the testdb database.
# mysql -u root -p testdb < testdb.sqlIf you want to restore a database that already exist on targeted machine, then you will need to use the mysqlimport command.
# mysqlimport -u root -p testdb < testdb.sql
In the same way you can also restore database tables, structures and data.