-
Notifications
You must be signed in to change notification settings - Fork 11
/
db_backup_library.php
230 lines (196 loc) · 6.68 KB
/
db_backup_library.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/*
-- Specification--
-- Class Name is: db_backup
-- Methods
-- connect(Server Name,User name,passwrd,Database)
--Usages: If your project connected with database already
then you don't need to use this 'connect()' Method
--Parameters
1st: Server Name
2nd: Username
3rd: Password
4th: Database Name
-- tables() It will Return an array with all tables name in the database
-- backup() It will Initialize The Backup
-- download() It will download The Backup file in sql
--Parameters
1st: If you want to give a custom name of backup use it Default is 'backup'
-- save() If you want to save the backup file into a server directory you can use it
--parameters
1st: Path URL
2nd: file Name Default Name is backup_yyy-mm-dd
--db_import(source)
Usage: If you want to Import Database from SQL File Use this method
--Parameters
1st: Source/Path of SQL file
*/
class db_backup{
private $exported_database;
public function backup(){
/*-------------------------------------*/
//------Creating Table SQL start-------//
/*-------------------------------------*/
$table_sql = array();
foreach ($this->tables() as $key => $table) {
$tbl_query=mysql_query("SHOW CREATE TABLE ".$table);
$row2 = mysql_fetch_row($tbl_query);
$table_sql[]=$row2[1];
}
$solid_tablecreate_sql = implode("; \n\n" , $table_sql);
/*-------------------------------------*/
//-------Creating Table SQL end--------//
/*-------------------------------------*/
/*-------------------------------------*/
//------Inserting Data SQL Start-------//
/*-------------------------------------*/
$all_table_data=array();
foreach ($this->tables() as $key => $table) {
$show_field=$this->view_fields($table);
$solid_field_name=implode(", ",$show_field);
$create_field_sql="INSERT INTO `$table` ( ".$solid_field_name.") VALUES \n";
//Start checking data available
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
$table_data= mysql_query("SELECT*FROM ".$table);
if(!$table_data){
echo 'Could not run query: '. mysql_error();
}
if (mysql_num_rows($table_data) > 0) {
$data_viewig=$this->view_data($table);
$splice_data = array_chunk($data_viewig,50);
foreach($splice_data as $each_datas){
$solid_data_viewig=implode(", \n",$each_datas)."; ";
$all_table_data[]=$create_field_sql.$solid_data_viewig;
}
}
else{
$all_table_data[]=null;
}
//End checking data available
}
$entiar_table_data=implode(" \n\n\n",$all_table_data);
/*-------------------------------------*/
//-------Inserting Data SQL End--------//
/*-------------------------------------*/
$this->exported_database=$solid_tablecreate_sql."; \n \n".$entiar_table_data;
return $this;
}
//Additional Methods
/*-------------------------------------*/
//--------Functions Start here---------//
/*-------------------------------------*/
public function download($name='backup'){
/*//Download
$file_name="Tmpdata.sql";
$file=fopen($file_name,"w+");
fwrite($file, $this->exported_database);*/
header('Content-Type: application/sql');
header('Content-Disposition: attachment; filename='.$name.'.sql');
echo $this->exported_database;
/*readfile($file_name);
fclose($file);
unlink($file_name);*/
}
public function save($path,$name=""){
$name = ($name != "") ? $name : 'backup_' . date('Y-m-d');
//Save file
$file = fopen($path.$name.".sql","w+");
$fw = fwrite($file, $this->exported_database);
if(!$fw){
return false;
}
else {
return true;
}
}
public function connect($server,$user,$pass,$db){
$conn=mysql_connect($server,$user,$pass);
$condb=mysql_select_db($db);
if(!$conn || !$condb){
echo mysql_error();
}else{
return true;
}
}
public function tables(){
/*-------------------------------------*/
//------Creating Table List start------//
/*-------------------------------------*/
$tb_name=mysql_query("SHOW TABLES");
$tables=array();
while ($tb=mysql_fetch_row($tb_name)) {
$tables[]=$tb[0] ;
}
/*-------------------------------------*/
//-------Creating Table List end-------//
/*-------------------------------------*/
return $tables;
}
public function view_fields($tablename){
$all_fields=array();
$fields = mysql_query("SHOW COLUMNS FROM ".$tablename);
if (!$fields) {
echo 'Could not run query: ' . mysql_error();
}
if (mysql_num_rows($fields) > 0) {
while ($field = mysql_fetch_assoc($fields)) {
$all_fields[]="`".$field["Field"]."`";
}
}
return $all_fields;
}
public function view_data($tablename){
$all_data=array();
$table_data=mysql_query("SELECT*FROM ".$tablename);
if(!$table_data){
echo 'Could not run query: '. mysql_error();
}
if(mysql_num_rows($table_data)>0){
while ($t_data=mysql_fetch_row($table_data)) {
$per_data=array();
foreach ($t_data as $key => $tb_data) {
$per_data[]= "'".str_replace("'","\'",$tb_data)."'";
}
$solid_data= "(".implode(", ",$per_data).")";
$all_data[]=$solid_data;
}
}
return $all_data;
}
/*-------------------------------------*/
//---------Functions End here----------//
/*-------------------------------------*/
//Export End here==================================================================
//Import Start here==================================================================
function db_import($file_path){
$tbl_query=null;
foreach ($this->tables() as $key => $table) {
$tbl_query=mysql_query("DROP TABLE IF EXISTS ".$table);
}
//---------------------------------------------------------------------------
//Forign code Start here
//---------------------------------------------------------------------------
$templine = '';
// Read in entire file
$lines = file($file_path);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
//echo "Database imported successfully <br/>";
return true;
}
}