Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Back up database tables.
<?php
$dbhost = 'DB_Host'; // Database Host
$dbuser = 'DB_User'; // Database User
$dbpass = 'DBUser_Password'; // Database User Password
$db='DB_Name'; // Database Name
$i=0;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql= "SHOW TABLES from $db";
$result = mysql_query( $sql, $conn );
while ($row=mysql_fetch_array($result)) {
echo $table_name= $row[0];
echo "</br>";
$i++;
$backup_file = getcwd() .$table_name .'.sql';
$sql1 = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";
$retval = mysql_query( $sql1, $conn );
if(! $retval ) {
die('Could not take data backup: ' . mysql_error());
}
echo "Eport " . $table_name . " Successfully</br>";
}
echo "Number of tables: " . $i . "</br>";
mysql_close($conn);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment