简体   繁体   中英

How to test for success of mysqldump in PHP using shell_exec function

I am calling mysqldump via PHP to generate a backup of my database.

In my script I want to test whether or not the command completed successfully.

So far all of the solutions I have found use either 'exec' or 'system' to run mysqldump.

Those functions are disabled on my host, however 'shell_exec' is available.

Using shell_exec how can I check if the mysqldump command completed successfully, or if there was an error?

The value that shell_exec() returns is what the executed command outputs.

So, you should be getting all of the output from mysqldump inside of the return.

Now, to check whether mysqldump failed or not, you will most likely want to do some search in the output string that correlates with the result.

Just a quick example. If you ran php -v in the console, you'd get something similar like this as a result:

PHP 5.6.10 (cli) (built: Jun 10 2015 15:59:59)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

This is what you'd get as a result of shell_exec() :

string(148) "PHP 5.6.10 (cli) (built: Jun 10 2015 15:59:59) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies " 

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