How to Backup a WordPress Site for free!

1. Overview

How to backup a WordPress Site free? The task is not trivial but I make it easier to keep your WordPress site safely backed up on your computer? I’m assuming that WordPress is installed on a Linux machine. However, this can be easily adapted to do it on other operating systems as well!

2. Is there a plugin?

Why don’t you use an off the shelf plugin which has already been tried and tested?

This is a very legitimate question. There are many popular plugins out there such as UpdraftPlus WordPress Backup Plugin and Jetpack by WordPress.com just to name a few.

Don’t get me wrong, plugins are useful for a variety of reasons, but they have to be treated for what they are, third-party software, over which you have little or no control. From a security point of view, the fewer plugins you install, the better it is.

Furthermore, the more plugins you install, the slower the site will run. You might also be tempted to install a long list of plugins, causing incompatibilities over time.

2.1 How to backup wordpress site without plugin

If you have good knowledge of how WordPress works under the hood, you have good SQL and MySQL knowledge, and you can find your way around an operating system easily, then you will soon know how to backup a wordpress site without plugin.

3. What items are we backing up?

The best way to backup a wordpress site without plugin is to copy the:

  1. file system
  2. database schema

3.1 How to backup the File System

The first step is backing up the WordPress files from the file system, including scripts, themes, plugins, and uploaded content. You need to know the directory which holds all of your WordPress. In this case, the directory is:

/var/www/html

The following command will compress all contents from that location into one single file.

tar -cjf blogFiles_28-04-2019.tar /var/www/html

Let’s tackle that command piece by piece. “tar -cfj” is a Linux command with which we instruct the operating system to compress files it finds in the location “/var/www/html”, and name the result blogFiles_28-04-2019.tar”. Note that the data was inserted in a way to indicate when the backup was taken.

So, just go to a directory of your choice and execute that command. Once it finishes, you will be able to see a new file called “blogFiles_28-04-2019.tar“. The amount of time required by the command, and the file size of its result, depend on how large your WordPress site is. 

3.2 How to Backup WordPress Database Schema

It’s now time to get a copy of the database.

For this step, you will need:

  1. the username,
  2. the password,
  3. the database name,
  4. the hostname of your WordPress database.

If you don’t know this information but have access to your host, you can retrieve them by reading the contents of the wp-config.php file. In our setup, this file is found in /var/www/html/wp-config.php.

With all the information at hand, you can execute the following command, replacing the placeholders with your information:

mysqldump --user=[YOUR_USER] --password=[YOUR_PASSWORD] --databases [YOR_DB_NAME] --host=<YOUR_HOSTNAME> > blogData_28-04-2019.sql

By executing this command, we are instructing the database engine to take a copy of all WordPress database contents into the file blogData_28-04-2019.sql

4. Storing the backups

At this point, we have our WordPress site backed up into two files “blogFiles_28-04-2019.tar” and “blogData_28-04-2019.sql”.

For obvious reasons, keeping the backup files on the same and single server is not a bright idea. Should something happen to the server, the site will be lost, together with the backup files. We don’t want this to happen, hence there are a few more steps to complete. We need to store a copy of these files in a reliable and secure place.

In this article, I will describe two ways which are both reliable and secure. Apart from these mentioned, there are other solutions.

4.1 FTP

FTP stands for File Transfer Protocol and is used to move files from one server to another. There are applications like FileZilla, or the linux command line tool lftp. In this article, we’ll use the latter. Just in case the lftp is not installed by default on your server, you can install it using the following command:

sudo apt-get install lftp

Once lftp is present, you can execute the following to copy the two files created in the steps before.

lftp [YOUR_FTP_USERNAME]@[YOUR_FTP_HOSTNAME]:~> put blogData_28-04-2019.sql
lftp [YOUR_FTP_USERNAME]@[YOUR_FTP_HOSTNAME]:~> put blogFiles_28-04-2019.tar

In both cases, lftp will ask for the password

4.2 The Cloud

Another alternative to using FTP servers is to use a cloud-based file storage solution like Google Drive, or DropBox. One solution would be more ideal than others depending on price plans and operating system support. There are other options too, like using a cloud-based source control solution like http://www.bitbucket.org, or http://www.github.com

5. Automation

In a real-world scenario, backups are automated. One approach that will be explained in this article is the following. In this part of the article, we will assume that you are hosting your blog on a Linux machine and that you are familiar with GIT.

We can achieve this by creating a shell script combining all the above steps and use a scheduler to execute our script at particular intervals. One such scheduler is the Linux Crontab. In this example, we will have the WordPress site backed up every day.

First, we need a shell script to create and upload the backup files. For simplicity’s sake, we will use bitbucket to store our backups on the cloud. We will also assume that the ssh keys of the client are already set in bitbucket. Create a new repository on bitbucket and clone it on the WordPress server. Update the script with your paths:

#!/bin/bash

echo "Finding config details..."
BLOG_DIR=/var/www/html
DEST_DIR=~/dbdumps/wellbeingbaristabackups/
DB_NAME=`echo "<?php require_once(\"${BLOG_DIR}/wp-config.php\"); echo DB_NAME;" | php`
DB_USER=`echo "<?php require_once(\"${BLOG_DIR}/wp-config.php\"); echo DB_USER;" | php`
DB_PASS=`echo "<?php require_once(\"${BLOG_DIR}/wp-config.php\"); echo DB_PASSWORD;" | php`
DB_HOST=`echo "<?php require_once(\"${BLOG_DIR}/wp-config.php\"); echo DB_HOST;" | php`
 
#Compress the WordPress files
echo "Compressing WordPress files..."
FILES_TAR_FILE_NAME=${DEST_DIR}blogFiles-$(date +%Y%m%d).tar
tar -cjf $FILES_TAR_FILE_NAME $BLOG_DIR

#Dump the database contencts
echo "Taking a database dump..."
DB_TAR_FILE_NAME=${DEST_DIR}blogFiles-$(date +%Y%m%d).sql
mysqldump --user=$DB_USER --password=$DB_PASS --databases $DB_NAME --host=$DB_HOST > $DB_TAR_FILE_NAME

#Push to git
echo "Sending files to remote"
cd $DEST_DIR
git add .
git commit -m "Another daily backup"
git push origin master

The final step is to configure the scheduling. The above script assumes that the backups will be taken not more than once daily. If you need more frequent backups, the date timestamp of the files needs to include hours or possibly minutes. Next is the scheduling for the script. To enable this we need to create a Linux cron job. Open your terminal and type in the following command:

sudo crontab -e

This will open the cron editor. You might need to change the script name and/or location, but if you named the script “wordPressBackup.sh” and saved it in location “/home/user/dbdumps/”, then add the following line in the crontab editor.

0 1 * * * /home/user/dbdumps/wordPressBackup.sh

This instruction will perform the backup, every day at 1 am.

6. Conclusion

In this article, we have seen how to make sure your WordPress site is backed up properly.

We have also used tools such as FTP and GIT to store our backups safely.

Scroll to Top