Backing up Your Website Automatically on a Dedicated Server

April 7, 2008

You already know the importance of backing up your dedicated server, but there are easier ways to accomplish that, which do not require periodic backups by hand. In fact, you can easily setup website backups to run automatically via what are called “cron jobs”.

What is a con job? A cron job is what’s known as a time-based scheduling service. This just means it does a specific job at a specific time. It’s most often used in Linux or Unix operating systems, so normally you can run them easily from cPanel or SSH.

Here are some helpful scripts that you can use to setup cron jobs with dedicated web hosting! These make backing up your website a breeze.

Site Backup via Cron

<?
$datestamp = date(“Y-m-d_H-i-s”); // Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING VARIABLES TO MATCH YOUR SETUP */
$filename= “Full_Account_Backup-$datestamp.tar”; // The name (and optionally path) of the dump file
$ftp_server = “123.123.123.123″; // Name or IP. Shouldn’t have any trailing slashes and shouldn’t be prefixed with ftp://
$ftp_port = “21″; // FTP port – blank defaults to port 21
$ftp_username = “anonymous”; // FTP account username
$ftp_password = “”; // FTP account password – blank for anonymous
$filename = “/home/YOURACCOUNT/” . $filename . “.gz”;

$command = “tar cvf ~/$filename ~/*”;
$result = exec($command);

$command = “gzip -9 -S .gz ~/$filename”;
$result = exec($command);

// set up basic connection
$ftp_conn = ftp_connect($ftp_server);

// Turn PASV mode on or off

ftp_pasv($ftp_conn, false);

// login with username and password
$login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);

// check connection
if ((!$ftp_conn) || (!$login_result))
{
echo “FTP connection has failed.”;
echo “Attempted to connect to $ftp_server for user $ftp_username”;
exit;
}
else
{
echo “Connected to $ftp_server, for user $ftp_username”;
}

// upload the file
$upload = ftp_put($ftp_conn, “foo.tar.gz”, $filename, FTP_BINARY);

// check upload status
if (!$upload)
{
echo “FTP upload has failed.”;
}
else
{
echo “Uploaded $filename to $ftp_server.”;
}

// close the FTP stream
ftp_close($ftp_conn);

unlink($filename); //delete the backup file from the server
?>

**Make sure that this script is with a .php extension, and the file has 755 permissions. You also need to change the first few variables to those of your site setup and add a cron job in Cpanel with a path to the script such as php /home/username/path-to-the-php-script (replacing path-to-the-php-script with your actual path to it).

More Dedicated Web Hosting Help!

Comments are closed.