Home > Help Center > Shared Hosting > Databases > WordPress Database Backup and Restore through SSH

WordPress Database Backup and Restore through SSH

In this tutorial, we'll show you how to create a database archive and how to recover the database from the same archive.

All these actions will be executed from the command-line interface.

We connect through SSH to our hosting using PuTTY.

We navigate to our WordPress site directory and list its contents:

[~]# cd public_html/hostpulse-demo/
[~/public_html/hostpulse-demo]# ls –la

The content of Wordpress site's directory

Open wp-config.php with the nano (or any other) editor:

The contents of the wp-config.php file

Copy database settings to your computer:

Database settings in the wp-config.php file

Close wp-config.php file with CTRL+X

This is the code structure that will create our MySQL backup:

mysqldump -u USERNAME -p DATABASE > backup-file.sql

Replace USERNAME and DATABASE with the actual settings from your wp-config.php file, copy and paste the code in the terminal (right-click only), then hit Enter on the keyboard.

To distinguish this backup file from other backup files, you can give it any name.

Now copy the password and right-click in the terminal to paste it (you'll see nothing on the screen for security reasons):

mysqldump -u cpaneluser_demoftpUSR -p cpaneluser_hpdemo > backup-2107.sql

The above command in the terminal

List the directory to see the new backup file:

The new backup file in the directory

This is how our site looks like now:

Overview of the site's Hello world page

Now we'll rename the article title Hello world! in the database archive using this command:

sed -i 's/original/new/g' file.txt
sed -i 's/Hello world!/New Article Title!/g' backup-2107.sql

Changing the article title

Using another command we can check if our new article title has been saved in the database archive:

grep 'something' file.txt

grep 'New Article Title!' backup-2107.sql

The new article title in the archive file

Now let's import (restore) the database from the archive file.

The changed title will prove that we are using the archived copy and not the original database.

We are going to use this code:

mysql -u USERNAME -p DATABASE < backup-file.sql

where again you must use your database settings:

Restoring the database from the archive

Now let's refresh the site to see the effect:

The same page with the new title

Summary

You see how convenient is to use SSH commands.

Here's a list of all used commands:

cd - change directory.

ls –la - list directory contents.

nano wp-config.php – open file with nano editor.

CTRL+O then hit Enter to save the file if you have made any changes.

CTRL+X to close the file.

mysqldump -u USERNAME -p DATABASE > backup-file.sql - create an archive file.

mysql -u USERNAME -p DATABASE < backup-file.sql - - restore database from archive.

grep 'something' file.txt -- find the word something in file.txt.

sed -i 's/original/new/g' file.txt -- replace words or phrases (original with new) in file.txt

Still not finding what you're looking for?

Contact our support team with any additional questions or concerns.

Contact support