-
- Define (' msb_version ', ' 1.0.0 ');
- Define (' Msb_nl ', "\ r \ n");
- Define (' Msb_string ', 0);
- Define (' Msb_download ', 1);
- Define (' Msb_save ', 2);
- Define (' __sep__ ', "/*sep*/");
- Set_time_limit (600);
- Class Mysql_backup {
- var $server = ' localhost ';
- var $port = 3306;
- var $username = ' root ';
- var $password = ';
- var $database = ';
- var $link _id =-1;
- var $connected = false;
- var $tables = array ();
- var $drop _tables = true;
- var $struct _only = false;
- var $comments = true;
- var $backup _dir = ";
- var $fname _format = ' d_m_y__h_i_s ';
- var $error = ';
- var $complete _inserts = false;
- var $inserts _block = 200;
- function Execute ($task = msb_string, $fname = ", $compress = False) {
- if (! ($sql = $this->_retrieve ())) {
- return false;
- }
- if ($task = = Msb_save) {
- if (empty ($fname)) {
- $fname = $this->backup_dir;
- $fname. = Date ($this->fname_format);
- $fname. = ($compress? '. Sql.gz ': '. sql ');
- }
- return $this->_savetofile ($fname, $sql, $compress);
- } elseif ($task = = msb_download) {
- if (empty ($fname)) {
- $fname = Date ($this->fname_format);
- $fname. = ($compress? '. Sql.gz ': '. sql ');
- }
- return $this->_downloadfile ($fname, $sql, $compress);
- } else {
- return $sql;
- }
- }
- function _connect () {
- $value = false;
- if (! $this->connected) {
- $host = $this->server. ':' . $this->port;
- $this->link_id = mysql_connect ($host, $this->username, $this->password);
- }
- if ($this->link_id) {
- if (Empty ($this->database)) {
- $value = true;
- } elseif ($this->link_id!==-1) {
- $value = mysql_select_db ($this->database, $this->link_id);
- } else {
- $value = mysql_select_db ($this->database);
- }
- }
- if (! $value) {
- $this->error = Mysql_error ();
- }
- return $value;
- }
- function _query ($sql) {
- if ($this->link_id!==-1) {
- $result = mysql_query ($sql, $this->link_id);
- } else {
- $result = mysql_query ($sql);
- }
- if (! $result) {
- $this->error = Mysql_error ();
- }
- return $result;
- }
- function _gettables () {
- $value = Array ();
- if (! ($result = $this->_query (' SHOW TABLES ')) {
- return false;
- }
- while ($row = Mysql_fetch_row ($result)) {
- if (Empty ($this->tables) | | in_array ($row [0], $this->tables)) {
- $value [] = $row [0];
- }
- }
- if (!sizeof ($value)) {
- $this->error = ' No tables found in database. ';
- return false;
- }
- return $value;
- }
- function _dumptable ($table) {
- $value = ";
- $this->_query (' LOCK TABLES ' $table. ' WRITE ');
- if ($this->comments) {
- $value. = ' # '. MSB_NL;
- $value. = ' # Table structure for table '. $table. '`' . MSB_NL;
- $value. = ' # '. Msb_nl. MSB_NL;
- }
- if ($this->drop_tables) {
- $value. = ' DROP TABLE IF EXISTS '. $table. '`;' . __sep__. MSB_NL;
- }
- if (! ($result = $this->_query (' SHOW CREATE TABLE '. $table)) {
- return false;
- }
- $row = Mysql_fetch_assoc ($result);
- $value. = Str_replace ("\ n", Msb_nl, $row [' Create Table ']). ';' . __sep__;
- $value. = Msb_nl. MSB_NL;
- if (! $this->struct_only) {
- if ($this->comments) {
- $value. = ' # '. MSB_NL;
- $value. = ' # Dumping data for table '. $table. '`' . MSB_NL;
- $value. = ' # '. Msb_nl. MSB_NL;
- }
- $value. = $this->_getinserts ($table);
- }
- $value. = Msb_nl. MSB_NL;
- $this->_query (' UNLOCK TABLES ');
- return $value;
- }
- function _getinserts ($table) {
- $value = ";
- if (! ( $result = $this->_query (' SELECT * from '. $table)) {
- return false;
- }
- if ($this->complete_inserts) {
- while ($row = Mysql_fetch_row ($result)) {
- $values = ";
- foreach ($row as $data) {
- $values. = ' \ '. Addslashes ($data). '\', ';
- }
- $values = substr ($values, 0,-2);
- $value. = ' INSERT into '. $table. ' VALUES ('. $values. ');' . __sep__. MSB_NL;
- }
- } else {
- $blocks _counter = 0;
- $blocks = Array ();
- while ($row = Mysql_fetch_row ($result)) {
- $values = Array ();
- foreach ($row as $data) {
- $values [] = ' \ '. Addslashes ($data). '\'';
- }
- $blocks [] = ' ('. Implode (', ', $values). ')';
- if ($blocks _counter < $this->inserts_block) {
- $blocks _counter++;
- } else {
- $value. = ' INSERT into '. $table. ' VALUES '. Implode (', ', $blocks). ";" . __sep__. MSB_NL;
- $blocks = Array ();
- $blocks _counter = 0;
- }
- }
- if (count ($blocks)) {
- $value. = ' INSERT into '. $table. ' VALUES '. Implode (', ', $blocks). ";" . __sep__. MSB_NL;
- }
- }
- return $value;
- }
- function _retrieve () {
- $value = ";
- if (! $this->_connect ()) {
- return false;
- }
- if ($this->comments) {
- $value. = ' # '. MSB_NL;
- $value. = ' # MySQL database dump '. MSB_NL;
- $value. = ' # Created by Mysql_backup class, Ver. '. Msb_version. MSB_NL;
- $value. = ' # '. MSB_NL;
- $value. = ' # Host: '. $this->server. MSB_NL;
- $value. = ' # Generated: '. Date (' M J, Y '). ' at '. Date (' H:i '). MSB_NL;
- $value. = ' # MySQL version: '. Mysql_get_server_info (). MSB_NL;
- $value. = ' # PHP version: '. Phpversion (). MSB_NL;
- if (!empty ($this->database)) {
- $value. = ' # '. MSB_NL;
- $value. = ' # Database: '. $this->database. '`' . MSB_NL;
- }
- $value. = ' # '. Msb_nl. Msb_nl. MSB_NL;
- }
- if (! ( $tables = $this->_gettables ())) {
- return false;
- }
- foreach ($tables as $table) {
- if (! ( $table _dump = $this->_dumptable ($table))) {
- $this->error = Mysql_error ();
- return false;
- }
- $value. = $table _dump;
- }
- return $value;
- }
- function _savetofile ($fname, $sql, $compress) {
- if ($compress) {
- if (! ( $ZF = Gzopen ($fname, ' W9 '))) {
- $this->error = ' can\ ' t create the output file. ';
- return false;
- }
- Gzwrite ($ZF, $sql);
- Gzclose ($ZF);
- } else {
- if (! ( $f = fopen ($fname, ' W '))) {
- $this->error = ' can\ ' t create the output file. ';
- return false;
- }
- Fwrite ($f, $sql);
- Fclose ($f);
- }
- return true;
- }
- function _downloadfile ($fname, $sql, $compress) {
- Header (' content-disposition:filename= '. $fname);
- Header (' Content-type:application/octetstream ');
- Header (' Pragma:no-cache ');
- Header (' expires:0 ');
- Echo ($compress Gzencode ($sql): $sql);
- return true;
- }
- }
- ?>
Copy Code
|