Backup and Restore MySQL database using php

<?php $conn = mysql_connect('hostname', 'username', 'password');
if(! $conn ) { die('Could not connect: ' . mysql_error()); }
$table_name = "shop";
$backup_file = "shop.sql";
//Backup of any table
$sql = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";
//To restore the backup
$sql = "LOAD DATA INFILE '$backup_file' INTO TABLE $table_name";
mysql_select_db('test');
$result = mysql_query( $sql, $conn );
if(! $result ) { die('Could not take data backup: ' . mysql_error()); }
echo "Data successfully backed up";
mysql_close($conn); ?>

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Pinterest