If you are running a WordPress website on a cloud server or a VPS, then you may need to take manual backups for various reasons. While this article is not the fastest to read the guide reading this guide will help you to manage your server without requiring you to install any web hosting panel or PHPMyadmin. Before taking a backup, you should look at your MySQL database size. If the size of database is too huge then you may read our this guide.
As we know, WordPress has two things to take a backup of – the first is the FTP files and the second is the MySQL database. Another thing you may take backup of is the SSL certificates. I will assume that :
- If you are using a Windows PC then you have configured Ubuntu Bash for Windows, so you’ll use Ubuntu Bash to SSH.
- If you are using a Mac, then you are using iTerm2.
- If you are using a Linux PC, then you are using the default terminal app of your OS.
You already have one server’s WordPress (which you’ll take backup) with a domain name. Another server is required to take the backup (backup server). There are server backup server providers available these days, you can use our favourite VPSDime. VPSDime does not cost a huge per month and the staffs are knowledgeable about managing Linux servers. In case you got confused with something around a server, you can even pay them to do the job for you. If you are already using VPSDime to host your site, then use some other web hosting for backup. The backup server and main server should not be in the same data center (to minimize the chance of exploit, downtime etc).
---
Steps to Backup WordPress MySQL Database
Navigate to the root of the public directory of the main server, usually, we setup WordPress at /var/www/html
:
1 2 3 4 | cd /var/www/html ls -al locate wp-config.php cat wp-config.php |
You’ll get the MySQL database user name and password from wp-config.php
file. You can example the MySQL database:
1 2 3 4 | mysqldump -u <username> -p # example # mysqldump -u root -p show databases; |
It will show something like the below:
1 2 3 4 5 6 7 | +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | your-database | +--------------------+ |
You can check the tables:
1 | show tables; |
You can exit the MySQL interface by typing:
1 | exit; |
You can take a backup of the MySQL database by typing:
1 | mysqldump -u username -p databasename > backup.sql |
As it is a manual backup, you can restore this backup to test. You can restore to MySQL server by typing:
1 2 3 4 | show databases; USE yourdatabasename; mysql> \. backup.sql show tables; |
You can change the database name on wp-config.php
file to check whether the backup is working. It is a robust, foolproof method, although it consumes some time. You can drop any of the databases you want:
1 | mysql> DROP DATABASE backup; |
Now, from the backup server, you can wget
the SQL backup:
1 2 3 | cd /var/www/ mkdir backup && cd backup wget https://example.com/backup.sql |
DO NOT forget to delete the SQL backup from the main server :
1 2 | cd /var/www/html rm backup.sql |
But doing the above via SSH every day is not practical for most webmasters. Consider automating by using a bash script or using MySQL Workbench.
Steps to Backup WordPress FTP
As the size of FTP files is often large, we need to check the space available on the server and the size of the FTP files.
1 2 3 4 5 6 | # go to the root of FTP cd /var/www/html # check the space available on the whole server df -h # check the size of the FTP files du -h /var/www/html |
Now make a tarball of the whole FTP :
1 2 | # go to the root of FTP tar -vcf mybackup.tar.gz . |
Now from your backup server, wget the tarball:
1 2 | cd /var/www/backup wget https://example.com/mybackup.tar.gz |
Although it is rarely required (unless you want to move the server), you can untar by running:
1 2 | file mybackup.tar.gz tar xvf mybackup.tar.gz |
Again, you can automate this process by using a bash script or you can integrate GitHub with your instance.
Conclusion
Taking regular backup is of utmost importance. I will suggest you frequently download the backups on your local computer too and maybe, you can write them on an optical drive, SD card etc. A backup server makes the process fast but having a backup in “hand” gives a feeling of mental peace.
Although this article is bigger to read, this will extremely help you in case you want to migrate from one server to another.