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
wp-config.php with the nano (or any other) editor:
Copy database settings to your computer:
wp-config.php file with
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
List the directory to see the new backup file:
This is how our site looks like now:
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
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
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:
Now let's refresh the site to see the effect:
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