-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlDump.php
More file actions
170 lines (131 loc) · 4.42 KB
/
SqlDump.php
File metadata and controls
170 lines (131 loc) · 4.42 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
# Database Database Dump class
# Version 0.1
# Writen By Kakhaber Kashmadze <info@soft.ge>
# Licensed under MIT License
# Script works with archive type gzip and with databases MySQL and PostgreSQL.
# On windows environment path variable must be set for mysqldump and pg_dump
class SqlDump{
private $dbType='mysql'; /* By default database type is mysql. Allowed types for mysql is mysql, for postgresql is pgsql */
private $dumpType='gzip'; /* Type of archive */
private $dbParams=array("host","user","password","database"); /* Database parameters */
function __construct(){
;
}
/* Return database parameters */
function retDbParams(){
return $this->dbParams;
}
/* Set database parameters */
function setDbParams($dbParams){
$this->dbParams=$dbParams;
}
/* Returns Database Type */
function retDbType(){
return $this->dbType;
}
/* Sets Database Type: mysql or pgsql */
function setDbType($dbType){
$dbtype=strtolower(trim($dbType));
if($dbtype=='mysqli') $dbtype='mysql';
if($dbtype=='postgresql') $dbtype='pgsql';
$this->dbType=$dbtype;
}
/* Return dump archvie type */
function retDumpType(){
return $this->dumpType;
}
/* Set dump archvie type */
function setDumpType($dumpType){
$this->dumpType=$dumpType;
}
/* Download file */
function downFile($file, $fileNm, $ctype) {
if (file_exists($file)) {
/*
if(ob_get_level()!==0) ob_clean();
header('Content-Description: File Transfer');
header('Content-Type: '.$ctype.'');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename=' . $fileNm);
readfile($file);
unlink($file);
exit;
*/
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$fileNm);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
}
}
/* Dump database and download */
function save($dumpPath){
$dbparams=$this->retDbParams();
$dbhost = $dbparams[0];
$dbuser = $dbparams[1];
$dbpass = $dbparams[2];
$dbname = $dbparams[3];
$dump_path=$dumpPath;
$fileName=$dbname."_".date("Y-m-d-H-i-s").".sql";
$dump_file=$dump_path.$fileName;
if($this->retDbType()=='mysql'){
$conn=@mysql_connect($dbhost,$dbuser,$dbpass);
if(!$conn){
echo "Error when connecting to server";
return false;
}elseif(!mysql_select_db($dbname, $conn)){
echo "Error selecting database";
return false;
}
}elseif($this->retDbType()=='pgsql'){
$conn=@pg_pconnect("host=".$dbhost." port=5432 dbname=".$dbname." user=".$dbuser." password=".$dbpass."");
if(!$conn){
echo "Error when connecting to database";
return false;
}
}
if($this->retDbType()=='mysql')
$command = "mysqldump --opt -h ".$dbhost." -u ".$dbuser." -p".$dbpass." ".$dbname." > ".$dump_file;
elseif($this->retDbType()=='pgsql'){
if(!stristr(PHP_OS, 'WIN')) exec("export PGPASSWORD=".$dbpass);
else exec("set PGPASSWORD=".$dbpass);
$command = "pg_dump -h ".$dbhost." -U ".$dbuser." ".$dbname." > ".$dump_file;
}
exec($command);
if(filesize($dump_file)<=0) {
echo "\nError: size of file is ".filesize($dump_file);
return false;
}
if($this->retDumpType()=='gzip'){
$dumpFileGzip=$dump_file.'.gz';
$downloadFileName=$fileName.'.gz';
$fp=fopen($dump_file, "rb");
if($fp){
if(filesize($dump_file)>0){
$gz = gzopen($dumpFileGzip,'wb9');
while (!feof($fp)) {
gzwrite($gz, fread($fp, 8096));
}
unlink($dump_file);
fclose($fp);
gzclose($gz);
$this->downFile($dumpFileGzip, $downloadFileName, "application/x-gzip");
}else{
echo "\nError: size of file is ".filesize($dump_file);
return false;
}
}else{
echo "\nError";
return false;
}
}
return false;
}
}