- Change a Password for MySQL on Linux via Command Line
- Create a MySQL Database on Linux via Command Line
- Select a MySQL Database on Linux via Command Line
- 3 Tips For Working With a MySQL Database!
- How to Back Up And Restore MySQL Databases From The Command Line
Next Series: MySQL via Command Line 102: Basic User InteractionWhile automated backups are important, sometimes you just want to take a MySQL dump of the database prior to making a change to your site. When modifying files in Linux, you can simply copy a file to another name in order to make a new copy. In this tutorial, we will show you how to create a backup of your database (or multiple databases) and also how to restore a backup from either command line or cPanel.
Backing Up a Single DatabaseIf you want to back up a single database, you merely create the “dump” (aka make a backup) and send the output of the “mysqldump” command into a .sql file. Don’t worry, this command doesn’t affect the database in any way; It merely makes a copy of the database.
mysqldump database_name > database_name.sql
Backing Up Multiple DatabasesMultiple databases can be backed up at the same time using the same “mysqldump” command:
mysqldump --databases database_one database_two > two_databases.sqlIn the command above, database_one is the name of the first database to be backed up, and database_two is the name of the second. This command will incorporate both databases into a single database.
Backing Up All DatabasesIt is also simple to back up all of the databases on a server:
mysqldump --all-databases > all_databases.sqlAgain, this will add all databases into a single database .sql file.
Restoring a Backup
Restoring a Database Via Command LineSince the dump files are just data preceded by SQL commands, you can restore the database backup by telling MySQL to run the commands in it and put the data back into the proper database.
mysql database_name < database_name.sqlIn the code above, database_name is the name of the database you want to restore to, and database_name.sql is the name of the backup file .sql file you are restoring from. If you are trying to restore a single database from a dump of all the databases, you have to let MySQL know like this:
mysql --one-database database_name < all_databases.sqlThis command pulls the original “database_name” .sql dump from the all_databases.sql mysql dump and restores it into its own original database name.
Restoring Databases From cPanel Backups
- Log into cPanel by typing https://yourhostname/cpanel/ into your browser.
- In the Files section, click on the Backups icon.
- Under Partial Backups > Restore a MySQL Database Backup, click on the Browse button. (If you followed the steps in the above section, Creating A Backup you’ll be able to click the Browse button to find a .sql file in your computer.)
- In the popup that appears, navigate to the appropriate destination and select the backup file (.sql) you intend to use.
- Click Open.
- Click Upload.