duanmu2015 2015-10-10 21:46
浏览 121
已采纳

PHP:mysqldump:找不到表:“>”

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 
";
            }
        }
    }

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:

  • My Desktop System Shell: Ubuntu 15.04
  • Mysql Server: Mysql 5.6 installed in Ubuntu Server 15.04
  • PHP version: 5.6.4

Any help ?

Thank you

  • 写回答

1条回答 默认 最新

  • duan19740319 2015-10-10 22:18
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?