Hello Ashok!

My Experience with affiliate and internet marketing

Archive for the ‘PHP’ Category

2
Oct
2006

If you are running a database driven website, then you must be aware of that how much important it is to take backup of your database regularly. I run many websites and some of them are database driven. The Data is generally user created i.e. new profiles are added, exisiting profiles are modified etc. In this case it become neccessary to take backup of database more frequently.

Initially I used to take backup of my database through phpmyadmin . But this process was very time consuming I had to spend almost half hour to one hour to take the backup.

Later I decided that I should automated this task, so I started searching on net to find some solution. Although I found many solutions, but they were not perfect for my requirement. Finally with the help of the ideas which I got through those solutions, I created my own solution to take the backup automatically.

I divided the process of taking backup in three parts and executed these task through cron jobs.

First part of the task was to take the backup of database using mysqldump command. This command takes the backup and store it in server.
The script to take backup is very simple. It consist of only two lines.

<?php
$filename=’backupfile’.date(’dmy’);
exec(”mysqldump -hlocalhost -uusername -ppassword databasename > /home/admin/domains/yourwebsite.com/public_html
/backupdirectory/$filename.txt”);
?>

First line create a file name which is consist of date parmeter to generate unique filename every day.
Second line take the back and store in the yourwebsite.com/public_html/backupdirectory.

Since the backup is in text format, In second step I convert text file in gzip format to compress it.
The script to convert text file into gzip file is

<php
$filename=’backupfile’.date(’dmy’);
exec(”gzip /home/admin/domains/yourwebsite.com/public_html/
backupdirectory/$filename.txt”);
?>

In third step, I send this file through email to my inbox. Since with ordinary mail function, we can not send attachment. I use phpmailer to send this file. The script to do the task is:

<?php
require(”class.phpmailer.php”);
$filename=’backupfile’.date(’dmy’);
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = “localhost”; // specify main and backup server
$mail->SMTPAuth = false;
//$mail->Username = “youremail@yourdomain.com”; // SMTP username
//$mail->Password = “mailpassword”; // SMTP password$mail->From = “youremail@yourdomain.com”;
$mail->FromName = “From Name”;
$mail->AddAddress(”destination@youremail.com”); // Where you want to send backup
$mail->AddAttachment(”/home/admin/domains/
yourwebsite.com/public_html/backupdirectory/$filename.txt.gz”);
$mail->IsHTML(true); // set email format to HTML

$mail->Subject = “Subject of Email”;
$mail->Body = “Body of Email”;
$mail->AltBody = “Alt Body of Email”;
if(!$mail->Send())
{

echo “Message could not be sent.”;
echo “Mailer Error: ” . $mail->ErrorInfo;
echo ”
“;
// exit;
}
?>

I run these three tasks through cron jobs, which is set to run everyday and I got the backup in my inbox daily. This saves a lot of my time in taking backup of my database, and I always have the latest backup of my database with me. Are you still using phpmyadmin to take the backup?

Technorati Tags: , , ,

ashok

Comments (1)

Web Hosting India