Database is the backbone of every dynamic websites. That's why webmaster needs to often backup there website's database. There are many ways to backup MySQL database like backup form phpmyadmin, using shell commend and also many other ways. Today I am going to show you my way to backup MySQL database. I have coded a PHP class to take MySQL database backup. This class can backup all or specified tables on a database.
The DBBackup Class:
Configuration:
General usage:
Backup Specified tables:
The DBBackup Class:
class DBBackup { private $con; private $tables = array(); private $path; function __construct($host = null, $username = null, $password = null, $database = null) { /* Connect and select MySQL DB */ if( is_null($host) || is_null($username) || is_null($password) || is_null($database) ) throw new Exception("The host, username, password and database name arguments must be specified!"); if( ! $this->con = @mysql_connect($host, $username, $password) ) throw new Exception("Could not connect MySQL server {$username}@{$host}"); if( ! @mysql_select_db($database, $this->con) )throw new Exception("Could not select database: {$database}"); } public function table( $table = 'all' ) { /* Get defined tables */ if( is_array( $table )){ // Cheque table validity foreach($table as $t){ if(in_array($t , $this->getAllTables())){ $this->tables[] = $t; } else{ throw new Exception("Table `{$t}` not exists in the DB"); } } } elseif( $table == 'all' ){ $this->tables = $this->getAllTables(); } else{ throw new Exception("Please enter tables name as array"); } } public function path($path = '') { /* Set upload path */ $this->path = $path; } public function backUp() { /* Done backup */ $return = ''; foreach($this->tables as $table){ $return .= $this->tableStracture($table); foreach ($this->recode($table) as $result) { $return .= $result; } } $this->writeToFile($return); print_r($return); return true; } private function getAllTables() { /* Get tables list name from DB */ $sql = mysql_query("SHOW TABLES"); while ($row = mysql_fetch_row($sql) ) { foreach ($row as $key => $value) { $table[] = $value; } } return $table; } private function tableStracture($table) { /* Get table stracture */ $return = "\nDROP TABLE IF EXISTS `{$table}`;\n\n"; $row = ( mysql_fetch_row(mysql_query("SHOW CREATE TABLE {$table}")) ); $return .= $row[1].";\n\n"; return $return; } private function recode($table) { /* Get data recodes */ $query = mysql_query("SELECT * FROM {$table} LIMIT 0, 1000"); $num_fields = mysql_num_fields($query); $num_rows = mysql_num_rows($query); $results = array(); if ($num_rows){ while($row = mysql_fetch_row($query)) { $return = "INSERT INTO {$table} VALUES("; for($i=0; $i<$num_fields; $i++) { $row[$i] = addslashes($row[$i]); $row[$i] = str_replace("\n","\\n",$row[$i]); $row[$i] = str_replace("\r","\\r",$row[$i]); $return .= ( isset($row[$i]) ) ? "'{$row[$i]}'" : "''"; if ($i<($num_fields-1)) { $return.= ','; } } $return.= ");\n"; $results[] = $return; } } return $results; } private function writeToFile($str) { /* Write down backup file */ $path = (isset($this->path))? $this->path : ''; $backupPath = $path . date('Y-m-d-H-i').'_'.md5(uniqid()).'.sql'; if( ! $handle = @fopen( $backupPath ,'w+') ) throw new Exception("Could not save backup file at {$backupPath}"); fwrite($handle, $str); fclose($handle); } public function close() { /* Close MySQL connection */ mysql_close($this->con); } }
Configuration:
define('HOST', 'localhost'); // Host name define('USERNAME', 'root'); // User name define('PASSWORD', 'root'); // Password define('DATABASE', 'mydb'); // DB Name define('PATH', 'backup/'); // Backup Directory
General usage:
try { $db = new DBBackup(HOST, USERNAME, PASSWORD, DATABASE); $db->path(PATH); $db->backUp(); $db->close(); echo "Done!"; } catch (Exception $e) { die($e->getMessage()); }
Backup Specified tables:
try { $db = new DBBackup(HOST, USERNAME, PASSWORD, DATABASE); $db->table(array('table1', 'table2', 'table3')); $db->path(PATH); $db->backUp(); $db->close(); echo "Done!"; } catch (Exception $e) { die($e->getMessage()); }
Thank you ist super!!!! Reno
ReplyDeletedich vu ke toan thue dich vu lam bao cao tai chinh tín
ReplyDeletekhóa học kế toán thực hành re
cong ty dich vu ke toan
dich vu ke toan tai bac ninh
dịch vụ kế toán trọn gói giá rẻ
dịch vụ kế toán tại tp.hcm
dịch vụ báo cáo thuế
dịch vụ quyết toán thuế uy
học kế toán tại tphcm
học kế toán tại cầu giấy tín
học kế toán tại long biên
học kế toán tại hà đông re
học kế toán tại thanh xuân
học kế toán tại bắc ninh
học kế toán tại bình dương
học kế toán tại hải phòng
dịch vụ thành lập doanh nghiệp trọn gói
dịch vụ thành lập doanh nghiệp tại bắc ninh
dịch vụ quyết toán thuế tại quận 5
dịch vụ quyết toán thuế tại quận 3
dịch vụ quyết toán thuế tại tphcm
dịch vụ quyết toán thuế tại quận cầu giấy
dịch vụ quyết toán thuế tại quận long biên
dịch vụ quyết toán thuế tại quận hà đông
dịch vụ quyết toán thuế tại quận thanh xuân
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeletePHP Training in chennai | PHP Training Course
PHP Training in chennai | Online PHP Course
dự án Eurowindow river park có giá bán chỉ từ 17tr/m2
ReplyDeletechung cư the k park