Sunday, June 5, 2011

mysql database dump using php

Sometimes application needs database backup or rather dump on a regular interval or on request.

So we need a faster and easy way to create mysql dump and also compress those database files to save disk space.

 

This could be a very easy if we use some shell commands through php and create mysql dump. See below how we could achieve this.

 

 

<?php

//Database details

$dbhost = "localhost";

$dbname = "bear44_visitusa";

$dbuser = "bear44_visit";

$dbpwd  = "H8JKjfhyur&*HGrjy";

 

 

 

//List of tables you want to dump separated by single space

$tbl = "getaways_leftmenu getaways_seourl getaways_cmspages";

 

//execute command

exec("mysqldump -h $dbhost -u $dbuser -p\"$dbpwd\" $dbname $tbl > sqlfilename.sql");

 

 

//stores location of current directory

$dir = dirname(realpath(__FILE__));

 

 

 

//command to compress previously created sql file, also gzip -9 states to remove original sql file 

$zip = "gzip -9 $dir/sqlfilename.sql";

 

//execute command

exec($zip);

 

?>
   

 

thats it and you have zipped version of your dump file and the original sql file is automatically removed

 

 

No comments:

Post a Comment