-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTdbBackup.php
197 lines (180 loc) · 5.8 KB
/
STdbBackup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* 09/08/2017
* Wed
* Created By Sunil Thatal
*/
class STdbBackup
{
// $dbServer Contain mysql
private $dbInfo=array(
'server' => 'mysql',
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'dbname'
);
// var $mailId="[email protected]";
private $dirPath="DbBackup";
private $Mysql;
private $sqlInfo="";
public $dbError="";
var $timeZone="Asia/Kolkata";
function __construct($arr=array(),$mailId="")
{
if ($this->ValidateInfo($arr)==true) {
// $dbInfo['server'] =$arr['server'];
$this->dbInfo['username'] =$arr['username'];
$this->dbInfo['password'] =$arr['password'];
$this->dbInfo['dbname'] =$arr['dbname'];
$this->dbInfo['host'] =$arr['host'];
}
$this->OpenConnection();
// $this->backup=$this->BackUp();
}
private function ValidateInfo($arr){
$errorMsg=array();
if (!isset($arr['dbname'])) {
$errorMsg['dbname']="<br>Error!! Database Name not defined.";
}
if (!isset($arr['username'])) {
$errorMsg['username']="<br>Error!! Username Name not defined.";
}
if (!isset($arr['password'])) {
$errorMsg['password']="<br>Error!! Password Name not defined.";
}
if (sizeof($errorMsg)>0) {
foreach ($errorMsg as $key => $value) {
$this->dbError.=$value."\n";
}
return false;
}else{
return true;
}
}
public function BackUp(){
$fileName="";
$this->sqlInfo="-- STdbBackup SQL Backup\n-- Created By Sunil Thatal
\n-- Host: ".$this->dbInfo['host']."\n-- Generation Time:".date('d-m-Y h:i A')." \n-- Server version: 5.6.17\n";
$this->sqlInfo.="\n---- Database: `".$this->dbInfo['dbname']."`--\n\n";
$this->sqlInfo.='SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";'."\n".'SET time_zone = "+00:00";';
/************Dumping Database Table Name, Engine , Auto Increament***/
$sql1="SELECT TABLE_NAME, ENGINE, AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='".$this->dbInfo['dbname']."'";
$result1 = $this->Mysql->query($sql1);
if ($result1) {
if ($result1->num_rows > 0) {
// $dbTableName=array();
while($STrow = $result1->fetch_assoc()) {
$this->sqlInfo.="\n--\n-- Table structure for table `".$STrow['TABLE_NAME']."`\n--\n";
// $this->sqlInfo.="CREATE TABLE IF NOT EXISTS `".$STrow['TABLE_NAME']."` (\n";
$sql2="SHOW CREATE TABLE `".$STrow['TABLE_NAME']."`";
$result2 = $this->Mysql->query($sql2);
if (!$result2) {
$this->dbError="$sql2";
echo $sql2;
}else{
if ($result2->num_rows > 0) {
$dbTableKey=array();
$counter=0;
while($STtableColumns = $result2->fetch_assoc()) {
/****************Table Schema Generating****************/
$this->sqlInfo.=$STtableColumns['Create Table'];
$this->sqlInfo.=";";
}
$sql3="SELECT * FROM `".$STrow['TABLE_NAME']."`";
$result3 = $this->Mysql->query($sql3);
if ($result3->num_rows > 0) {
/****************Dumping Data From table***************************/
$this->sqlInfo.="\n\n--\n-- Dumping data for table `".$STrow['TABLE_NAME']."`\n--\n";
$tableDataArr=array();
while($STtableData = $result3->fetch_assoc()) {
$tableDataArr[]=$STtableData;
}
$counter=0;
$this->sqlInfo.="INSERT INTO `".$STrow['TABLE_NAME']."` (";
foreach ($tableDataArr[0] as $key => $dw) {
if ($counter>0) {
$this->sqlInfo.=", ";
}
$this->sqlInfo.="`".$key."`";
$counter++;
}
$this->sqlInfo.=") VALUES \n";
$counter12=0;
foreach ($tableDataArr as $key => $value) {
if ($counter12>0) {
$this->sqlInfo.=",\n";
}
$this->sqlInfo.="(";
$counter=0;
foreach ($value as $index => $data) {
if ($counter>0) {
$this->sqlInfo.=", ";
}
if (is_numeric($data)!=false) {
$this->sqlInfo.=$data;
}else{
$order = array("\r\n", "\n", "\r");
$replace = '\n';
$data=str_replace("'", "\'", $data);
$data=str_replace($order, $replace, $data);
$this->sqlInfo.="'".$data."'";
// $this->sqlInfo.="'".$data."'";
}
$counter++;
}
$this->sqlInfo.=")";
$counter12++;
}
$this->sqlInfo.=";\n";
}
}
}
}
}else{
$this->dbError=$this->$dbInfo['dbname']." Database is Empty.";
return false;
}
}else{
$this->dbError="Database `".$this->dbInfo['dbname']."` not found!.";
return false;
}
if ($this->dbError==="") {
$fileName="db_backup_".$this->dbInfo['dbname']."_".date('YmdHis').".sql";
// print($this->sqlInfo);
if (!file_exists($this->dirPath)) {
mkdir($this->dirPath, 0777, true);
}
$this->dirPath=rtrim($this->dirPath,"/");
$handle = fopen($this->dirPath."/".$fileName, 'w') or die();
if (!$handle) {
$this->dbError='Cannot open file: '.$fileName;
return false;
}
if (!fwrite($handle, $this->sqlInfo)) {
$this->dbError="Unable to Write file ".$this->dirPath."/".$fileName;
return false;
}
// return $this->sqlInfo;
return $this->dirPath."/".$fileName;
}else{
return false;
}
$this->CloseConnection();
}
private function OpenConnection(){
$this->Mysql = new mysqli($this->dbInfo['host'], $this->dbInfo['username'], $this->dbInfo['password'], $this->dbInfo['dbname']);
// Check connection
if ($this->Mysql->connect_error) {
//die("Connection failed: " . $this->Mysql->connect_error);
$this->dbError="Database Connection Failed: ".$this->Mysql->connect_error;
}else{
return true;
}
}
private function CloseConnection(){
$this->Mysql->close();
}
public function setTimeZone($timeZone=""){
}
}