CakePHP: Export / Download Database Code

/**
   * 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.
   */
  public function db_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->response->body($return);
      $this->response->type('Content-Type: text/x-sql');
      $this->response->download($fileName);

      return $this->response;
  }

Comments

Popular posts from this blog

CakePHP 4: Firebase Cloud Messaging Component in CakePHP for Push Notification

CakePHP 4 : Using Component inside Command ( Shell )

LIMIT SSH ACCESS BY COUNTRY