简体   繁体   English

如何判断mysqldump完成的时间

[英]How to tell when mysqldump is complete

Each night I do a backup of my database using the following command. 每天晚上,我都会使用以下命令对数据库进行备份。 I ftp this backup file offsite every night, but I have no idea when it is safe to begin the ftp session since I don't know how long the dump takes. 我每天晚上都在异地通过ftp备份该备份文件,但是我不知道何时可以安全地开始ftp会话,因为我不知道转储需要多长时间。

Is there any way with php to tell when the mysqldump is done - for example testing whether $filename is still in use or querying mysql for dumps in progress? php有什么办法告诉mysqldump何时完成-例如测试$filename是否仍在使用中或向mysql查询正在进行的转储?

Thanks in advance. 提前致谢。

$command = "mysqldump -h " . DB_SERVER . " -u ". DB_USER ." -p". DB_PASS ." --add-drop-table --quick --set-gtid-purged=OFF " . DB_NAME . " > ". $filename; 

$result = passthru($command);

How about creating a backup log file? 如何创建备份日志文件?

$command = "mysqldump -h " . DB_SERVER . " -u ". DB_USER ." -p". DB_PASS ." --add-drop-table --quick --set-gtid-purged=OFF " . DB_NAME . " > ". $filename; 
$result = passthru($command);

$file = fopen('backup-file-name', 'a'); // can specify full path
fwrite($file, date('M-d-Y h:i:s') . " -- backup finished \n");
fclose($file);

Or something like this: 或类似这样的东西:

// open file and say it's in progress
$file = fopen('backup-file-name', 'w'); // can specify full path
fwrite($file, date('M-d-Y h:i:s') . " -- backup in progress \n");
fclose($file);

$command = "mysqldump -h " . DB_SERVER . " -u ". DB_USER ." -p". DB_PASS ." --add-drop-table --quick --set-gtid-purged=OFF " . DB_NAME . " > ". $filename; 
$result = passthru($command);

// open file and say that backup is done
$file = fopen('backup-file-name', 'w'); // can specify full path
fwrite($file, date('M-d-Y h:i:s') . " -- backup finished \n");
fclose($file);

EDIT: 编辑:

The PHP code executed after passthru() only triggered after passthru() is finished. 后执行的PHP代码passthru()后,才触发passthru()完成。

Another useful info is that passthru() can take a second argument return_var that returns the status of the command. 另一个有用的信息是passthru()可以使用第二个参数return_var ,该参数返回命令的状态。

Couple notes: 夫妻注意事项:

  • 0 means the status returned successfully for Unix commands. 0表示成功返回Unix命令的状态。

  • The second argument for passthru() returns value by reference, so please pass a declared variable to the function. passthru()第二个参数通过引用返回值,因此请将已声明的变量传递给函数。

Example: 例:

$command_status = 'N/A';
$result = passthru($command, $command_status);
// then check if $command_status value === 0
// maybe log the status of the command as well

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

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