How to Backup a WordPress Site using the Command Line

  • Post author:
  • Post last modified:November 10, 2024
  • Reading time:7 mins read

Backing up a WordPress site

The process of backup of a WordPress site comprises of two parts. First, the sites’s database under MySQL or MariaDB DBMS is saved in a database dump file. Second, the files for the site under the Document Root are saved in an archive file. The two need to be kept together and both are required to restore the website from a backup. In this post we will look at Linux commands for backup and restore of a WordPress based website.

1.0 Backup overview

We will be using the mysqldump command to create the database dump for the website. For taking a backup of the files under the Document Root, we will be using the tar command. In general, any transaction in a system triggers multiple database updates. Suppose a transaction triggers two database updates. The database is in a consistent state before the transaction. When a transaction happens, there is a small time window between the first and the second database updates. After the first update, the database gets into an inconsistent state. After the second update is complete, all database updates for the transaction are done and the database gets back into a consistent state. If the backup is taken, when the database is in an inconsistent state, the restore from backup might not work well. So, for taking the backup for the website, we will stop the web server for the duration of the backup.

We will take backup for the example.com website. We start with following assumptions.

  1. The Document Root is at /home/user1/www/example. The Document Root is the directory where the website files are stored.
  2. The database for the website is stored under “example_db” database in MySQL or MariaDB. The database administrator for the website has user-id “example-user” and the password for the database is “example-password“. The database name, user-id and password must match with the values given while creating the database and also the values specified in the wp-config.php file in the Document Root.
  3. The backup is taken in the /home/user1/backup directory.
  4. The web server is nginx.

2.0 Backup Commands

(i) Stop the web server.

$ sudo systemctl stop nginx

(ii) Take the dump of the database.

$ mysqldump --user=example-user --password="example-password" --no-tablespaces --result-file=/home/user1/backup/example_db-dump.sql example_db

The database dump for the website is stored in /home/user1/backup/example_db-dump.sql file.

(iii) Create a tar archive for the Document Root.

$ cd /home/user1/www/
$ tar -cvzf /home/user1/backup/example.tar.gz example

The tar archive is stored in /home/user1/backup/example.tar.gz file.

(iv) Restart the web server.

$ sudo systemctl start nginx

(v) Find the SHA256 checksum for the backup files.

$ cd /home/user1/backup
$ sha256sum example.tar.gz example_db-dump.sql > checksum

The files example.tar.gz, example_db-dump.sql and checksum constitute the backup for the example.com website. You can check the veracity of the backup files any time with the command,

$ cd /home/user1/backup
$ sha256sum -c checksum

3.0 Backup script

We can put the backup commands in a bash script named backup-example so that we can execute the script rather than type the individual commands. The script is,

#!/bin/bash

set -x

BACKUP_TS=`date +%F-%H%M`

BACKUP_DIR="/home/user1/backup/example-${BACKUP_TS}"

mkdir ${BACKUP_DIR}

mysqldump --user=example-user --password="example-password" --no-tablespaces \
        --result-file=${BACKUP_DIR}/example_db-${BACKUP_TS}.sql example_db

cd /home/user1/www
tar -cvzf ${BACKUP_DIR}/example-${BACKUP_TS}.tar.gz example

cd ${BACKUP_DIR}
sha256sum example_db-${BACKUP_TS}.sql \
        example-${BACKUP_TS}.tar.gz > checksum-${BACKUP_TS}.sha256

ls -ls

We create a directory for each backup in the /home/user1/backup directory. We also embed the backup timestamp in the file names and these help in housekeeping of the backup files.

4.0 Restore from backup

If something goes wrong, or you want to move the website to a new host, you can restore the website from an earlier backup. The commands are as follows.

(i) The requirements for WordPress are PHP 7.4 or greater and MySQL Version 8.0 or MariaDB version 10.4, or greater.

(ii) Stop the nginx web server.

$ sudo systemctl stop nginx

(iii) Create the database, example_db. If it exists, delete it and create a fresh one.

$ sudo mysql -u root
...
MariaDB [(none)]> create database example_db;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> grant all on example_db.* to 'example-user'@'localhost' identified by "example-password";
Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> quit
Bye

(iv) Load the newly created database, example_db, with the database dump in the backup.

$ cd /home/user1/backup
$ mysql -u example-user -p
Enter password: example-password
...
MariaDB [(none)]> show databases;
...
MariaDB [(none)]> use example_db;
Database changed
MariaDB [example_db]> source example_db-dump.sql;
MariaDB [example_db]> quit
Bye

(v) Install the files from the tar archive in the backup.

$ cd /home/user1/www
$ tar -xvzf /home/user1/backup/example.tar.gz

(vi) Restart the web server.

$ sudo systemctl start nginx

After this, the website should be running fine. In case of errors, debug from the system log and nginx log messages. Also, if your Document Root and/or the backup directory is outside the directory tree of user’s home directory, sudo might be required with the file update commands like tar.

Karunesh Johri

Software developer, working with C and Linux.