恢复mysql数据库备份?
我正在使用这个命令来备份 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将大于号切换为小于号。现在,在恢复作业中,数据库再次写入文件;现在它已被清空,它正在清除文件。
一般情况下,
>
表示写入,<
表示读取。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.
Generally,
>
means write,<
means read.您必须更改恢复命令行,将
>
替换为<
:You have to change the restore command line replacing
>
with<
: