skonsoft
skonsoft

Reputation: 1806

PHP: mysqldump: Couldn't find table: ">"

I tried to make simple php script to backup databes using mysqldump but i got this error:

mysqldump: Couldn't find table: ">"

And this is the function that i have write to make the job:

 protected function dump($params, $dbs, $backup_folder)
    {
        if (!is_writable($backup_folder)) {
            throw new Exception(sprintf('The backup folder is not writable: %s',
                                        $backup_folder));
        }

        $backup_folder = trim($backup_folder, "/"); // be sure that there is no / at the end

        $baseCmd = 'mysqldump --hex-blob --routines --skip-lock-tables -h%s -u%s -p%s %s > %s';
        foreach ($dbs as $db) {
            $dest = sprintf('%s/%s', $backup_folder,
                            $db . '_' . date('YmdHis') . '.sql');
            $cmd = sprintf($baseCmd, $params['host'], $params['user'],
                           $params['password'], $db, $dest);
            echo "Exec: $cmd: ";

            $last = system(escapeshellcmd($cmd), $res);

            echo $res;

            if ( 0 !== $res) {
                echo "Fail";
            } else {
                echo "Done \n";
            }
        }
    }

The command printed by this function give:

mysqldump --hex-blob --routines --skip-lock-tables -hdb-backup -uroot -pMyPwd my_db_name > backups/my_db_name_20151010232734.sql

When i execute this command from Shell it works very well but not from Php script.

My config is:

Any help ?

Thank you

Upvotes: 0

Views: 3222

Answers (2)

user26826180
user26826180

Reputation: 1

Use this as the quotation marks will help your query run as one:

mysqldump -u user -ppassword_here db_name
b -r "path_to_backup\file_name.sql"

Upvotes: 0

jeromegamez
jeromegamez

Reputation: 3551

The escapeshellcmd() command prepends a \ to the >.

From the PHP documentation:

Following characters are preceded by a backslash: #&;`|*?~<>^()[]{}$\, \x0A and \xFF.

You should either escape each argument individually with escapeshellarg(), or ensure that your method arguments ($params, $db, and $backup_folder) are valid and don't contain one of those chars and just use

system($cmd, $res)

I hope this helps!

Upvotes: 2

Related Questions