यहाँ एक PHP
स्क्रिप्ट मैं जो होगा बैकअप अपने डेटाबेस में सभी तालिकाओं बनाया है। यह कुछ सुधारों के साथ इस http://davidwalsh.name/backup-mysql-database-php पर आधारित है। सबसे पहले यह foreign key restrictions
सही ढंग से स्थापित करेगा।
मेरे सेट अप में स्क्रिप्ट सप्ताह के एक निश्चित दिन पर चलेगी, चलो सोमवार को कहें। यदि यह सोमवार को नहीं चलता है, तो यह अभी भी मंगलवार को (उदाहरण के लिए) चलाएगा, .sql
फ़ाइल को पिछले सोमवार की तारीख के साथ, जब इसे चलाने के लिए चलाया गया था। यह 4 सप्ताह पहले .sql
फ़ाइल मिटा देगा, इसलिए यह हमेशा पिछले 4 बैकअप रखता है।
<?php
backup_tables();
// backup all tables in db
function backup_tables()
{
$day_of_backup = 'Monday'; //possible values: `Monday` `Tuesday` `Wednesday` `Thursday` `Friday` `Saturday` `Sunday`
$backup_path = 'databases/'; //make sure it ends with "/"
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'movies_database_1';
//set the correct date for filename
if (date('l') == $day_of_backup) {
$date = date("Y-m-d");
} else {
//set $date to the date when last backup had to occur
$datetime1 = date_create($day_of_backup);
$date = date("Y-m-d", strtotime($day_of_backup.' -7 days'));
}
if (!file_exists($backup_path.$date.'-backup'.'.sql')) {
//connect to db
$link = mysqli_connect($db_host,$db_user,$db_pass);
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,$db_name);
//get all of the tables
$tables = array();
$result = mysqli_query($link, 'SHOW TABLES');
while($row = mysqli_fetch_row($result))
{
$tables[] = $row[0];
}
//disable foreign keys (to avoid errors)
$return = 'SET FOREIGN_KEY_CHECKS=0;' . "\r\n";
$return.= 'SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";' . "\r\n";
$return.= 'SET AUTOCOMMIT=0;' . "\r\n";
$return.= 'START TRANSACTION;' . "\r\n";
//cycle through
foreach($tables as $table)
{
$result = mysqli_query($link, 'SELECT * FROM '.$table);
$num_fields = mysqli_num_fields($result);
$num_rows = mysqli_num_rows($result);
$i_row = 0;
//$return.= 'DROP TABLE '.$table.';';
$row2 = mysqli_fetch_row(mysqli_query($link,'SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
if ($num_rows !== 0) {
$row3 = mysqli_fetch_fields($result);
$return.= 'INSERT INTO '.$table.'(';
foreach ($row3 as $th)
{
$return.= '`'.$th->name.'`, ';
}
$return = substr($return, 0, -2);
$return.= ') VALUES';
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysqli_fetch_row($result))
{
$return.="\n(";
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("#\n#","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
if (++$i_row == $num_rows) {
$return.= ");"; // last row
} else {
$return.= "),"; // not last row
}
}
}
}
$return.="\n\n\n";
}
// enable foreign keys
$return .= 'SET FOREIGN_KEY_CHECKS=1;' . "\r\n";
$return.= 'COMMIT;';
//set file path
if (!is_dir($backup_path)) {
mkdir($backup_path, 0755, true);
}
//delete old file
$old_date = date("Y-m-d", strtotime('-4 weeks', strtotime($date)));
$old_file = $backup_path.$old_date.'-backup'.'.sql';
if (file_exists($old_file)) unlink($old_file);
//save file
$handle = fopen($backup_path.$date.'-backup'.'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}
}
?>
स्रोत
2015-08-20 12:00:31
[निर्यात MySQL PHP का उपयोग डेटाबेस केवल] के संभावित डुप्लिकेट (http://stackoverflow.com/questions/22195493/export-mysql-database-using-php-only) –