Backup Plan: How we backup our critical files. (Part 2 of 2)

Last week we talked about how Netvantage Marketing shares files using a common Dropbox account and how it allows us multiple backup copies of our files at the same time. This week we will discuss the idea of backing up website data and emails from your cPanel server. Hold on to your butt…this one is long and dense!

Backing Up Website Data & Emails

We are about to discuss a topic that many companies don’t think about at all. When was the last time you considered that you might need to have a backup of your entire website, database(s), and/or emails? I created this solution about a year ago when I was making updates to our website and falsely assumed that our hosting provider was making nightly backups of our site. I got myself into a bit of a pickle and was lucky to be able to dig out of it. From that point forward, I always made sure to have a recent and working backup of our site.

First I’ll lay out the overall concept, then I’ll give you the detail of how it’s set up. Check out my badass drawing!

Netvantage Online Systems Backup Scheme

Our website is hosted on a LAMP (Linux, Apache, MySQL, PHP) server with cPanel as the management console. I have instructed cPanel to make a full backup of our account once per week, compress that backup into a tar.gz file, and send that file via FTP to a completely separate physical server. The second server receives the file and stores it for 10 days so there are usually two weekly copies of the backup available. Four times per week the second server replicates itself to an Amazon S3 bucket and maintains a set number of daily, weekly, and monthly backups in the Amazon cloud.

This provides multiple copies of our data in at least three physical spaces—one of which is Amazon’s cloud service so, like our Dropbox solution from last week, I feel comfortable that we’re not losing any of this data either.

How To Set Up The Multi-Server Backup Fanciness

I typically use cPanel servers so these steps are geared towards that type of server. With some effort and good Google Fu, I think you could get this to work on most LAMP servers. Here are the steps.

Create a file called backup.php. In the backup.php file, copy/paste the following code: (…or download a ZIP of the backup.php file here, unzip it, and open it in your favorite text editor.)

<?php
/*
* cPanel Backup Script originally created by Eric Zhang
* https://www.ericzhang.me/projects/cpanel-auto-backup/
* 
* Downloaded from Netvantage Marketing
* https://netvantagemarketing.com/backup-plan-backup-critical-files-part-2/
*/

$auth = base64_encode("cPanelUsername:cPanelPassword");
$domain = "https://YourDomainHere.com:2083";
$theme = "paper_lantern";
$ftp = true;
$ftp_server = "ftp.server.com";
$ftp_username = "FTPUsername";
$ftp_password = "FTPPassword";
$ftp_port = "21";
$ftp_directory = "/";

// Do not change below
$url = $domain . "/frontend/" . $theme . "/backup/dofullbackup.html";
$data = array();
if ($ftp) {
 $data["dest"] = "ftp";
 $data["server"] = $ftp_server;
 $data["user"] = $ftp_username;
 $data["pass"] = $ftp_password;
 $data["port"] = $ftp_port;
 $data["rdir"] = $ftp_directory;
}

$options = array(
 'http' => array(
 'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Basic $auth\r\n",
 'method' => 'POST',
 'content' => http_build_query($data)
 ),
 'ssl' => array(
 'verify_peer' => false,
 'verify_peer_name' => false,
 'allow_self_signed' => true
 )
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
 exit("Error backing up server.");
}

echo $result;
?>

Above code courtesy of Eric Zhang.

There are several things you have to change to get this working. They are:

  • Line 10: replace “cPanelUsername” with your actual cPanel username and “cPanelPassword” with your actual cPanel password.
  • Line 11: replace “YourDomainHere” with your domain name, and, of course, change the .com to .net or .co.uk or whatever you need to do there.  (Don’t mess with the “:2083” . That’s the port number and needs to remain where it is.)
  • Line 12: replace “paper_lantern” with your theme name.*
  • Line 14: replace “ftp.server.com” with the IP address of your second server—the FTP server that will be receiving & storing the backup file.
  • Line 15: replace “FTPUsername” with the username used to log in to the FTP server.**
  • Line 16: replace “FTPPassword” with the password used to log in to the FTP server.
  • Line 18: you can specify the directory in which you want the backup stored or you can leave it as the base directory.

paper lantern theme name* NOTE:  “paper_lantern” is a common theme name, as is “x3.” You need to have an accurate theme name, however, or this won’t work. When you log in to cPanel, look at the bottom of the left column and you’ll see a box that looks like this. Whatever it says inside that box is your theme name.

** NOTE 2:  I suggest not using your main cPanel username & password here. Although they would work, it would be much easier to create a new FTP user in cPanel and then specify where that user is allowed to save files. This makes operating the script much easier.

Once you have your backup.php file created and modified, upload it to the root directory of your cPanel server. Note that the root directory of your site is the highest level directory. It is not where your web files are stored—it’s one level up from that.

Automation is the Key

Create New Cron JobNow that we’ve got the script in place, we need to set up a cron job to handle executing the script on a regular schedule. In your cPanel interface, search for “cron” and you’ll find an icon called “cron jobs.” Click that and get ready to add a new cron job.

Drop down “Common Settings,” choose Once per Week ( 0 0 * * 0 ), down below in the “Command” text box type

php -f ~/backup.php

and click “Add New Cron Job.” Boom! Done!

FTP Server Cleanup

Now that we have the backup files coming into our FTP server, we need to make sure to remove old files to keep things tidy. Log in to the cPanel on the FTP server, go to the Cron Jobs section, and create a new job. This time, have the job run every day and set the Command to:

find /home/cPanelUsername/PathToWhereYoureSavingFiles -type f -mtime +10 -exec rm {} \;

Be sure to change “cPanelUsername” to your actual username and change “PathToWhereYoureSavingFiles” to the actual path. Don’t forget the slash that goes between those two values! As an example, the exact command I use is:

find /home/nvm/public_html/nvm.backup -type f -mtime +10 -exec rm {} \;

One more item to note; the last element in that path is “nvm.backup” which is the directory in which the backup files are stored. So you are only specifying the directory to look in here, not the actual file name.

This command will look in the directory you specified in the path we just set up; it will find all the files in that directory, it will look at the files’ last modified time and compare that to the current date. If any files are ten days or older, it will delete only that file. If you want to change that ten days time period you can change it to whatever you want by altering the “+10” value in the command. For example, +14 would make it only delete files that are fourteen days old (or older); +30 would delete files that are a month old; etc.

That’s It!

If you followed this correctly and everything worked how it should, within a week you will have a full backup of all your website files, emails, databases, and any other detritus that in your hosting account. This article makes the process sound more complicated than it really is. Just take your time in setting things up and think logically through the process and the connections between the servers. It’s really not a super difficult process, just a little intricate. If you have questions or if you can think of a better way to do this, I’d love to hear it. Hit me up in the comments and I’ll get back to you.

Bonus Backup

As I mentioned before, after all the above is done and I have a successful backup of our Netvantage Marketing website, emails, and so forth, all that data is replicated again to Amazon S3. The full setup of how to do that is beyond the scope of this article, but I will point you in the right direction. To accomplish this, you must have root access to the FTP server and be able to work within Web Host Manager (WHM). Go to “Backup Configuration” and here are the settings I successfully use:

GLOBAL SETTINGS

  • Backup Status: Enable
  • Backup Type: Compressed
  • Maximum Destination Timeout: 2700 seconds
  • Maximum Restore Timeout: 21600 seconds

SCHEDULING AND RETENTION

  • Backup Daily: Yes
  • Days: Sunday, Monday, Wednesday, Friday
  • Retain 16 Daily Backups
  • Backup Weekly: Yes
  • Days: Thursday
  • Retain 12 Weekly Backups
  • Backup Monthly: Yes
  • On The: 1st of the month
  • Retain 6 monthly backups

FILES

  • Backup Accounts: Yes (you can enable/disable specific users if you like)
  • Backup Suspended Accounts: Disable
  • Backup Access Logs: Enable
  • Backup Bandwidth Data: Enable
  • Use Local DNS: Disable
  • Backup System Files: Yes

DATABASES

  • Backup SQL Databases: Per Account and Entire MySQL Directory
  • Configure Backup Directory: /backup (this won’t matter if you are not storing the backups locally and instead using Amazon S3 or something similar)
  • Retain backups in the default backup directory: No
  • Mount Backup Drive as Needed: Disable

ADDITIONAL DESTINATIONS

The only destination I have is Amazon S3. You’ll have to do your own research on how to set that up, though.  😉

If you’re curious about the cost I’ll tell you. We spend between $3 to $5 per month in data storage and transfer fees for the 67 GB we move and store on Amazon’s service. For between three to five dollars per month, that’s HUGE peace of mind!

Again, if you have questions, I’ll do my best to answer. Good luck and get to backing up!

Jerod Karam

Jerod Karam is Vice President of Technical Operations at Netvantage Marketing, an online marketing company specializing in SEO, PPC and social media. Jerod consults with internal teams and external clients on all manner of technical projects, manages the flow of information surrounding the company's online objectives, manages relationships with external partners and suppliers, and is a constant bother to everyone in terms of maintaining online security.

Leave a Reply

Your email address will not be published. Required fields are marked *