简体   繁体   English

MySQL:mysqldump生成数据库备份

[英]MySQL: mysqldump to generate database backup

I have had written the following snippet that generates the mysql database dump and saves it on the server : 我已经编写了以下代码段,该代码段生成mysql数据库转储并将其保存在服务器上

public function save_db_backup()
{
    $DBUSER=$this->db->username;
    $DBPASSWD=$this->db->password;
    $DATABASE=$this->db->database;

    $filename = $DATABASE . "-" . date("Y-m-d_H-i-s") . ".sql.gz";

    $save_path = $_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/' . $filename;

    $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > " . $save_path;

    exec( $cmd );
}

It has been working fine on my other server . 我的另一台服务器上运行正常 But after moving my site to this new server, it has suddenly stopped working ie database backup file is not being saved at the path specified. 但是,将站点移到新服务器后,站点突然停止工作,即数据库备份文件未保存在指定的路径中。 Also, I have checked exec is enabled on the server plus the directory is readable and also writeable : 另外,我检查了服务器上是否启用exec 并且目录可读且可写

 is_readable($_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/') // true
 is_writable($_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/') // true

I have checked and the database credentials are alright. 我已经检查过了,数据库凭据还可以。 I have tried with the path to mysqldump and that didn't work either: 我已经尝试了mysqldump的路径,但是也没有用:

$cmd = "/usr/bin/mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > " . $save_path;

What problem could there possibly be? 可能有什么问题?

You should check what user launches the command, and then if the dir is readable and also writeable by THAT user. 您应该检查哪个用户启动了该命令,然后检查该目录是否可被该用户读取和写入。 If you are launching it from a PHP script, it's the probable cause. 如果从PHP脚本启动它,则可能是原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM