Archive for January 30th, 2008

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 January 30th, 2008


Categories

 

January 2008
S M T W T F S
« Dec   Feb »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Site Content License

The content on this site is
© Matthew Helmke.

Unless otherwise noted, content on this site is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

Creative Commons License

Blogroll

Interesting Sites

Archives

Spam Blocked