-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
158 lines (132 loc) · 5.12 KB
/
test.php
File metadata and controls
158 lines (132 loc) · 5.12 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
<?php
//$myfile = fopen("testfile.txt", "w");
//fclose($myfile);
$ettings = mysql_fetch_object(mysql_query("SELECT * FROM settings"));
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)
{
$cur_time = date("D d.m.Y H:i:s");
$timename = date("D_d.m.Y_H.i.s");
$dat="# Backup of '$db_name': $cur_time \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($val,$table);
$dat .= "\n\n";
$dat .= "#\n# Data for table '$table'\n#\n";
$dat .= get_content($val,$table);
$dat .= "\n\n";
$i++;
}
$gzip = "";
if($compression == true && extension_loaded("zlib"))
$gzip = ".gz";
else
$compression = false;
writeBackupFile($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);
}
function get_def($db_name, $table, $sql_connection) {
$def = "";
$def .= "CREATE TABLE $table (\n";
$result = mysql_db_query($db_name, "SHOW FIELDS FROM $table",$sql_connection);
while($row = mysql_fetch_array($result)) {
$def .= " $row[Field] $row[Type]";
if ($row["Default"] != "") $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_db_query($dbname, "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, ", ") . ")";
}
$def .= "\n);";
return (stripslashes($def));
}
function get_content($dbname, $table, $sql_connection) {
$content="";
$result = mysql_db_query($dbname, "SELECT * FROM $table",$sql_connection);
while($row = mysql_fetch_row($result)) {
$insert = "INSERT 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 = "This 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.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content.= "Database backup.\r\n";
$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;
}
?>