Backup Your Website and Database on GoDaddy

This post, unfortunately, is not about any photographs I’ve created recently, but is rather tailored to photographers who are looking to automatically backup their website. I’m writing this because recently I had to setup an automatic backup of the Intertwine website. After googling around, I didn’t find a complete enough example of how to backup your website and database automatically and daily on GoDaddy. Sure you can find pieces of how to do just the website in some places, and other pieces of how to backup any database you may have, but a complete guide doesn’t exist. So, in hopes of driving more traffic, and helping out those that are not IT and web experts I write the following.

To begin with, this process will only work if your site is currently being hosted on a linux server; if you are on a windows server, then I highly recommend switching it (it’s quick and free to switch). I won’t go into the details of the advantages of one over the other, but you can get a quick answer here.

Next, for my example, I have to assume some sort of file structure on the web server; since everyone’s may be a bit different, your mileage may vary, but if you following along closely, you shouldn’t have any problems adjusting this process to fit your needs. So, I’m going to assume the following file structure:

dir Backup Your Website and Database on GoDaddy

In the above structure, the highest level directory to which we have access is within /html/. In there, you may have any number of other directories and files, but I’m going to assume you have at least the directory /_db_backups/ and /images/ as well as some .html, .php and .css files.

The way we are going to automate the backing up of our website files and any database we have is through something called cron jobs; cron is basically a time-based scheduler which will run any specified script (a list of commands) that we, the user, will define for it. In our case, we will define a script which will tell the server which files to backup and to where to back them up. So, there are two parts to this automation process:

  1. Writing the script
  2. Scheduling a cron job

Let’s begin, shall we?

1. Writing the script

The script consists of a series of commands written in a language the server will understand. We will be creating it as a simple text file, however I recommend you use notepad++ to do all the editing of the script. This isn’t required, in fact any text editor will do, it’s just that notepad++ makes things easier with features like syntax highlighting, etc. If, instead of copying and pasting yourself, you’d like to download the actual file, then click here. Now, let’s start scripting! Open notepad++ and paste the following code into a new document:


#!/bin/bash

#remove old files
rm $HOME/html/_db_backups/*.gz

So what’s going on here, what does it mean? Any line of code that is preceded with a hash (#) symbol, is considered a comment and won’t be run by the server; it’s a way in which we can write descriptive text regarding what the code does. So, in these three line of code, we only actually have one that does anything: the very last line. This line of code tells the server to remove (rm) any files with an extension .gz that reside in the /_db_backups/ directory. We’ve written this line because we want to delete any old backups (.gz files) we have and keep only the latest one.

Next, paste the following after the lines we’ve already pasted:


#section for database backup
NOWDATE=`date +%m%d%y` # Sets the date variable format for zipped file
/usr/bin/mysqldump -h HOST -u USER -pPASSWORD DATABASE | gzip > $HOME/html/_db_backups/db_backup_$NOWDATE.sql.gz

This is the part where we tell the server to begin backing up our database (such as a WordPress database), and this is the part that will required you to put in information specific to you and your database. First we define a variable, NOWDATE, and set it to the current date. Then, we tell the server to dump (download) all the data of the specified database, then it gets compressed (gzip), named db_back_date.sql.gz (where date will be the current date) and placed in the directory /_db_backups/. Each of the underlined words in the above line of code, will have to be adjusted by you with the information pertinent to your MySQL database. To get this information, login to your GoDaddy Hosting Control Center, click on your domain name, then choose MySQL – located within the green Databases tab found at the top of your screen. Once there, press the ‘Edit/View Details’ (the icon that looks like a pencil) button of the database you want to backup. Here you’ll find the ‘Host Name’, ‘User Name’ and ‘Database Name’ which you’ll need to use and replace the underlined words in the code above. The password, naturally, only you should know and will not be shown.

When editing the above line of code with your password, notice that there is no space after -p like there is for -h HOST and -u USER. So, if your password is meow then you would write -pmeow.

Moving on, we will tell the server to backup our site files, paste the following after the lines we’ve already pasted:


#section for website backup
mkdir -p $HOME/html/_db_backups/site # create site directory if it doesn't already exist
cp -r $HOME/html/*.html $HOME/html/_db_backups/site # copy all .html files
cp -r $HOME/html/*.css $HOME/html/_db_backups/site # copy all .css files
cp -r $HOME/html/*.php $HOME/html/_db_backups/site # copy all .php files
cp -r $HOME/html/images $HOME/html/_db_backups/site # copy /images/ directory
tar -cvzPf $HOME/html/_db_backups/site_backup_$NOWDATE.tar.gz $HOME/html/_db_backups/site/* # zip site files
rm -r $HOME/html/_db_backups/site/* # remove copy of site files

Here, we are doing several things. First, we create a new directory (mkdir) called /site/ within the /_db_backups/ directory. Then we copy (cp) any .html, .css and .php files to this /site/ directory; the same goes for the entire /images/ directory. Once we have a copy of all our site files (located in /_db_backups/site/) we compress them (tar), name it site_backup_date.tar.gz (where date will be the current date) and place this compressed file in /_db_backups/. Finally, we remove (rm) all our site file copies since we’ve just compressed them into their own file.

And that’s it for the script! All that’s left is to save it; so click File, Save As… and choose any name, just make sure it ends with .sh. In my case, I’ve named it backup.sh. We now have code that will backup our database and site files. If, instead of copying and pasting yourself, you’d like to download the actual file, then click here.

2. Scheduling a cron job

Still with me? We’re almost finished! We now have to tell the server which script to run and when to run it; but before we can do that we have to actually put the script on our sever. So, in your GoDaddy Hosting Control Panel, click on ‘FTP File Manager’ found as the third entry in the green ‘Content’ tab located at the top of the page. Once there, click on the /_db_backups/ directory, then click on the Upload button and choose your backup.sh file. If done correctly, this will upload your script to the /_db_backups/ directory. You can see in the following screen shot that our file uploaded correctly to the specified directory.

ftp dir 640x238 Backup Your Website and Database on GoDaddy

Once we have our script in the proper directory, we have to give access to the server to allow it to actually run the script. So, while still in the FTP manager, navigate to the /_db_backups/ directory, check the box located to the left of our backup.sh file, click the permissions icon towards the top of your screen, and then make sure the option for ‘Executable’ is checked. Now click save and you’re all set to go!

perm 640x377 Backup Your Website and Database on GoDaddy

We can now finally setup the cron job. In the Hosting Control Panel, click on ‘Cron Job Manager’, found as the first entry in the green ‘Content’ tab. Once there, click on ‘Create Cron Job’ and fill in the ‘Cron job title:’ with any name you want, specify the ‘Frequency:’ as daily, and for ‘Command:’ browse to your backup.sh file in the /_db_backups/ directory. Before leaving this page, you may want to set an email address so you can get notified of how this cron job is doing; set your email address towards the left side of the screen.

cron job 640x132 Backup Your Website and Database on GoDaddy

And that’s it! Your site and database will now be backed up daily to the /_db_backups/ directory.

Related posts

No related posts yet.

3 Comments

  1. Very detailed and clear tutorial. Thanks!

  2. Very detailed and clear instructions. Will definitely want to use this. Thanks!

  3. Great write-up. Very clear. Can’t wait to try it on my Moodle site, elearning.mpitta.com, once I finish getting the site set up.