mseifert mseifert - 4 months ago 12
PHP Question

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.

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?

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

Answer

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.

Another useful info is that passthru() can take a second argument return_var that returns the status of the command.

Couple notes:

  • 0 means the status returned successfully for Unix commands.

  • The second argument for passthru() returns value by reference, so please pass a declared variable to the function.

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