In this article, I will share information about how to restore (restore) our WordPress data from the backup we have with WP CLI & SSH.
When Do We Need a Restore Backup
From time to time this restore process will be needed, for example when we experience an incident:
- An error occurred when updating themes/plugins
- The website is broken and we can't fix it
- Move the website to another place
- and maybe there are other things that require you to restore from an existing backup
For those of you who want to know the backup process, you can read on "Fast WordPress Backup with WP CLI & SSH" .
Condition Note
Note the conditions we use are as follows:
- Database backup files (.sql) & files (.tar.gz) are already in the root shell
- The extracted files will be placed in a folder
./public_html
- In the folder
./public_html
, WordPress has been installed first, whether it's just installed or a website that is already active and has content - Commands run from root shell
Important! Data on existing or installed WordPress files will be replaced/overwritten with new data so that the old data will be deleted/lost. Make sure that you understand this risk and anticipate if an error occurs during the restore.
Command Restore Backup with WP CLI and SSH
To restore a backup, the first thing you need to do is import the database and extract your WordPress files with the following steps:
- Know the name of the backup file. For those who don't know the name of the backup file that you want to restore, let's first see the contents of the directory with the command
ls -l
digitalizer@shell:~$ ls -l total 333456 -rwxrwxr-x 1 digitalizer digitalizer 148 Mar 6 23:29 autobackup.sh drwxr-xr-x 2 digitalizer digitalizer 4096 Mar 6 23:18 backup -rwxrwxr-x 1 digitalizer digitalizer 346 Mar 6 23:21 backup.sh -rw-rw-r-- 1 digitalizer digitalizer 36570924 Mar 6 23:20 digitalizer.my.id.2022-03-06.sql -rw-rw-r-- 1 digitalizer digitalizer 304862665 Mar 6 23:20 digitalizer.my.id.2022-03-06.tar.gz drwxr-x--- 2 root nogroup 4096 Feb 25 10:23 logs drwxr-xr-x 10 digitalizer nogroup 4096 Mar 2 04:06 public_html digitalizer@shell:~$
From the results above, we know that database backups are files digitalizer.my.id.2022-03-06.sql
and website file backups are filesdigitalizer.my.id.2022-03-06.tar.gz
- Import the database with the command:
wp --path='./public_html' db import digitalizer.my.id.2022-03-06.sql
The command above will import the database from the file digitalizer.my.id.2022-03-06.sql
into the database used by WordPress according to the username and database in the file./public_html/wp-config.php
- Extract the backup file with the command:
tar zxf digitalizer.my.id.2022-03-06.tar.gz
The above command will extract the digitalizer.my.id.2022-03-06.tar.gz
.
For the record, when I compressed the WordPress files before, the directory I compressed was ./public_html
also, so I hope you are careful and match your backup file folder with the destination file folder.
- The process of importing the database and extracting files is complete .
After the database import and extract process is complete, then there are several steps that you may need to do. I say maybe because maybe you don't have to. We continue reading:
Finishing the Restore Backup Process
These steps are:
- Login to your admin dashboard via a browser, usually
https://yourdomain.com/wp-admin
- Set permalink from menu
Settings - Permalink
- Especially for Oxygen Builder users, you need to sign the shortcode from the
Oxygen - Setting - Security - Sign All Shortcodes
. - Check from the front side of your website, and make sure all appearances and functions are running normally.
For those of you who don't know about Oxygen Builder, please read the article "Oxygen Builder, the Best WordPress Page Builder Plugin?".
Restore Backup with New Domain
If you want to restore your WordPress backup as well as change it to a new domain, then after the database import and extract file process is complete, you can use the command below to replace all the old domain strings into the new domain.
wp --path='./public_html' search-replace olddomain.com newdomain.net --all-tables
The above command will replace all the strings of olddomain.com to newdomain.net in all tables in the database. To --all-tables
is optional.
Additional Notes: Restore Backup to Domain Subdirectory
An example case is as follows, suppose I have a website with a domain digitalizer.my.id
, then I will move it to another host/server and will use a domain digitalizer.my.id/oldsite
, then the steps are as follows:
- Install a new WordPress (fresh install) into the directory
./public_html/oldsite/
- Run the restore backup command above, but change the path according to the new conditions
#import database backup into WordPress which is at ./public_html/oldsite wp --path='./public_html/oldsite' db import digitalizer.my.id.2022-03-06.sql #extract the backup file into the ./public_html folder tar zxf digitalizer.my.id.2022-03-06.tar.gz -C ./public_html
- Rename the backup folder from
./public_html/public_html
to./public_html/oldsite
mv ./public_html/public_html ./public_html/oldsite
- Open
.htaccess
file
nano ./public_html/oldsite/.htaccess
- Change the WordPress root folder in the section
# BEGIN WordPress
and# END WordPress
, in this case change "RewriteBase /
" to "RewriteBase /oldsite
". The details are like this
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
Becomes:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase /oldsite/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
- Perform the Finishing the Restore Backup Process as described above.
Closing
That's how to restore our WordPress backup using WP CLI and SSH. For some people who are familiar with GUI (Graphical User Interface) this method may be difficult, but for some people find it easier and faster with CLI (Command Line Interface).
How about you?
- Zulfikar Wijaya -
Credits:
- Image taken from freepik.com and have modified by us.