简体   繁体   中英

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:

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?

If your objective is to do incremental backups of your database mysqldump is not ideal option for it.

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.

  2. You may want to use binlog in MySQL which will help you do it ( follow the link to read the MySQL backup methods )

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.

[Edit: After looking at your pastebin code]

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:

$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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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