One of the important things in maintaining a website is making backups. Usually a good provider has a backup system as follows:
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.
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:
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.
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).
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.
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).).
#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.
#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/
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:
#!/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."
chmod +x backup.sh
./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.
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
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:
#!/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"
chmod +x autobackup.sh
0 4 * * * ~/autobackup.sh
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.
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 -