00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Mage 00022 * @package Mage_Backup 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 00028 /** 00029 * Database backup model 00030 * 00031 * @category Mage 00032 * @package Mage_Backup 00033 * @author Magento Core Team <core@magentocommerce.com> 00034 */ 00035 class Mage_Backup_Model_Db 00036 { 00037 00038 /** 00039 * Buffer length for multi rows 00040 * default 512 Kb 00041 * 00042 */ 00043 const BUFFER_LENGTH = 524288; 00044 00045 /** 00046 * Retrieve resource model 00047 * 00048 * @return Mage_Backup_Model_Mysql4_Db 00049 */ 00050 public function getResource() 00051 { 00052 return Mage::getResourceSingleton('backup/db'); 00053 } 00054 00055 public function getTables() 00056 { 00057 return $this->getResource()->getTables(); 00058 } 00059 00060 public function getTableCreateScript($tableName, $addDropIfExists=false) 00061 { 00062 return $this->getResource()->getTableCreateScript($tableName, $addDropIfExists); 00063 } 00064 00065 public function getTableDataDump($tableName) 00066 { 00067 return $this->getResource()->getTableDataDump($tableName); 00068 } 00069 00070 public function getHeader() 00071 { 00072 return $this->getResource()->getHeader(); 00073 } 00074 00075 public function getFooter() 00076 { 00077 return $this->getResource()->getFooter(); 00078 } 00079 00080 public function renderSql() 00081 { 00082 ini_set('max_execution_time', 0); 00083 $sql = $this->getHeader(); 00084 00085 $tables = $this->getTables(); 00086 foreach ($tables as $tableName) { 00087 $sql.= $this->getTableCreateScript($tableName, true); 00088 $sql.= $this->getTableDataDump($tableName); 00089 } 00090 00091 $sql.= $this->getFooter(); 00092 return $sql; 00093 } 00094 00095 /** 00096 * Create backup and stream write to adapter 00097 * 00098 * @param Mage_Backup_Model_Backup $backup 00099 * @return Mage_Backup_Model_Db 00100 */ 00101 public function createBackup(Mage_Backup_Model_Backup $backup) 00102 { 00103 $backup->open(true); 00104 00105 $this->getResource()->beginTransaction(); 00106 00107 $tables = $this->getResource()->getTables(); 00108 00109 $backup->write($this->getResource()->getHeader()); 00110 00111 foreach ($tables as $table) { 00112 $backup->write($this->getResource()->getTableHeader($table) . $this->getResource()->getTableDropSql($table) . "\n"); 00113 $backup->write($this->getResource()->getTableCreateSql($table, false) . "\n"); 00114 00115 $tableStatus = $this->getResource()->getTableStatus($table); 00116 00117 if ($tableStatus->getRows()) { 00118 $backup->write($this->getResource()->getTableDataBeforeSql($table)); 00119 00120 if ($tableStatus->getDataLength() > self::BUFFER_LENGTH) { 00121 if ($tableStatus->getAvgRowLength() < self::BUFFER_LENGTH) { 00122 $limit = floor(self::BUFFER_LENGTH / $tableStatus->getAvgRowLength()); 00123 $multiRowsLength = ceil($tableStatus->getRows() / $limit); 00124 } 00125 else { 00126 $limit = 1; 00127 $multiRowsLength = $tableStatus->getRows(); 00128 } 00129 } 00130 else { 00131 $limit = $tableStatus->getRows(); 00132 $multiRowsLength = 1; 00133 } 00134 00135 for ($i = 0; $i < $multiRowsLength; $i ++) { 00136 $backup->write($this->getResource()->getTableDataSql($table, $limit, $i*$limit)); 00137 } 00138 00139 $backup->write($this->getResource()->getTableDataAfterSql($table)); 00140 } 00141 } 00142 $backup->write($this->getResource()->getTableForeignKeysSql()); 00143 $backup->write($this->getResource()->getFooter()); 00144 00145 $this->getResource()->commitTransaction(); 00146 00147 $backup->close(); 00148 00149 return $this; 00150 } 00151 00152 }