gpt4 book ai didi

php - 如何判断 mysqldump 何时完成

转载 作者:行者123 更新时间:2023-11-29 03:24:44 25 4
gpt4 key购买 nike

每天晚上我都会使用以下命令备份我的数据库。我每晚都将此备份文件 ftp 到异地,但我不知道什么时候开始 ftp session 是安全的,因为我不知道转储需要多长时间。

php 是否有任何方法可以判断 mysqldump 何时完成 - 例如测试 $filename 是否仍在使用或查询用于转储的 mysql 正在进行中?

提前致谢。

$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);

最佳答案

如何创建备份日志文件?

$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);

或者像这样:

// 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);

编辑:

passthru() 之后执行的 PHP 代码仅在 passthru() 完成后触发。

另一个有用的信息是 passthru() 可以采用第二个参数 return_var 返回命令的状态。

注意事项:

  • 0表示Unix命令返回成功。

  • second argument for passthru() 通过引用返回值,因此请将声明的变量传递给函数。

例子:

$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

关于php - 如何判断 mysqldump 何时完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38449041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com