In this post we will see How we can backup one wordpress site.So for this we working on one Linux machine with Apache and MySQL. As wordpress is nothing just a PHP code, so in short we are working on LAMP setup.

We have below setup

SetUP

Linux      - CetOS 7.8
MySQL      - Mariadb server 5.5.65
Apache     - Apache 2.4.6
PHP        - 7.3.17
Wordpress  - 5.4.1

Basically any WordPress site is created through few components like mentioned above. Some of components could be work with some normal changes are rarely save very specific settings for wordpress. So let’s see which component need backup and has major changes in wordpress.

MySQL — This save WordPress data like post content
Apache — This contain virtual settings and DocumentRoot

So above components are used mostly in wordpress which has changes on frequent basis. So we need to backup these components. In some previous post we saw how we install LAMP (Linux,Apache, MySQL and PHP)

MySQL Backup

WordPress save all its data in any Database, Mostly we use MySQL database server which is easily to use and we can take its backup which helps in case disaster scenario we can restore this database backup file in any MySQL database server to restore site again. So let’s see how we can take backup of MySQL.

In Linux machine we can take MySQL backup with some simple command through root user like below.

/bin/mysqldump -u root -p'passw0rd'  database-name | gzip > /backup-directory/database_backup_`date +%F-%H:%M`.sql.gz

With above command we can take backup of particular database in MySQL Server or even of ‘–all-databases’ present on MySQL Server. This way we will get one sql.gz(Gzip Sql file) file which could easily restore in any MySQL Server. SO lets see how we can restore this sql file

mysql -u root -p'passw0rd' wordpress < sql-file-name 

With above command we can restore MySQL wordpress database. But do we need to take backup manually every time, surely no. We want some kind scheduling organiser which would complete this schedule this task for us.
So now we need to perform backup of MySQL on daily basis through some scheduling mechanism. In Linus we have scheduling mechanism which is called cron, We cover crons in previous posts.Linux cron thing can easily able to schedule Linux commands, scripts etc. This is well aware used tool within Linux engineers.

We can set cron for mysqldump command or we can also crate one normal bash script which could do some other stuff as well with this.
Another advice, we should not just save backup file on local Disk, we should always sync database backup sql file on any other machine or on some other secure location like some Cloud storage, like i am using GoogleDrive, so that restore anytime or on any machine.

0 1 * * * /scripts/database_backup.sh

In above cron setup, we have schedule to run databases backup script on 0100 every day, at this time servers has less load.

Apache Backup

WordPress also used to save all of its images, wordpress PHP files, themes , plugin files in Apache Document Root which makes it necessary to setup backup for apache files as well. So apache files are normal file-system file that we can backup with some normal copy mechanism that could same its permission, link etc so that we could save it like we can restore it later for wordpress restoration.

In Linux we can do this through rsync or tar. I prefer doing tar as this is just local disk first. In case we are working to take backup on remote machine, then i prefer to use rsync command.

In Apache we have two important directories

1. /etc/httpd -- Apache configuration file
2. /var/www   -- Document Root of Apache
tar -cpzf /backup-directory/apache_`date +%F-%H:%M:%S`.tar.gz /etc/httpd /var/www

So in above command we used to take backup of both configuration files and DocumentRoot.

We should create one script and schedule it through cron like below

0 2 * * * /scripts/apache_backup.sh

In above ways we can take backup of MySQL database and Apache configuration, DocumentRoot which make easy to create website from this data again from scratch.We can just need required software and restore data from backup file