If you’ve never had a website go missing, then you’re very lucky. Your host can have a network error or hardware failure that loses your site’s files. A malicious hacker can penetrate your FTP server and replace your site with files of his choice. You might get curious about a new plugin, install it, and break your site to pieces in the process. No words really describe the panic you feel when you realize your website is gone!
If your host allows you to use CRON jobs, you can protect yourself from all those things by running a daily backup of your database and web site files. If you lose your site for any reason, you can put it right back in a few minutes.
A CRON job is script that your web server executes at a specified time. CRON comes from the word chronograph, and it is a time-based job scheduler for LINUX based operating systems. Most WordPress sites are running on such an operating system. If you have access to a CRON scheduler through your hosting control panel or have command-line access to your hosting server, you should be able to write a shell script to back up your database and site files.
The first thing you will need is a shell script. The script will connect to your database, export the whole thing to a file, and zip the file up so you can store it safely somewhere. Then, it will zip up all your website files.
If you have a host panel where you can create and edit a file, you can do this there. Otherwise, open any non WYSIWYG text editor and create a file called daily_backups.sh. The .sh file extension indicates this is a shell script file.
NOWDATE=`date +%a` # Sets the date variable format for zipped file: Sun
clear # clears terminal window
echo "Hi, $USER!"
mysqldump --opt -Q -h your-website-host-name --user=your-db-user-name --password=your-db-password your-database-name | gzip -v9 - > /www/public_html/backups/MySQL-$NOWDATE-yourwebsite.sql.gz
The first line is an indicator to the server for which shell processing language to use. Your server might require
#!/bin/bash or something else there. Use
echo $SHELL from the command prompt to determine your shell type if you have command line access.
TERM instruction tells the server what terminal type it is communicating with; in this case, a text terminal.
Next we create a variable named
NOWDATE and use a little script magic to set it to be equal to the abbreviation for the current day of the week e.g. Sun, Mon, Tue, etc.
mysqldump command will “dump” the entire database including create procedures and insert procedures for all the current data.
your-website-host-name with your website host name for your database. You can find this in your wp-config file if you don’t know it.
your-db-name with actual values. Again, these values are likely exactly what they are in your wp-config file.
/www/public_html/ with whatever your hosting account’s root path is.
yourwebsite with some meaningful name.
Now, save the file. You need to set the permissions on the file so that it is executable. If your cpanel has a cron scheduler, just add this file to the list of files it runs. If you need to edit your crontab from command line, see this tutorial.
To back up your site files, as well as your database, add a few more lines to your script.
echo "Zipping wordpress directory structure..."
tar -czf $HOME/backups/$NOWDATE.mywebsite.tar.gz $HOME/public_html/*
Hopefully, you do have a directory outside your public web root so that your backup files are stored in a location that is not accessible via the internet.
The tar command will pack the files up into a tarball for you (like a zip archive).
This article was contributed by Jennifer Nodwell, who worked as a systems analyst and developer on large-scale scale systems at EDS and Nortel where she wrote over 2 million lines of code. She has been building websites since 1997, and after realizing there was life beyond the cubicle, chucked the corporate life for free-lancing as a web developer in 2005. Now, she works almost exclusively with open source CMS applications like WordPress.