-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseBackup.php
More file actions
152 lines (134 loc) · 4.77 KB
/
Copy pathDatabaseBackup.php
File metadata and controls
152 lines (134 loc) · 4.77 KB
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
<?php
/*
* @Author : Channaveer Hakari
* @Email : channaveer888@gmail.com
* @Version : 1.0
* @Description : This Script is used to backup your complete database or partial tables backup
*/
/* Error Reporting */
error_reporting(E_ALL);
/* Creating New Instance for the class DatabaseBackup */
$dbBackup = new DatabaseBackup();
/* @Function Called - backupDatabase()
* @parameters - 1st Param - Default : fetches all the tables
Specific Tables - array(table1,table2,table3...);
* 2nd Param - If not specified then creates one where this script resides
*/
$tables = '*';
$dbBackup->backupDatabase($tables,'BackupLogs');
class DatabaseBackup{
private $hostname = ''; /* DB Hostname */
private $username = ''; /* DB Username */
private $password = ''; /* DB Password */
private $database = ''; /* Database Name */
private $characterSet = 'utf8'; /* DB Character Set */
private $backupDirectory = 'BackupLogs'; /* Backup Directory */
/* Mysqli Connection Handle */
private $link = '';
/* Class Constructor */
function __construct(){
/* Initialization of DB variables */
$this->hostname = 'localhost';
$this->username = 'root';
$this->password = '';
$this->database = 'test_db';
/* Call DB Initialization Function */
$this->initalizeDB();
}
/* Function used to Initialize the MySQL DB */
private function initalizeDB(){
$this->link = mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
/* If any error then display appropriate message */
if(mysqli_connect_error()){
die('Connection Error - '.mysqli_connect_errno().' : '.mysqli_connect_error());
}
/* If the Character Set is not defined then set default defined one */
if(!mysqli_character_set_name($this->link)){
mysqli_set_charset($this->link,$this->characterSet);
}
}
/* Function is used to Backup you Database */
public function backupDatabase($tables = '*',$backupDirectory = ''){
/* If all the tables needed */
if($tables == '*'){
$tables = array();
/* Fetch all the tables of the current database */
$result = mysqli_query($this->link,"SHOW TABLES");
/* Loop through all the rows and assign to $tables array */
while($row = mysqli_fetch_row($result)){
$tables[] = $row[0];
}
}else{
/* If $tables is an array then assign directly else explode the string */
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
/* Create the database */
$sql = 'SET FOREIGN_KEY_CHECKS = 0;'."\n".'CREATE DATABASE IF NOT EXISTS `'.$this->database."`;\n";
/* Use the database */
$sql .= 'USE `'.$this->database.'`;';
/* Loop throug all the $tables */
foreach($tables as $table){
/* Output message */
echo 'Logging Table : `'.$table.'` : ';
/* Fetch the details of the table */
$tableDetails = mysqli_query($this->link, "SELECT * FROM ".$table);
/* Check the Number of Coloumns in the table */
$totalCols = mysqli_num_fields($tableDetails);
/* If the table exists then drop */
$sql .= "\n\nDROP TABLE IF EXISTS `".$table."`;\n";
/* Create the table structure */
$result1 = mysqli_fetch_row(mysqli_query($this->link,'SHOW CREATE TABLE '.$table));
$sql .= $result1[1].";\n\n";
while($row = mysqli_fetch_row($tableDetails)){
$sql .= 'INSERT INTO `'.$table.'` VALUES(';
for($j=0; $j<$totalCols; $j++){
$row[$j] = preg_replace("/\n/","\\n",addslashes($row[$j]));
if (isset($row[$j]))
{
$sql .= '"'.$row[$j].'"' ;
}
else
{
$sql.= '""';
}
if ($j < ($totalCols-1))
{
$sql .= ', ';
}
}
$sql .= "); \n";
}
echo 'Completed <br/>';
}
$sql .= 'SET FOREIGN_KEY_CHECKS = 1;';
/* If the 2nd parameter was not specified then default one will be passed */
$backupDirectory = ($backupDirectory == '') ? $this->backupDirectory : $backupDirectory;
if($this->logDatabase($sql,$backupDirectory)){
echo '<h4>Exported Database <span style="color:#7D0097">`'.$this->database.'`</span>Successfully to folder - <span style="color:#1CAD7A"> `'.$backupDirectory.'`</span><h4>';exit;
}else{
echo '<h2>Error in Exporting Database '.$this->database.'<h2>';exit;
}
}
/* Function used to Log the Database */
private function logDatabase($sql,$backupDirectory = ''){
if(!$sql){
return false;
}
if(!file_exists($backupDirectory)){
if(mkdir($backupDirectory)){
$filename = 'log_'.$this->database.date('Y-m-d_H-i-s');
$fileHandler = fopen($backupDirectory.'/'.$filename.'.sql','w+');
fwrite($fileHandler,$sql);
fclose($fileHandler);
return true;
}
}else{
$filename = 'log_'.$this->database.date('Y-m-d_H-i-s');
$fileHandler = fopen($backupDirectory.'/'.$filename.'.sql','w+');
fwrite($fileHandler,$sql);
fclose($fileHandler);
return true;
}
return false;
}
}