MySQL Database Backup and Restore via Command Line (Copy-and-pasteable)

Posted in Notes on 5 February 2016

Putting this together because I always end up checking online for the correct bash commands for Linux. These commands rely on your knowing for sure which database is which and you should always double check to be sure.

Export

First step is to go to use cd to change directory to the one in which you want the SQL Dump to appear. The dump will consist of a text file and all the database structure and content included.

cd var/www/example.com

If you want to double check the contents of the directory you are in then simply enter ls and a list of directories and files will be returned.

ls

When you’re happy you’re in the right directory, we can export the file. In this instance I have a user name (user123) after the -uand the database is database_live and the initial -p means bash will ask me for the password.

mysqldump -p -uuser123 database_live > mydumpfile.sql

Hit enter, enter the password and after some serious thinking (depending on the size of the database) you’ll be able to enter the next command. Again, entering ls will allow you to verify there is a file called mydumpfile.sql in the directory.

Import

The next step is then to import the database into your other database. It should be noted that importing the previous dump will with default configuration wipe any existing data during the import into the second database.

In the instance below I have a user name (uuser456 ) after the -uand the database is database_live and the initial -p means bash will ask me for the password.

mysql -p -uuser456 database_backup < mydumpfile.sql

Hit enter, enter the password for the user and again it will hang while the details are imported.

Related Notes Posts

February 2024

Animated SVGs (Scalable Vector Graphics)

Animated SVGs, Scalable Vector Graphics, are increasingly popular choices for adding dynamic elements to websites. Their scalability, lightweight nature, and flexibility make them attractive options... Continue reading

December 2023

Using Scalable Vector Graphics (SVGs)

Scalable Vector Graphics (SVGs) have revolutionised website design, offering unparalleled flexibility, scalability, and interactivity. As versatile graphic elements, SVGs can enhance the visual appeal and... Continue reading

September 2023

Designing websites for accessibility

In the ever-evolving landscape of web design, the balancing act between accessibility and aesthetic appeal remains a crucial consideration. As the digital realm becomes increasingly... Continue reading

More Notes Posts