Fast WordPress Backup with WP CLI & SSH

How to quickly backup WordPress with 1 WP CLI command and 1 SSH command, create backup script and autobackup script.
Share:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
Backup Cepat WordPress dengan WP CLI & SSH
Updated: 10 March 2022Array

One of the important things in maintaining a website is making backups. Usually a good provider has a backup system as follows:

  1. Runs routine backup automatically, there are daily, weekly or monthly.
  2. The data is stored on another server in a different location to anticipate the loss of backup data if the datacenter used is hit by a natural disaster.

However, as the owner or manager of a website, I also recommend making backups independently, be it automatic or manual.

But why? Because we will not know what kind of problems will arise in the future.

There is my own personal experience, the hosting company that I use is closed and I can't retrieve existing website data, nor can I get backup data from the company.

Based on this experience, we should routinely make backups independently, can be stored on our computers, or on our other servers/clouds.

Of course we hope that we don't use these backups, which means that our website and hosting are not problematic.

Backup WordPress Website with WP CLI and SSH

There are various ways to backup website data based on WordPress, but the principle of all the different ways is that we save data from our database and data files.

But this time I will share about how we back up website data with WP CLI and SSH. This method is specifically for those using LINUX OS, cPanel or the like.

But before that for those who don't know about WP CLI and SSH, here's a brief explanation:

What is SSH?

We quote from Wikipedia, Secure Shell Protocol (SSH) is a cryptographic network protocol for operating network services securely over an insecure network. Its most prominent applications are remote login and command line execution.

What is the WP CLI?

We quote from wp-cli.org, WP-CLI is a command line interface for WordPress. You can update plugins, configure multisite installations, and more, without using a web browser.

So, in short both are interfaces for executing commands using text, not graphics (GUI).

Pros and cons of Backup WordPress with WP CLI and SSH

  • The reason for using this method for me is that the process is quite fast in the sense that we can reduce some of the steps that we usually do manually, and maybe this is just my feeling, but it seems that the process of exporting databases and compressing files is also faster, although logically it seems the same ha ha..
  • The drawback is, not all web hosting support with WP CLI and SSH. Especially SSH, sometimes we have to ask the hosting provider to activate it first. Some providers don't even provide SSH features for security reasons.

Before you use SSH, you can also check beforehand whether the hosting already provides Terminal features. Because this feature is the same as SSH, but we need to login to our hosting panel first.

WordPress backup command with WP CLI and SSH

Condition notes:

  • We will use the WP CLI to export the database, and SSH to compress the files.
  • Assuming we are logged in SSH/Terminal, and are in the root shell
  • WordPress files are in the public_html folder

I will share 2 commands for each of them, namely to output a file without a date as a base, and an output file with a date as an example of its development.

For file backups, I personally prefer to archive all files in the WordPress folder so that if there are files that are needed by the website, they can be included. For the compression I use will produce a file with output .tar.gz

Another alternative is to just export WordPress content which will generate data in .xml format but I don't use this step in this backup process.

Other things you can develop yourself by looking for references, especially about commands on SSH (or rather BASH , a command language in shells).).

Commands for Database Backup

#database backup without date
wp --path='./public_html' db export thedomainname.sql

#backup database with today date 
wp --path='./public_html' db export "thedomainname.$(date '+%Y-%m-%d').sql"

*Replace thedomainname with the website domain that you want to backup.

Commands for File Backup

#files backup without date
tar czf thedomainname.tar.gz ~/public_html/

#files backup with today date
tar czf "thedomainname.$(date '+%Y-%m-%d').tar.gz" public_html/

Backup Script

From the two commands above, we can make a simple shell script so that it can be run in one command and change the domain name variable according to our needs. The steps are as follows:

  1. Create and edit a new file, for example we name it backup.sh
nano backup.sh
  1. Add a script like this:
#!/bin/bash
echo -e "What is the domain name that you want to backup?"
echo -n "(only for the purpose of naming the backup file, for example: digitalizer.my.id)? : " 
read domain
tar czf "$domain.$(date '+%Y-%m-%d').tar.gz" public_html/
wp --path='./public_html' db export "$domain.$(date '+%Y-%m-%d').sql"
echo -e "$domain has finished the backup."
  1. Save then exit to the shell prompt by pressing ctrl+o and ctrl+x
  2. Give permission so that the file can be executed from a shell prompt
chmod +x backup.sh
  1. Run the file by typing ./backup.sh , and it will produce something like this:
./backup.sh
What is the domain name that you want to backup?
(only for the purpose of naming the backup file, for example: digitalizer.my.id)? : digitalizer.my.id
Success: Exported to 'digitalizer.my.id.2022-03-06.sql' .
digitalizer.my.id has been backed up.
  1. If we look at the contents of the directory with the ls -l command , we will find 2 files from the backup (sql & tar.gz)
drwxr-xr-x  2 asdkjlkas asdkjlkas      4096 Mar  6 23:18 backup
-rwxrwxr-x  1 asdkjlkas asdkjlkas       346 Mar  6 23:21 backup.sh
-rw-rw-r--  1 asdkjlkas asdkjlkas  36570924 Mar  6 23:20 digitalizer.my.id.2022-03-06.sql
-rw-rw-r--  1 asdkjlkas asdkjlkas 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 asdkjlkas nogroup        4096 Mar  2 04:06 public_html

Auto Backup Scripts

What if we want to make autobackups or backups that run automatically?

Of course we can't use the backup script above, because in that script we have to enter the domain name first.

The trick is that we replace it with the domain we want, and remove the command to input the domain, the steps are as follows:

  1. Create and open a new file, for example we name it autobackup.sh
nano autobackup.sh
  1. If we want to generate a backup file for example: digitalizer.my.id domain, then add a script in the file as follows:
#!/bin/bash
tar czf "digitalizer.my.id.$(date '+%Y-%m-%d').tar.gz" public_html/
wp --path='./public_html' db export "digitalizer.my.id.$(date '+%Y-%m-%d').sql"
  1. Save then exit to the shell prompt by pressing ctrl+o and ctrl+x
  2. Give the file permission to execute from a shell prompt:
chmod +x autobackup.sh
  1. Add a cron job so that the command can run automatically, open and edit the cron file :
crontab -e
  1. Suppose if we want to run autobackup.sh every day at 4 am then add the following command:
0 4 * * * ~/autobackup.sh
  1. Save then exit to the shell prompt by pressing ctrl+o and ctrl+x
  2. Finished.

That way, every 4 am there will be a backup process for your WordPress database and files.

If you have trouble converting the schedule into a cron command, you can use the Crontab Guru to generate a cron command.

Closing

So with just 2 command lines, each with WP CLI and SSH you have made a backup of your WordPress website data. You can save the results locally, or you can move them later to another safer place.

In addition, with the development of shell scripts you can also make backups with one command, and can also create backups that run automatically.

What do you think about these backup steps with WP CLI and SSH (BASH)? Leave your comment below… Good luck, I hope useful!

- Zulfikar Wijaya -

Credits:

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