-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.php
More file actions
169 lines (143 loc) · 5.78 KB
/
Copy pathbackup.php
File metadata and controls
169 lines (143 loc) · 5.78 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
<?php
//$myfile = fopen("testfile.txt", "w");
//fclose($myfile);
/*
function backup($db_name, $sql_connection)
{
return backup(false, false, "", "backup/", $db_name, $sql_connection);
}
function backup($compression, $db_name, $sql_connection)
{
return backup($compression, false, "", "backup/", $db_name, $sql_connection);
}
function backup($compression, $backup_folder, $db_name, $sql_connection)
{
return backup($compression, false, "", $backup_folder, $db_name, $sql_connection);
}
*/
function backup($compression, $backup_send_mail, $backup_mail, $backup_folder, $db_name, $sql_connection)
{
mysql_select_db($db_name, $sql_connection);
$cur_time = date("D d.m.Y H:i:s");
$timename = date("D_d.m.Y_H.i.s");
global $version;
$dat ="-- VERSION ".$version." \n";
$dat .="-- Backup of '$db_name': $cur_time \n";
$dat .= "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\n";
$tables = mysql_list_tables($db_name,$sql_connection);
$num_tables = @mysql_num_rows($tables);
$i = 0;
while($i < $num_tables)
{
$table = mysql_tablename($tables, $i);
$dat .= "\n-- ----------------------------------------------------------\n--\n";
$dat .= "-- Structure for Table '$table'\n--\n";
$dat .= get_def($db_name,$table, $sql_connection);
$dat .= "\n\n";
$dat .= "--\n-- Data for table '$table'\n--\n";
$dat .= get_content($db_name,$table, $sql_connection);
$dat .= "\n\n";
$i++;
}
$gzip = "";
if($compression == true && extension_loaded("zlib"))
$gzip = ".gz";
else
$compression = false;
global $rootfolder;
writeBackupFile($_SERVER['DOCUMENT_ROOT'].$rootfolder.$backup_folder.$db_name."_".$timename.".sql".$gzip, $compression, $dat);
$sent = false;
if($backup_send_mail)
$sent = sendFileMail($backup_mail, "Backup of ".$db_name." on ".$cur_time, $backup_folder.$db_name."_".$timename.".sql".$gzip);
return array("sentmail"=>$sent, "mailreciever"=>$backup_mail, "filename"=>$backup_folder.$db_name."_".$timename.".sql".$gzip, "md5"=>md5_file($backup_folder.$db_name."_".$timename.".sql".$gzip));
}
function get_def($db_name, $table, $sql_connection) {
$def = "";
$def .= "CREATE TABLE IF NOT EXISTS `$table` (\n";
$result = mysql_query("SHOW FULL COLUMNS FROM $table",$sql_connection);
if(!$result)
return "";
while($row = mysql_fetch_array($result)) {
$def .= " `$row[Field]` $row[Type]";
if (strlen($row["Collation"])> 0) $def .= " CHARACTER SET ".explode("_", $row["Collation"])[0]." COLLATE ".$row["Collation"];
if ($row["Default"] != "") if($row['Default'] != "CURRENT_TIMESTAMP" && $row['Default'] != "NULL") $def .= " DEFAULT '$row[Default]'"; else $def .= " DEFAULT $row[Default]";
if ($row["Null"] != "YES") $def .= " NOT NULL";
if ($row[Extra] != "") $def .= " $row[Extra]";
$def .= ",\n";
}
$def = ereg_replace(",\n$","", $def);
$result = mysql_query("SHOW KEYS FROM $table",$sql_connection);
while($row = mysql_fetch_array($result)) {
$kname=$row[Key_name];
if(($kname != "PRIMARY") && ($row[Non_unique] == 0)) $kname="UNIQUE|$kname";
if(!isset($index[$kname])) $index[$kname] = array();
$index[$kname][] = $row[Column_name];
}
while(list($x, $columns) = @each($index)) {
$def .= ",\n";
if($x == "PRIMARY") $def .= " PRIMARY KEY (" . implode($columns, ", ") . ")";
else if (substr($x,0,6) == "UNIQUE") $def .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
else $def .= " KEY $x (" . implode($columns, ", ") . ")";
}
$sql = "SHOW TABLE STATUS FROM `".$db_name."` LIKE '$table';";
$status = mysql_fetch_object(mysql_query($sql,$sql_connection));
$def .= "\n) ";
$def .= "ENGINE=".$status->Engine." DEFAULT CHARSET=".explode("_", $status->Collation)[0]." AUTO_INCREMENT=".($status->Auto_increment!=""?$status->Auto_increment:"1").";";
return (stripslashes($def));
}
function get_content($dbname, $table, $sql_connection) {
$content="";
$result = mysql_query("SELECT * FROM $table",$sql_connection);
while($row = mysql_fetch_row($result)) {
$insert = "REPLACE INTO $table VALUES (";
for($j=0; $j<mysql_num_fields($result);$j++) {
if(!isset($row[$j])) $insert .= "NULL,";
else if($row[$j] != "") $insert .= "'".addslashes($row[$j])."',";
else $insert .= "'',";
}
$insert = ereg_replace(",$","",$insert);
$insert .= ");\n";
$content .= $insert;
}
return $content;
}
function writeBackupFile($filename, $compression, $dat)
{
if ($compression==1 || $compression)
{
$fp = gzopen($filename,"w");
gzwrite ($fp,$dat);
gzclose ($fp);
}
else
{
$fp = fopen ($filename,"w");
fwrite ($fp,$dat);
fclose ($fp);
}
}
function sendFileMail($mail, $subject, $file)
{
$mime_boundary = "-----=" . md5(uniqid(rand(), 1));
$header = "From: backup@server.com \r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: multipart/mixed;\r\n";
$header.= " boundary=\"".$mime_boundary."\"\r\n";
$content = "\r\n\r\nThis is a multi-part message in MIME format.\r\n\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-type: text/plain;charset=utf-8\r\n";
$content.= "MD5 of $name :".md5_file($file);
$name = basename($file);
$data = chunk_split(base64_encode(implode("", file($file))));
$len = filesize($file);
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Disposition: attachment;\r\n";
$content.= "\tfilename=\"$name\";\r\n";
$content.= "Content-Length: .$len;\r\n";
$content.= "Content-Type: application/x-gzip; name=\"".$file."\"\r\n";
$content.= "Content-Transfer-Encoding: base64\r\n\r\n";
$content.= $data."\r\n";
if(mail($mail, $subject, $content, $header)) return true;
else return false;
}
?>