2

I have been trying implementing a DB Backup function by simply clicking on link. What I am doing is writing my function to the AppController & function is...

    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);
    }

From view I am calling it as link that on clicking link it create my file in desired folder...

 <li><?php echo $this->Html->link("Backup", "/app/backup/", array('class' => 'Backup tooltip')); ?></li>

Its ending me with fatal. Please help.

With Modification:

    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";
        }
    }

My new function its working on Ubuntu but not on Windows. Please help.

Sankalp
  • 1,300
  • 5
  • 28
  • 52
  • $result = $this->query('SHOW TABLES'); In the AppController '$this' is not referring to a model and cannot be directly querried – Jeroen Jul 26 '13 at 08:36

2 Answers2

6

Using exec() and mysqldump might be easy in some cases, but it won't work in the following cases:

  • You're on shared hosting
  • Exec is disabled
  • mysqldump is not installed

If any of these are true, then you need the following function. This function handles NULL values, and UTF-8 data.

Usage:

Put this function in any controller, then navigate to:

http://yoursite.com/admin/yourcontroller/database_mysql_dump

This will download a valid MySQL dump to the browser as a .sql file.

/**
 * 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);
}

Thanks to Sankalp for the starting point for this code.

I hope this helps someone.

Ben Hitchcock
  • 1,368
  • 10
  • 12
  • Thanks, I will try the same. But I need to change in case I want to save it to the same server, I mean to particular folder where project runs. Can you please help?? – Sankalp Feb 28 '14 at 13:09
  • In that case, you need to put something like this: file_put_contents ( ROOT . $filename , $return ); at the end of this function. Note that you will need write access to the file/folder you are writing to. – Ben Hitchcock Mar 04 '14 at 00:57
  • Does it works fine for you ? On my side nothing happens. A var_dump() of $datasource shows nothing on my side ... – zeflex Sep 20 '14 at 00:33
  • It does work fine, although I've since massaged the function to handle larger dumps. – Ben Hitchcock Sep 22 '14 at 06:19
  • 1
    I can't imagine a grosser way to phrase that. – Phantom Watson Mar 27 '15 at 21:03
5

The method doesn't exist

From the question:

writing my function to the AppController

$result = $this->query('SHOW TABLES');

query is a model method - it doesn't exist on Controller objects. To get the code in the question to work, call query on a model object (any model), or preferably move the whole code into a specific model method and call it.

The code is in the wrong class

The App controller is effectively an abstract class, it's not supposed to be instanciated or web-accessible at all. By putting the code in a public function in the App controller, it is accessible from any controller i.e.:

/posts/backup
/comments/backup
/users/backup

That's not a good idea/design.

If there is a function which belongs to no particular controller/model; create a model to put the model logic in (if there is some) and create a controller to put that action in - e.g. (given what the question is asking about) a DbController and a Db model.

Use mysqldump

Why not just use mysqldump?

exec('mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql');

Generating a dump file with php, is at best (a lot) slower, and at worst produces an invalid sql file/fails to complete (especially relevant with a sizable db).

Community
  • 1
  • 1
AD7six
  • 63,116
  • 12
  • 91
  • 123
  • Totally agree, imagine your function run on 2Gb database or more. It will take ages. Use @AD7six proposal or write a shell script which you can call it or even make a cron task which do this regularly - clickling a button is always a bad idea, people forget to do it. – Nik Chankov Jul 27 '13 at 01:30
  • Are you asking _if_ it works on windows? [sure](http://stackoverflow.com/questions/14139743/windows-batch-file-for-mysqldump-command) - if it's installed :). – AD7six Jul 31 '13 at 12:43
  • @AD7six : What need to be installed ?? my function works like this 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"; } } It fails on windows. How can I return reason of error. Can I try it with `try-catch`? Pls help.. – Sankalp Aug 04 '13 at 11:42