Skip to content

Making automatic backups for your website

I have several websites that I administer. As any good site admin will tell you, making consistent backups is vital. I have a script that I originally got from this site that I use to back my sites up automatically, using cron. Here’s how I do it, and how I have modified the script slightly for my own purposes.

First off, create a php text file. I named mine site_backup.php. You will need to modify this with your site and email info. This script will create a archive file with the name backup.<day>.tar.gz, where <day> is the number of the current day of the month. Every day of the month the number will change, thereby leaving you with a month’s worth of backups in your /home directory, that will be overwritten the next time the day of the month is the same.

<?php  $emailaddress = "yourname@emailaddress.com";  $target = "/home/".get_current_user()."/backup.".date(d).".tar.gz";  if (file_exists($target)) unlink($target);  system("tar create preserve gzip  file=".$target." --exclude-from=/public_html ~/mail",$result);  $size = filesize($target);  switch ($size) {    case ($size>=1048576): $size = round($size/1048576) . " MB"; break;    case ($size>=1024);    $size = round($size/1024) . " KB"; break;    default:               $size = $size . " bytes"; break;  }  $message = "The <yoursitename> website backup has been run.\n\n";  $message .= "The return code was: " . $result . "\n\n";  $message .= "The file path is: " . $target . "\n\n";  $message .= "Size of the backup: " . $size . "\n\n";  $message .= "Server time of the backup: " . date(" F d h:ia") . "\n\n";  mail($emailaddress, "<yoursitename> backup message" , $message, "From: Website <>");  ?>

One of my sites has a few directories that are loaded with graphics, all of which I have backed up elsewhere. I wanted to exclude those directories from the tar archive. If you have used tar by itself, you know this is an easy thing to do, but I had not done so in a script before. Fortunately, you do it the exact same way you would usually do it from the command line. Since we are using the “system” call in PHP, all we need to do is make sure our syntax is correct for tar.

After a quick look at the GNU tar man page to confirm how to use

--exclude

I noticed that I didn’t need to put all the directories directly in the script. This was news to me, and so I was happy I had read through the man page, discovering the

--exclude-from

option.

I created a text file, which I’ll call backupexclude, and included it in my /home directory. In it I listed each directory I wanted to exclude, one directory per line. Here’s an example

~/foldertoexclude1 ~/directory/subdirectorytoexclude1 ~/skipthis

Simple, huh?

To run it from the command line, you just type “php ~/site_backup.php” without the quotes. This way you can backup your website and email folders any time you want.

To call up the script using cron, add a line like this to the cron file (or added in whatever way your web host has configured cron jobs to be added by you).

# run at 2:15am every day of the month 15 2 * * *     php ~/site_backup.php

4 Comments

  1. matthew

    Sorry for the crossed out lines in the beginning of the script when tar is called. My blog software uses a double dash to initialize and terminate the strikeout, and for some reason, even though the text is set as "preformatted," parts are still being struck out. Sorry.

    The tar line should have double dashes in front of "create," "preserve," "gzip," and "file."

    I presume it is because of the presence of quotation marks in the line.

  2. Tom Haddon

    I don’t really see why you would want to use PHP for this. Why not just a shell script? A lot cleaner and easier to understand…

  3. matthew

    Hey Tom. Thanks for the comment. Yeah, I admit that using PHP may not be ideal, but I was studying it at the time I first found it, and since I was looking at the script anyway I figured, "Why not use it?" and see if it works well.

    It’s done the job for me for a few months, reliably and easily, so I thought I would share it.

    Besides, my shell scripting ability isn’t as good as my PHP…

  4. Jerome

    Hi Matthew, nice tips. I have learn another way to back up site, but it was on line back up and run automatically. So, it was very easy to be used by newbies. Anyway, this post add my knowledge about how to back up using php. Thanks for the info.

Comments are closed.