恢复mysql数据库备份?

发布于 2025-01-02 18:51:06 字数 1364 浏览 1 评论 0原文

我正在使用这个命令来备份 MySql 数据库:

public function backup() {
    $backup = $this->location.'/'.$this->database.'_backup_'.date('Y').'_'.date('m').'_'.date('d').'.sql';
    $cmd = "c:/xampp/mysql/bin/mysqldump --opt -h localhost -u root $this->database > $backup";
    try {
        system($cmd);
        $error              = false;
        $message['error']   = false;
        $message['message'] = 'Backup successfuly complete';
        return json_encode($message);
    } catch(PDOException $e) {

        $error              = true;
        $message['error']   = true;
        $message['message'] = $e->getMessage();;
        return json_encode($message);
    } 
}

上面的工作正常,数据库备份没有任何问题。这是恢复备份的命令:

public function restore($backup) {
    $cmd = "c:/xampp/mysql/bin/mysql -h localhost -u root $this->database > $backup";
    try {
        exec($cmd);
        $error              = false;
        $message['error']   = false;
        $message['message'] = 'Restore successfuly complete';
        return json_encode($message);
    } catch(PDOException $e) {

        $error              = true;
        $message['error']   = true;
        $message['message'] = $e->getMessage();;
        return json_encode($message);
    } 
}

上述函数的问题是,当我执行它时,数据库没有恢复,而是备份数据库表的.sql 文件被清空了。怎么了 ?

I'm using this command to backup a MySql database:

public function backup() {
    $backup = $this->location.'/'.$this->database.'_backup_'.date('Y').'_'.date('m').'_'.date('d').'.sql';
    $cmd = "c:/xampp/mysql/bin/mysqldump --opt -h localhost -u root $this->database > $backup";
    try {
        system($cmd);
        $error              = false;
        $message['error']   = false;
        $message['message'] = 'Backup successfuly complete';
        return json_encode($message);
    } catch(PDOException $e) {

        $error              = true;
        $message['error']   = true;
        $message['message'] = $e->getMessage();;
        return json_encode($message);
    } 
}

This above works fine, the database is backed up without any problems. And this is the command to restore the back up:

public function restore($backup) {
    $cmd = "c:/xampp/mysql/bin/mysql -h localhost -u root $this->database > $backup";
    try {
        exec($cmd);
        $error              = false;
        $message['error']   = false;
        $message['message'] = 'Restore successfuly complete';
        return json_encode($message);
    } catch(PDOException $e) {

        $error              = true;
        $message['error']   = true;
        $message['message'] = $e->getMessage();;
        return json_encode($message);
    } 
}

The problem with the above function is that when I execute it, the database is not restored, instead the .sql file, in which the database tables are backed up, it's emptied. What is happening ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2025-01-09 18:51:06

将大于号切换为小于号。现在,在恢复作业中,数据库再次写入文件;现在它已被清空,它正在清除文件。

mysql -h localhost -u root $this->database < $backup.sql
                                           ^

一般情况下,>表示写入,<表示读取。

Switch the greater than sign to a less than. Right now in your restore job you have the database writing again to a file; now that it's been emptied, it's clearing the file.

mysql -h localhost -u root $this->database < $backup.sql
                                           ^

Generally, > means write, < means read.

星光不落少年眉 2025-01-09 18:51:06

您必须更改恢复命令行,将 > 替换为 <

$cmd = "c:/xampp/mysql/bin/mysql -h localhost -u root $this->database < $backup";

You have to change the restore command line replacing > with < :

$cmd = "c:/xampp/mysql/bin/mysql -h localhost -u root $this->database < $backup";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文