我一直在尝试通过单击链接来实现数据库备份功能。我正在做的是将我的函数写入AppController
& 函数是......
public function backup($tables = '*') {
$this->layout = $this->autoLayout = $this->autoRender = false;
if ($tables == '*') {
$tables = array();
$result = $this->query('SHOW TABLES');
while ($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
} else {
$tables = is_array($tables) ? $tables : explode(',', $tables);
}
foreach ($tables as $table) {
$result = $this->query('SELECT * FROM ' . $table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE ' . $table . ';';
$row2 = mysql_fetch_row($this->query('SHOW CREATE TABLE ' . $table));
$return.= "\n\n" . $row2[1] . ";\n\n";
for ($i = 0; $i < $num_fields; $i++) {
while ($row = mysql_fetch_row($result)) {
$return.= 'INSERT INTO ' . $table . ' VALUES(';
for ($j = 0; $j < $num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n", "\\n", $row[$j]);
if (isset($row[$j])) {
$return.= '"' . $row[$j] . '"';
} else {
$return.= '""';
}
if ($j < ($num_fields - 1)) {
$return.= ',';
}
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
$handle = fopen('db-backup-' . time() . '-' . (md5(implode(',', $tables))) . '.sql', 'w+');
fwrite($handle, $return);
fclose($handle);
}
从视图中,我将其称为链接,单击链接时它会在所需文件夹中创建我的文件...
<li><?php echo $this->Html->link("Backup", "/app/backup/", array('class' => 'Backup tooltip')); ?></li>
它以致命的方式结束了我。请帮忙。
有修改:
public function admin_backup() {
$this->layout = $this->autoLayout = $this->autoRender = false;
$fileName = 'backUp_' . date("d-M-Y_h:i:s_") . time();
if (exec('mysqldump --user=root --password= --host=localhost demosite > ' . UPLOAD_FULL_BACKUP_PATH . $fileName . '.sql')) {
echo "Success";
} else {
echo "Failed";
}
}
我的新功能适用于 Ubuntu,但不适用于 Windows。请帮忙。