简体   繁体   English

我想用mysqldump备份表的最后记录

[英]I want to backup last records of a table with mysqldump

So far I have this: 到目前为止,我有这个:

exec('mysqldump --user=$DBUSER --password=$DBPASSWD --host= localhost DB_NAME (select column from database.table where= column > 1234) > table.sql');

Of course nothing happens when I open the file on my server using a browser. 当然,当我使用浏览器在服务器上打开文件时,什么也不会发生。 Now, the next code does output a SQL file but an empty one: 现在,下一个代码确实输出了一个SQL文件,但输出了一个空文件:

set_time_limit(600);
system("mysqldump -h localhost -u $DBUSER -p $DBPASSWD $DATABASE  > table.sql");

I'm after records generated every day so I can later do an incremental backup. 我需要每天生成的记录,因此以后可以进行增量备份。

Why is it that system() does output a file and exec() doesn't? 为什么system()不输出文件而exec()不输出文件?

If your objective is to do incremental backups of your database mysqldump is not ideal option for it. 如果您的目标是对数据库进行增量备份,那么mysqldump并不是理想的选择。

There are two things you might do here: 您可以在这里做两件事:

  1. [BAD WAY] Use your PHP code to serialize and dump the new records in CSV and JSON format and use it. [BAD WAY]使用您的PHP代码以CSV和JSON格式序列化并转储新记录,然后使用它。

  2. You may want to use binlog in MySQL which will help you do it ( follow the link to read the MySQL backup methods ) 您可能希望在MySQL中使用binlog可以帮助您做到这一点( 单击链接以阅读MySQL备份方法

Dumping selective records: 倾销选择性记录:

$recset = mysql_query("select column from database.table where= column > 1234");
for($recset as $row){
    file_put_contents("filename.json", json_encode($row) . "\n")
}

Now, once neeed you may read this file line by line and use json_enocode to get you data back in php array/object and do as you wish with it. 现在,一旦需要,您可以逐行读取此文件,并使用json_enocode将数据恢复为php数组/对象,并随心所欲地对其进行处理。

[Edit: After looking at your pastebin code] [编辑:查看完您的pastebin代码后]

After fixing a few variable name and code to work as per your need. 固定一些变量名和代码后,即可根据需要工作。

$docsql='nts.sql';
require("cnxn.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$q1=mysqli_query($cnx,"SELECT * FROM table WHERE col > '45678' ORDER BY col ASC");
$data_records=array();
while($r1=mysqli_fetch_assoc($q1)){
    $cntn=htmlentities($r1['cntn']);
    array_push($data_records, "(".$r1['col1'].",'".$r1['col2']."','".$r1['col3']."','".$r1['col4']."','".$r1['col5']."')");
}

$insert="INSERT INTO notas (col1, col2, col3, col4, col5) VALUES ";
file_put_contents($docsql, $insert);
foreach($data_records as $record){
    file_put_contents($docsql, $record) . "\n", FILE_APPEND);
}

To the solution that akm suggested, I just added a final touch to replace the last coma with a semicolon: 在akm建议的解决方案中,我添加了最后一个修饰符,以分号代替最后的昏迷:

$docsql='nts.sql';
require("cnxn.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$q1=mysqli_query($cnx,"SELECT * FROM table WHERE col > '45678' ORDER BY col  ASC");
$data_records=array();
while($r1=mysqli_fetch_assoc($q1)){
$cntn=htmlentities($r1['cntn']);
array_push($data_records, "(".$r1['col1'].",'".$r1['col2']."','".$r1['col3']."','".$r1['col4']."','".$r1['c ol5']."')");
}

$insert="INSERT INTO notas (col1, col2, col3, col4, col5) VALUES ";
file_put_contents($docsql, $insert);
foreach($data_records as $record){
    file_put_contents($docsql, $record) . "\n", FILE_APPEND);
}

$chng = fopen("nts.sql", "a+") or die("cant open file");
$rmv = fstat($chng);
ftruncate($chng, $rmv['size']-2);
fwrite($chng,';');
fclose($chng);

That did the job just fine. 那做得很好。 Thank's akm you Rock... 谢谢akm you Rock ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM