Easy Steps To Backup WordPress

Content Management Systems

While owning a wordpress site and having active content it’s mandetory to backup your site time to time to avoid any undesirable situation like hacking and malware issues where you need to restore your site back to normal. So if you have regular backups then even incase your database goes corrupt you can go back to the normal working state if you have the backups.


ADVERTISEMENT

There are many plugins available to backup database or the files in wordpress but they also have there own over head on performance and security when you are on a shared hosting service with limited hardware resources.
Usually almost all backup plugin’s use wp-cron.php file to schedule the backups but when you care about your security and have enabled many security features on your site its observed that almost all backup plugin’s failed to do the job correctly on time and in a scheduled way.

For example BackupWordpress plugin throws below error and fails to backup

BackUpWordPress has detected a problem. wp-cron.php is returning a 403 Forbidden response which could mean cron jobs aren't getting fired properly. BackUpWordPress relies on wp-cron to run scheduled backups. See the FAQ for more details.

So we came up with below easy simple script to backup wordpress which backups up both databases and the files in a easy way. Please follow below steps to setup and configure automatic wordpress backup for both database and files.

1. login to your hosting providers Cpanel or similar software.

2. Go to file system browser and create a folder sitebackups parallel to public_html folder to store all the backups.

wordpress backup

3. Go inside the sitebakups folder and create 2 folders dbbackups and filesbackup for storing DB backups and files backup respectively like below:

wordpress backup

4. For database backup create a shell script file named db.sh with below content inside the sitebackups folder.

wordpress DB backup script file

5. Open the db.sh file in code editor and put below simple script for taking DB backups.

#!/bin/sh

PATH=/usr/sbin:/sbin:/bin:/usr/bin

user="db_username"
pass="db_password"
host="localhost"

sub="$(date +"%Y-%m-%d")"
dest="/home/techpaste/sitebackups/dbbackups"
#mdb="$dest/db/$sub"

mdb="$dest"
if [ ! -d $mdb ]
then
mkdir -p $mdb >/dev/null 2>&1 && echo "Directory $mdb created." || echo "Error: Failed to create $mdb directory."
else
echo "$mdb directory already exists!"
fi

now="$(date +"%Y-%m-%d_%H-%M-%S")"

file=""

#dbs="$(mysql -u $user -h $host -p$pass -Bse 'show databases')"
dbs="db_name"
for db in $dbs
do
file="$mdb/$db.$now.sql.gz"
mysqldump -u $user -h $host -p$pass --complete-insert $db | gzip -9 > $file
echo "Backup $file.....DONE"
done

find $dest/ -maxdepth 1 -type f -mtime +7 -exec rm -f "{}" \;

Update below entries according to your websites DB name, password and hostname.

user="db_username"
pass="db_password"
host="localhost"
dbs="db_name"

 

6. For websites files backup create a shell script file named files.sh with below content inside the sitebackups folder.

wordpress files backup script

7. Open the files.sh file in code editor and put below simple script for taking files backups.

#!/bin/sh

backup_files="/home/techpaste/public_html"

dest="/home/techpaste/sitebackups/filesbackup"

if [ ! -d $dest ]
then
mkdir -p $dest >/dev/null 2>&1 && echo "Directory $dest created." || echo "Error: Failed to create $dest directory."
else
echo "$dest directory already exists!"
fi

day="$(date +"%Y-%m-%d_%H-%M-%S")"
host=$(hostname -s)
archive_file="$host-$day.tgz"
rm -f $dest/$archive_file
tar -czf $dest/$archive_file $backup_files

find $dest/ -maxdepth 1 -type f -mtime +7 -exec rm -f "{}" \;

Update below entries according to your sites requirement

backup_files=”/home/techpaste/public_html”

This is the path from which all the files will be backed up and compressed.

dest=”/home/techpaste/sitebackups/filesbackup”

This is the path where all the files backups will get stored.

8. Once above two script to backup wordpress are setup, provide execute privileges to owner to let it run in scheduled time and date.

wordpress backup

9. Once all above steps are completed we can setup the script to backup wordpress as a scheduled job to automate the DB and files backup on scheduled time. To do this we can use the crontab or else if your hosting provider provided Cpanel then you can follow below steps to setup cron jobs for automatic backups.

9.a. Login to Cpanel with your hosting userid and password and go to Cron jobs in Advanced section.
Cpanel cronjobs

9.b. Here you can use the common settings option to setup your cron jobs. I have requirement to backup the files once in a week so I have used below settings for the script to backup wordpress. Make sure your command and path to file is correct else cronjob will fail.
Example:

/bin/sh /home/techpaste/sitebackups/files.sh >/dev/null 2>&1

wordpress files backup cronjobs
9.c. Similarly for database backups I have requirement of daily backups so I have used below cron settings, you can adjust it according to your settings for the script to backup wordpress. Make sure your command and path to file is correct else cronjob will fail.

Example:

/bin/sh /home/techpaste/sitebackups/db.sh >/dev/null 2>&1

Wordpress DB backup cronjobs

10. Once all above settings are correctly set, you will be able to see the db and files backups in both dbbackups and filesbackup folders respectively after the scheduled cronjob run like below.
DB backups:
wordpress db backups
Files backup:
wordpress files backups

11. This completes backing up wordpress DB and Files automatically.

Notes:
1. In each script I have used the below command to remove any files older then 7 days. So if you want to keep it for longer then modify the mtime accordingly. Here +7 means any files older than 7 days.

find $dest/ -maxdepth 1 -type f -mtime +7 -exec rm -f "{}" \;

2. Both scripts works very fine and tested in our hosting account with Cpanel. Do test it out properly in your test environments before using this, we will be not held responsible for any kind of data losses or incidents reported due to this.

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

4 Responses

  1. phil salt says:

    HI,

    Thank you for posting the above information. It has been very useful, & adds another tool in the backup toolbox.

    Be well,
    Phil

  2. Eric says:

    I get the errors below when running the cron job. Any thoughts on how to fix the errors?

    /home/XXXXXX/sitebackups/db.sh: line 21: syntax error near unexpected token `do

    /home/XXXXXX/sitebackups/db.sh: line 21: `do

Leave a Reply