Make a website backup and email it to your Gmail account
How to backup your Joomla or Wordpress Installation and have the backup emailed to your Gmail account
If you want to take control over your own backups but are scared that you are going to forget to run them regularly, here is a method to make a backup of your website and have the backup emailed to your Gmail account.
Let's take a look at the advantages of doing this.
- You are not dependent on having your hosting provider make backups for you. Yes, WE make backups on a daily, weekly and monthly basis, but frankly, you might be hosting with a place whose backups are patchy or non-existent, and our backups sometimes also fail due to server load or other snafu's. In this way, you know that you always have your own backups available to you.
- The method that I am going to show you entails that the backups run automatically without you having to remember to a thing!
- This method of doing backups will not use your ISP bandwidth, but rather your server bandwidth. Please note therefore that if you use this, you should make sure that this will fit into your server bandwidth allocation (this could be a problem if you are hosting on a South African server!)
Click on the read more link to get the full tutorial!
The steps to setting up your automatic backup
In broad terms, we will do the following:
- Set up a Gmail account specifically to receive the backup
- Set up the backup php script on your server
- Set up a CRON job to regularly run the backup.
This tutorial does become a bit technical, however, I will try and keep it as simple as possible.
You will need to know how to do the following:
- Use FTP to upload files to your hosting server (or use your friendly File Manager interface through your Cpanel) and how to change folder permissions on your server.
- How to use the MySQL dump file that you are sending to your Gmail account to one day restore your website - this falls outside the scope of this tutorial. Just rest assured that if you have the file, almost any webmaster who works with Wordpress or Joomla will be able to restore your site using this file, even if you do not know yourself.
Step 1: Set up a Gmail account to receive the backup
This is relatively easy to set up. The reason why you want to set up a separate Gmail account for this is that you want to ensure that your backups are isolated and easy to find, and that you maximize the amount of space available for the backups, however, the chances of that actually happening is slim. Remember, you will be sending a zip file to your Gmail account of a couple of MB each time, and you have about 7GB available in your Gmail account. If your backup is 5MB big, you can send 1400 backups to the account. If you do this once a week, it means that you can send 26 years' worth of backups to your Gmail account before you run out of space!
But it is still worth setting up a separate account so that you keep your backups and your personal email separate.
Step 2: Set up the backup php script to create the backup file and send it as an attachment to your gmail account.
Before we climb into this we need to discuss a couple of things. Firstly, you need to decide what you want to backup. Remember that if you are running a Joomla or Wordpress website, your website content is not actually stored in the file system of your server, but in a MySQL database. If you therefore try to back up the files in the public_html folder, you will not really be backing up the website!
On the other hand, if your website is a static HTML type site created by Frontpage, Publisher or Dreamweaver, you do want to backup the files.
Also, if you are running a Joomla installation with Virtuemart and you regularly upload products, the product images ARE stored in the file system and not in the database. If you just backup the database therefore, you will not have the images backed up at the same time.
You also need to decide how often you want to back up your website. If you regularly add content, you will want to back up more regularly.
So here is a handy guide to decide what to backup, and how often:
|
Website designed in |
Content Changing How Often |
Frequency of Backup |
What to Back Up |
|
Joomla |
Regularly |
Weekly |
Database |
|
Joomla |
Seldom |
Monthly |
Database |
|
Joomla + Virtuemart or Image gallery or Sobi |
Regularly |
Weekly |
Database and files |
|
Joomla + Virtuemart or Image gallery or Sobi |
Seldom |
Monthly |
Database and files |
|
Wordpress |
Regularly |
Weekly |
Database |
|
Wordpress |
Seldom |
Monthly |
Database |
|
Wordpress + Image Gallery or Business Directory |
Regularly |
Weekly |
Database + Files |
|
Wordpress + Image Gallery or Business Directory |
Seldom |
Monthly |
Database + Files |
|
Dreamweaver or other static HTML editor |
Regularly |
Weekly |
Files |
|
Dreamweaver or other static HTML editor |
Seldom |
Monthly |
Files |
Please note that the frequency of the backup will be controlled through the Cron job that we will set up in Step 3.
Firstly, we need to set up the backup php script. We will set up separate backup files for the Database and for the Files, in this way you can control the frequency of the backups for the files and the database separately.
Copy the following code into a text editor and save it as 'backup.php' on your hard disk, make the necessary changes and then upload it to the root folder of your website (this is normally the public_html folder)
".$sqlFile;
$createZip = "tar cvzf $attachment $sqlFile";
exec($createBackup);
exec($createZip);
$fileatt = $attachment; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "DatabaseSQL.tgz";
/*CHANGE THIS INFORMATION TO FIT YOUR WEBSITE AND GMAIL ADDRESS */
//The email from address does not actually need to exist!
$email_from = "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
"; // Who the email is from
$email_subject = "Backup Database"; // The Subject of the email
$email_to = "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
"; // Who the email is to
/*CHANGE UP TO THIS POINT */
$headers = "From: ".$email_from;
$email_txt = "Backup file"; // Message that the email has in it
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
unlink($sqlFile);
unlink($attachment);
?>
In order to backup your database, you are going to need the following information:
- The name of your database
- The user of your database
- The password of the user
You can get this information out of the wp-config.php file on your server if you are making a Wordpress database backup, or the configuration.php file if you are making a Joomla database backup.
You also need to create a folder called 'tmp' in your public_html folder of your web server and make sure that this folder is writeable by having folder permissions of 755 (this is on the TM4Y Hosting server, some other servers might require you to make it 777).
To test it, run the following in your browser
http://www.yourwebsitename.co.za/backup.php
It should actually just execute without any messages to you but you should now be able to log into your gmail account and see if you have received an email with an attachment. If you don't see anything within 5 minutes, check the Junkmail folder!
Now, you might run into a case where your hosting provider does not allow the direct execution of Linux commands through a script, in that case you will get an error message when you try to execute backup.php that will tell you that 'exec' is not allowed.
In that case we need to set up an alternative script that will still extract the information from the database but does not make use of the exec command. In that case, use the following script and also save it as backup.php in your document root folder (so you would then rather use this one than the one above)
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
"; // Who the email is from
$email_subject = "Backup THEWEBSITE NAME Database"; // The Subject of the email
$email_txt = "Backup file"; // Message that the email has in it
$email_to = "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
"; // Who the email is to - GMAIL ACCOUNT
/*CHANGE UP TO THIS POINT */
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
unlink($sqlFile);
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$tmpDir = "tmp/";
$prefix = "db_";
$sqlFile = $tmpDir.$prefix.date('Y_m_d').".sql";
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link) or die(mysql_error());
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j]))
{ $return.= '"'.$row[$j].'"' ; }
else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$handle = fopen($sqlFile,'w+');
fwrite($handle,$return);
fclose($handle);
}
?>
In order to back up the files on your server, I would suggest doing so only if you regularly add images to your image gallery or products to your shop, or if you regularly add components and plugins to your site.
This is the script to make a backup of your files.
Please note that you need to save this script with the filename: backupfiles.php
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
"; // Who the email is from
$email_subject = "Backup YOURWEBSITE Database"; // The Subject of the email
$email_txt = "Backup file";
$email_to = "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
"; // Who the email is to - YOUR GMAIL ACCOUNT
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
unlink($attachment);
?>
Also note that you can only backup the files on your server with this script if you have access to the exec command.
Step 3: Set up a cron job to run the script
This step sets up a CRON job to automatically execute the script on a weekly or monthly basis.
CRON jobs run on hosting servers and are readily available on most Linux servers. If you have access to your Cpanel it is not that difficult to set up, however, what is sometimes difficult is to figure out how to set up your command so that it runs on your server!
In your Cpanel, look for the 'Advanced' section and then click on Cron Jobs.
Depending on your version of Cpanel, you will get a different interface, so I am not going to show the interface here. If you get the option between 'Standard' and 'Unix Style' - select Unix Style.
Firstly, before we get to the actual command, you need to set up the time interval.
Here are the two most common time interval settings that you will require - weekly and monthly
For Weekly:
Minute: 0
Hour : 0
Day : *
Month : *
Weekday : 0
This will run the Cron job every Sunday at Midnight
For Monthly:
Minute : 0
Hour : 0
Day : 1
Month : *
Weekday : *
The actual command is a bit more tricky since there are various formats and some work on some servers and not on others.
Here is a common format that seems to work on most servers:
lynx -dump http://www.yourwebsitename.com/backup.php
To backup the files on your server, create a second cron job and make the command:
lynx -dump http://www.yourwebsitename.com/backupfiles.php
Please note that you can have different time intervals for the different cron jobs, so your files can be backed up once a month and your database can be backed up once a week, as an example.
If your server does not have lynx enabled, you might have to make the command:
/usr/local/bin/php -f /home/cpanelusername/public_html/backup.php
Where /usr/local/bin/php is the path to your PHP installation and
/home/cpanelusername/public_html is the full path to your document root on your server.
You might have to find out from your hosting provider what the full path is to your php installation as well as the full path to your document root.
Normally the format is /home/cpanelusername/public_html if you are hosting on a CPanel installed server.
To test that the cron works you might want to execute it every 5 minutes to start off with (else you might have to wait a week to see if it runs!)
To test every 5 minutes change the interval settings for your Cron job as follows:
VERY IMPORTANT!! Remember to change this back to weekly or monthly as soon as it is working, else you are goint to make your hosting provider very angry and fill up your Gmail account very quickly!
Minute : */5
Hour: *
Day: *
Month: *
Weekday : *
Thanks to the following references for the code for backing up your database:
http://davidwalsh.name/backup-mysql-database-php
and the following reference for sending an email with an attachment (or more than one attachment):
http://www.codewalkers.com/c/a/Email-Code/PHP-Email-Attachment-v2-UPDATED-091106/
| Next > |
|---|
