Restore WordPress Backup with WP CLI & SSH

How to restore (restore) our WordPress data with WP CLI & SSH.
Share:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
restore from backup WordPress with WP CLI and SSH

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:

  1. An error occurred when updating themes/plugins
  2. The website is broken and we can't fix it
  3. Move the website to another place
  4. 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:

  1. Database backup files (.sql) & files (.tar.gz) are already in the root shell
  2. The extracted files will be placed in a folder./public_html
  3. 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
  4. 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:

  1. 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 commandls -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.sqland website file backups are filesdigitalizer.my.id.2022-03-06.tar.gz

  1. 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.sqlinto the database used by WordPress according to the username and database in the file./public_html/wp-config.php

  1. 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_htmlalso, so I hope you are careful and match your backup file folder with the destination file folder.

  1. 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:

  1. Login to your admin dashboard via a browser, usually https://yourdomain.com/wp-admin
  2. Set permalink from menu Settings - Permalink
  3. Especially for Oxygen Builder users, you need to sign the shortcode from the Oxygen - Setting - Security - Sign All Shortcodes.
  4. 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:

  1. Install a new WordPress (fresh install) into the directory ./public_html/oldsite/
  2. 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
  1. Rename the backup folder from ./public_html/public_html to ./public_html/oldsite
mv ./public_html/public_html ./public_html/oldsite
  1. Open .htaccess file
nano ./public_html/oldsite/.htaccess
  1. 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
  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Article

Slick Slider in Oxygen Builder
10 Example Slick Sliders in Oxygen Builder & How To Make Them
How to create a slider with Slick Slider in Oxygen Builder contains the stages of creation, the options and 10 sample sliders of the results.
How to Set a Website Always Use HTTPS (SSL)
In this article you will read how to set .htaccess so that the website always uses HTTPS (SSL) when opened by visitors.
iBee-Music-Mockup
Creating animated off canvas menus and hamburger buttons in Oxygen Builder
Tutorial on creating animated menu canvas and hamburger buttons in Oxygen Builder
COMPLETE PACKAGE OF WEBSITE DESIGN & DEVELOPMENT
Digitalizer offers website design & development services for company profiles, online shops, event organizers, educational institutions or other fields. You don't need to think about domains, web hosting / servers, DNS, email, design, development, security and other technical issues. Just prepare your website content, we do the rest.
All website design & development packages include 1 year maintenance!
Free Consultation
Back to top
cross