If you're new here, you may want to subscribe to my RSS feed. So that you can read the latest updates about Web2.0 tools, Making Money Online, Tips in SEO, Ajax and many more. Thanks for visiting ProgramimiCOM!

It sucks to visit your site and find it hacked or maybe a extremely rare, but deadly technical glitch happened on the server. This only sucks when you don’t have a backup of your database, but it’s actually very easy to do. We’re going to use cPanel’s database backup tools to make a CRON based backup script that will send us, as an attachment, a backup of our database via E-Mail.

This actually isn’t that hard to do, the hardest part was figuring out how to send attachments with PHP’s mail() function. Thankfully, I found an excellent tutorial on it.

First of all, never store CRON files in your public_html directory, otherwise the whole world can access it. Store the file in the directory above it, e.g. /home/username/backup.php instead of /home/username/public_html/backup.php. Now we can move on to the script. Right after the opening PHP tag, we must tell the server to ignore all headers (thanks, Tim!):

 

<?php
#!/usr/local/bin/php -q
Just to keep things organized, we’re going to create two functions, one to get the database backup, and the other to send. However, we’ll only be calling one function, as it will call the function to get the database backup. There’s going to be a lot of headers, which I really can’t explain, but I can assure you that they tell the E-mail client there’s going to be both a message and an attachment.

 

function get_database_file($database_name, $domain, $username, $password)
{
//Construct the URL out of all our little pieces
$url = ‘http://’ . $username . ‘:’ . $password .‘@’. $domain .‘/getsqlbackup/’. $database_name .‘.gz’;
//Return the file’s contents
return(file_get_contents($url));
}function send_database_file($to, $from, $subject, $message)
{
global $database_name, $domain, $username, $password;
//Thanks to http://www.theukwebdesigncompany.com/articles/php-file-attachments.php for making this easy
$unique_sep = md5(uniqid(time()));
//Tell our E-mail client that we’re sending both a message, and an attachment
$headers .= “From: $from\n.
“MIME-Version: 1.0\nContent-Type: multipart/mixed;boundary=\”$unique_sep\”;\n.
“charset=\”iso-8859-1\”\nContent-Transfer-Encoding: 7bit\n\n .
“–$unique_sep\n.
“Content-Type: text/plain; charset=\”iso-8859-1\”\n.
“Content-Transfer-Encoding: 7bit\n\n.
$message.\n\n.
“–$unique_sep\n.
“Content-Type: gz; name=\”wp.gz\”\n.
“Content-Transfer-Encoding: base64\n.
“Content-Disposition: attachment\n\n;
//Get the file from our function
$file = get_database_file($database_name, $domain, $username, $password);
//This is where the file is actually attached
$headers .= chunk_split(base64_encode($file)) . “–$unique_sep–\n;
//Finally we can mail
if(mail($to, $subject, $message, $headers))
{
return true;
}
else
{
return false;
}
}

We’ve still got to declare some variables for our database, URLs, and user details. Last, but certainly not least, we’re going to send the backup!

 

//If your database is user_wp, ONLY PUT wp!
$database_name = “wp”;
//Include :2082, it’s cPanel’s port
//No www or http
$domain = “site.com:2082″;
//cPanel username and password
$username = “username”;
$password = “random_password”;
//Who to send the backup to
$email = “backups@programimi.com”;
//Appears in the from field
$from = “server@programimi.com”;//Send it off with the data in the E-mail message.
send_database_file($email, $from, $database_name .” Database Backup”, “The backup of “. $database_name .” was performed at “. date(“g:i A F j, Y”));
?>

Save the above (compiled into one) file as whatever you like, and remember, not in the public_html directory, the one above it! We still need to set up the CRON, hop into cPanel and click the CRON Jobs icon.

CronJob

Now click the standard button. I’ve made a little image that should get you through the next step:

CronMenu

Your database backup should arrive on whatver day, hour, and minute you told the server to run it! That’s all there is to it.