cakephp中的DB备份功能

cakephp中的DB备份功能,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我一直在尝试通过点击链接来实现DB备份功能。我正在做的是将函数写入AppController&函数是 public function backup($tables = '*') { $this->layout = $this->autoLayout = $this->autoRender = false; if ($tables == '*') { $tables = array(); $r

我一直在尝试通过点击链接来实现DB备份功能。我正在做的是将函数写入
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);
    }
我的新功能在Ubuntu上工作,但在Windows上不工作。请帮助。

该方法不存在 从问题中:

将我的函数写入AppController

是一种模型方法-它不存在于控制器对象上。要使问题中的代码正常工作,可以对模型对象(任何模型)调用query,或者最好将整个代码移动到特定的模型方法中并调用它

代码在错误的类中 appcontroller实际上是一个抽象类,它根本不应该是实例化的或web可访问的。通过将代码放入App controller的公共功能中,可以从任何控制器访问该代码,即:

/posts/backup
/comments/backup
/users/backup
那不是一个好主意/设计

如果存在不属于特定控制器/模型的功能;创建一个模型以放入模型逻辑(如果有),并创建一个控制器以放入该操作-例如(给定问题的内容)一个
DbController
和一个
Db
模型

使用mysqldump 为什么不呢

使用php生成转储文件,充其量要慢(很多),最坏的情况是生成无效的sql文件/无法完成(特别是与较大的数据库相关)。

在某些情况下,使用exec()和mysqldump可能很容易,但在以下情况下不起作用:

  • 你在共享主机上
  • Exec已禁用
  • 未安装mysqldump
如果其中任何一个为真,则需要以下函数。 此函数处理空值和UTF-8数据

用法: 将此功能置于任何控制器中,然后导航到:

这将把有效的MySQL转储文件作为.sql文件下载到浏览器中

/**
 * Dumps the MySQL database that this controller's model is attached to.
 * This action will serve the sql file as a download so that the user can save the backup to their local computer.
 *
 * @param string $tables Comma separated list of tables you want to download, or '*' if you want to download them all.
 */
function admin_database_mysql_dump($tables = '*') {

    $return = '';

    $modelName = $this->modelClass;

    $dataSource = $this->{$modelName}->getDataSource();
    $databaseName = $dataSource->getSchemaName();


    // Do a short header
    $return .= '-- Database: `' . $databaseName . '`' . "\n";
    $return .= '-- Generation time: ' . date('D jS M Y H:i:s') . "\n\n\n";


    if ($tables == '*') {
        $tables = array();
        $result = $this->{$modelName}->query('SHOW TABLES');
        foreach($result as $resultKey => $resultValue){
            $tables[] = current($resultValue['TABLE_NAMES']);
        }
    } else {
        $tables = is_array($tables) ? $tables : explode(',', $tables);
    }

    // Run through all the tables
    foreach ($tables as $table) {
        $tableData = $this->{$modelName}->query('SELECT * FROM ' . $table);

        $return .= 'DROP TABLE IF EXISTS ' . $table . ';';
        $createTableResult = $this->{$modelName}->query('SHOW CREATE TABLE ' . $table);
        $createTableEntry = current(current($createTableResult));
        $return .= "\n\n" . $createTableEntry['Create Table'] . ";\n\n";

        // Output the table data
        foreach($tableData as $tableDataIndex => $tableDataDetails) {

            $return .= 'INSERT INTO ' . $table . ' VALUES(';

            foreach($tableDataDetails[$table] as $dataKey => $dataValue) {

                if(is_null($dataValue)){
                    $escapedDataValue = 'NULL';
                }
                else {
                    // Convert the encoding
                    $escapedDataValue = mb_convert_encoding( $dataValue, "UTF-8", "ISO-8859-1" );

                    // Escape any apostrophes using the datasource of the model.
                    $escapedDataValue = $this->{$modelName}->getDataSource()->value($escapedDataValue);
                }

                $tableDataDetails[$table][$dataKey] = $escapedDataValue;
            }
            $return .= implode(',', $tableDataDetails[$table]);

            $return .= ");\n";
        }

        $return .= "\n\n\n";
    }

    // Set the default file name
    $fileName = $databaseName . '-backup-' . date('Y-m-d_H-i-s') . '.sql';

    // Serve the file as a download
    $this->autoRender = false;
    $this->response->type('Content-Type: text/x-sql');
    $this->response->download($fileName);
    $this->response->body($return);
}
感谢Sankalp提供了这段代码的起点


我希望这对某人有所帮助。

$result=$this->query('showtables');在AppController中,“$this”不是指模型,不能直接查询。完全同意,假设您的函数运行在2Gb或更多数据库上。这需要很长时间。使用@AD7six proposal或编写一个shell脚本,您可以调用它,甚至可以编写一个cron任务来定期执行此操作-单击按钮总是一个坏主意,人们会忘记执行此操作。您是否询问它是否在windows上工作?-如果安装了:)@AD7six:需要安装什么??我的函数的工作方式类似于这个公共函数admin_backup(){$this->layout=$this->autoLayout=$this->autoRender=false;$fileName='backup'.date(“d-M-Y\u h:i:s_”).time();if(exec('mysqldump--user=root--password=--host=localhost demosite>)。UPLOAD\u FULL\u backup\u路径。$fileName..sql')){echo“成功”;}其他{echo“失败”;}它在windows上失败。我如何返回错误原因。我可以用
try catch
尝试它吗?请帮助。谢谢,我会尝试相同的。但我需要更改,以防我想将它保存到同一服务器,我指的是项目运行的特定文件夹。你能帮助吗?在这种情况下,你需要放置类似这样的内容:file\u put\u contents(ROOT.$filename,$return);此函数结束时。请注意,您需要对正在写入的文件/文件夹具有写访问权限。它对您正常工作吗?在我这方面,什么都不会发生。var_dump()$datasource在我这方面什么都没有显示…它确实工作得很好,尽管我已经修改了函数来处理更大的转储。我无法想象有更糟糕的方式来表达它。
$result = $this->query('SHOW TABLES');
/posts/backup
/comments/backup
/users/backup
exec('mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql');
/**
 * Dumps the MySQL database that this controller's model is attached to.
 * This action will serve the sql file as a download so that the user can save the backup to their local computer.
 *
 * @param string $tables Comma separated list of tables you want to download, or '*' if you want to download them all.
 */
function admin_database_mysql_dump($tables = '*') {

    $return = '';

    $modelName = $this->modelClass;

    $dataSource = $this->{$modelName}->getDataSource();
    $databaseName = $dataSource->getSchemaName();


    // Do a short header
    $return .= '-- Database: `' . $databaseName . '`' . "\n";
    $return .= '-- Generation time: ' . date('D jS M Y H:i:s') . "\n\n\n";


    if ($tables == '*') {
        $tables = array();
        $result = $this->{$modelName}->query('SHOW TABLES');
        foreach($result as $resultKey => $resultValue){
            $tables[] = current($resultValue['TABLE_NAMES']);
        }
    } else {
        $tables = is_array($tables) ? $tables : explode(',', $tables);
    }

    // Run through all the tables
    foreach ($tables as $table) {
        $tableData = $this->{$modelName}->query('SELECT * FROM ' . $table);

        $return .= 'DROP TABLE IF EXISTS ' . $table . ';';
        $createTableResult = $this->{$modelName}->query('SHOW CREATE TABLE ' . $table);
        $createTableEntry = current(current($createTableResult));
        $return .= "\n\n" . $createTableEntry['Create Table'] . ";\n\n";

        // Output the table data
        foreach($tableData as $tableDataIndex => $tableDataDetails) {

            $return .= 'INSERT INTO ' . $table . ' VALUES(';

            foreach($tableDataDetails[$table] as $dataKey => $dataValue) {

                if(is_null($dataValue)){
                    $escapedDataValue = 'NULL';
                }
                else {
                    // Convert the encoding
                    $escapedDataValue = mb_convert_encoding( $dataValue, "UTF-8", "ISO-8859-1" );

                    // Escape any apostrophes using the datasource of the model.
                    $escapedDataValue = $this->{$modelName}->getDataSource()->value($escapedDataValue);
                }

                $tableDataDetails[$table][$dataKey] = $escapedDataValue;
            }
            $return .= implode(',', $tableDataDetails[$table]);

            $return .= ");\n";
        }

        $return .= "\n\n\n";
    }

    // Set the default file name
    $fileName = $databaseName . '-backup-' . date('Y-m-d_H-i-s') . '.sql';

    // Serve the file as a download
    $this->autoRender = false;
    $this->response->type('Content-Type: text/x-sql');
    $this->response->download($fileName);
    $this->response->body($return);
}