Esempio n. 1
0
 /**
  * Constructor of Mysqldump. Note that in the case of an SQLite database
  * connection, the filename must be in the $db parameter.
  *
  * @param string $db         Database name
  * @param string $user       SQL account username
  * @param string $pass       SQL account password
  * @param string $host       SQL server to connect to
  * @param string $type       SQL database type
  * @param array  $dumpSettings SQL database settings
  * @param array  $pdoSettings  PDO configured attributes
  */
 public function __construct($db = '', $user = '', $pass = '', $host = 'localhost', $type = 'mysql', $dumpSettings = array(), $pdoSettings = array())
 {
     $dumpSettingsDefault = array('include-tables' => array(), 'exclude-tables' => array(), 'compress' => Mysqldump::NONE, 'no-data' => false, 'add-drop-table' => false, 'single-transaction' => true, 'lock-tables' => true, 'add-locks' => true, 'extended-insert' => true, 'disable-keys' => true, 'where' => '', 'no-create-info' => false, 'skip-triggers' => false, 'add-drop-trigger' => true, 'hex-blob' => true, 'databases' => false, 'add-drop-database' => false, 'skip-tz-utz' => false, 'no-autocommit' => true, 'default-character-set' => Mysqldump::UTF8, 'skip-comments' => false, 'skip-dump-date' => false, 'disable-foreign-keys-check' => true);
     $pdoSettingsDefault = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false);
     $this->db = $db;
     $this->user = $user;
     $this->pass = $pass;
     $colonPos = strpos($host, ':');
     if (false !== $colonPos) {
         $this->port = substr($host, $colonPos + 1);
         $this->host = substr($host, 0, $colonPos);
     } else {
         $this->port = null;
         $this->host = $host;
     }
     $this->dbType = strtolower($type);
     $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings);
     $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings);
     if (!isset($this->pdoSettings[PDO::MYSQL_ATTR_INIT_COMMAND])) {
         $this->pdoSettings[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES " . $this->dumpSettings['default-character-set'];
     }
     $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault));
     if (count($diff) > 0) {
         throw new Exception("Unexpected value in dumpSettings: (" . implode(",", $diff) . ")");
     }
     // Create a new compressManager to manage compressed output
     $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']);
 }
Esempio n. 2
0
 /**
  * Constructor of Mysqldump. Note that in the case of an SQLite database
  * connection, the filename must be in the $db parameter.
  *
  * @param string $dsn        PDO DSN connection string
  * @param string $user       SQL account username
  * @param string $pass       SQL account password
  * @param array  $dumpSettings SQL database settings
  * @param array  $pdoSettings  PDO configured attributes
  */
 public function __construct($dsn = '', $user = '', $pass = '', $dumpSettings = array(), $pdoSettings = array())
 {
     $dumpSettingsDefault = array('include-tables' => array(), 'exclude-tables' => array(), 'compress' => Mysqldump::NONE, 'no-data' => false, 'add-drop-table' => false, 'single-transaction' => true, 'lock-tables' => true, 'add-locks' => true, 'extended-insert' => true, 'complete-insert' => false, 'disable-keys' => true, 'where' => '', 'no-create-info' => false, 'skip-triggers' => false, 'add-drop-trigger' => true, 'routines' => false, 'hex-blob' => true, 'databases' => false, 'add-drop-database' => false, 'skip-tz-utc' => false, 'no-autocommit' => true, 'default-character-set' => Mysqldump::UTF8, 'skip-comments' => false, 'skip-dump-date' => false, 'init_commands' => array(), 'disable-foreign-keys-check' => true);
     $pdoSettingsDefault = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false);
     $this->user = $user;
     $this->pass = $pass;
     $this->parseDsn($dsn);
     $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings);
     $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings);
     $this->dumpSettings['init_commands'][] = "SET NAMES " . $this->dumpSettings['default-character-set'];
     if (false === $this->dumpSettings['skip-tz-utc']) {
         $this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'";
     }
     $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault));
     if (count($diff) > 0) {
         throw new Exception("Unexpected value in dumpSettings: (" . implode(",", $diff) . ")");
     }
     if (!is_array($this->dumpSettings['include-tables']) || !is_array($this->dumpSettings['exclude-tables'])) {
         throw new Exception("Include-tables and exclude-tables should be arrays");
     }
     // Create a new compressManager to manage compressed output
     $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']);
 }
Esempio n. 3
0
 /**
  * Main call
  *
  * @param string $filename  Name of file to write sql dump to
  * @return null
  */
 public function start($filename = '')
 {
     // Output file can be redefined here
     if (!empty($filename)) {
         $this->fileName = $filename;
     }
     // We must set a name to continue
     if (empty($this->fileName)) {
         throw new Exception("Output file name is not set");
     }
     // Connect to database
     $this->connect();
     // Create a new compressManager to manage compressed output
     $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']);
     $this->compressManager->open($this->fileName);
     // Formating dump file
     $this->compressManager->write($this->getHeader());
     if ($this->dumpSettings['add-drop-database']) {
         $this->compressManager->write($this->typeAdapter->add_drop_database($this->db));
     }
     // Listing all tables from database
     $this->tables = array();
     if (empty($this->dumpSettings['include-tables'])) {
         // include all tables for now, blacklisting happens later
         foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->db)) as $row) {
             array_push($this->tables, current($row));
         }
     } else {
         // include only the tables mentioned in include-tables
         foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->db)) as $row) {
             if (in_array(current($row), $this->dumpSettings['include-tables'], true)) {
                 array_push($this->tables, current($row));
                 $elem = array_search(current($row), $this->dumpSettings['include-tables']);
                 unset($this->dumpSettings['include-tables'][$elem]);
             }
         }
     }
     // If there still are some tables in include-tables array, that means
     // that some tables weren't found. Give proper error and exit.
     if (0 < count($this->dumpSettings['include-tables'])) {
         $table = implode(",", $this->dumpSettings['include-tables']);
         throw new Exception("Table (" . $table . ") not found in database");
     }
     // Disable checking foreign keys
     if ($this->dumpSettings['disable-foreign-keys-check']) {
         $this->compressManager->write($this->typeAdapter->start_disable_foreign_keys_check());
     }
     // Exporting tables one by one
     foreach ($this->tables as $table) {
         if (in_array($table, $this->dumpSettings['exclude-tables'], true)) {
             continue;
         }
         $isTable = $this->getTableStructure($table);
         if (true === $isTable && false === $this->dumpSettings['no-data']) {
             $this->listValues($table);
         }
     }
     // Exporting views one by one
     foreach ($this->views as $view) {
         $this->compressManager->write($view);
     }
     // Enable checking foreign keys if needed
     if ($this->dumpSettings['disable-foreign-keys-check']) {
         $this->compressManager->write($this->typeAdapter->end_disable_foreign_keys_check());
     }
     $this->compressManager->close();
 }