MaGiAsHoK Experiences

09 Jan, 2008

Database Backup

Posted by: MaGiAsHoK In: PHP


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




Securenext Software Pvt Ltd, Chennai, India


No Responses to "Database Backup"

Comment Form

Categories

Calender

July 2010
M T W T F S S
« Jun    
 1234
567891011
12131415161718
19202122232425
262728293031  

Alexa Rank


RSS India News

  • 'Unani and ayurveda docs can conduct surgery' July 29, 2010
    The Madras high court has said registered practitioners in siddha, ayurveda, homeopathy and unani are eligible to practise surgery, obstetrics and gynaecology, anaesthesiology, ENT, ophthalmology, etc. […]
  • Number of Indian students going to US falls, rises for UK July 29, 2010
    More than twice the number of Indian students who applied to fly abroad for entry into colleges this year chose the UK over the US. […]
  • CVC X-rays 16 Commonwealth Games projects, detects brazen corruption July 29, 2010
    The Commonwealth Games edifice is crumbling under thickening suspicions of corruption and inefficiency, with the CVC referring one of the several cases of corruption bedeviling the event to the CBI. […]
  • PF scam: 3 HC judges spent money on beauty treatment July 29, 2010
    The tenure of the three former Allahabad HC judges as district judge of Ghaziabad between 2003 and 2006, saw them sanction fraudulent withdrawal of Rs 2.73 crore from PF accounts of class III and IV employees. […]
  • Qureshi to blame for failure of India-Pak talks: PM July 29, 2010
    Breaking his silence on the recent India-Pakistan dialogue, PM Manmohan Singh on Thursday blamed Pakistan's pugnacious foreign minister Shah Mahmood Qureshi for the failure of the Islamabad talks. […]
  • Seven laws of attraction July 29, 2010
    Surprising factors come into play when you are looking for a mate, we find out […]
  • UK to talk to Pakistan on ending terror in India July 29, 2010
    Addressing a joint press conference with PM Manmohan Singh, visiting British PM David Cameron today said he will discuss with Pakistan 'frankly, clearly and openly' on the need to reduce and eliminate terror from India. […]
  • Pak 'saddened' by Cameron's remarks July 29, 2010
    Pakistan today said it was 'saddened' by British PM David Cameron's remarks that it was exporting terror, adding they did not reflect ground realities. Cameron's remarks came days before Asif Ali Zardari's UK visit. […]
  • Tips for steamy sex July 29, 2010
    […]
  • Tendulkar slams 5th double ton in Test cricket July 29, 2010
    Milestone man Sachin Tendulkar re-wrote the record books, hammering the fifth double Test century to add another feather to his well-adorned cap. […]

RSS Cricket News

Twitter Goodies

Suscribete GRATIS

Configurar el USUARIO Feedburner!!
KnxDT

Feedburner Subscription Widget


Hotmail

Gmail


Search Engine Optimization and SEO Tools


Open Source Shopping Cart, Chennai, India Bench on Demand IT Professionals, Chennai, India Ruby on Rails: Rapid Web Development, Chennai, India


Videos, Slideshows and Podcasts by Cincopa Wordpress Plugin

Ads Plugin created by MaGiAsHoK