· 8 years ago · Mar 15, 2018, 12:00 AM
1<?php
2////////////////////////////////////////////////////////////////
3//
4// Warning: Editing the following without instruction or
5// knowledge of how it works may lead to errors in its
6// execution, always consult me before editing any of this
7//
8// ~David A Perez
9//
10////////////////////////////////////////////////////////////////
11
12//////////////////////////////////////////////////////////
13// Mysql config
14//////////////////////////////////////////////////////////
15$db_host = "mysqldb2.ehost-services.com:3306";// host address
16$db_username = "jacka_prod2"; // username
17$db_password = "password123"; // password
18$db_name = "jackatida_production_2"; // database name
19//////////////////////////////////////////////////////////
20//////////////////////////////////////////////////////////
21$show_date = date("d-m-Y");
22
23//////////////////////////////////////////////////////////
24// email config
25//////////////////////////////////////////////////////////
26$sendto = array();
27 $sendto[] = "davidp@thecityof.net";
28$sendfrom = "AS Automated Backup <sys@cityofproduction.com>";
29$subject = "Automated MySQL Backup: ".$show_date;
30$message = "See attatchments";
31//////////////////////////////////////////////////////////
32//////////////////////////////////////////////////////////
33
34
35//////////////////////////////////////////////////////////
36// Create DB Backup
37//////////////////////////////////////////////////////////
38$db_obj = new backup($db_host, $db_username, $db_password, $db_name);
39 $db_obj->create_backup();
40
41$zip = new ZipArchive();
42$zip_filetoadd = $db_obj->backup_file;
43$zip_filename = $db_obj->filename_zip;
44
45$res = $zip->open("backups/".$zip_filename, ZipArchive::CREATE);
46
47if($res === TRUE){
48 $zip->addFile($zip_filetoadd);
49 $zip->close();
50}else{
51 echo 'Failed to create zip file';
52}
53
54//////////////////////////////////////////////////////////
55//////////////////////////////////////////////////////////
56 foreach($sendto as $value){
57 if(send_email($value) === true){
58 echo "Email Sent";
59 }else{
60 echo "Email delivery failed";
61 }
62 }
63////////////////////////////////////////////////////////////////
64function send_email($email){
65 global $sendfrom;
66 global $subject;
67 global $message;
68 global $db_obj;
69
70 $finfo = finfo_open(FILEINFO_MIME_TYPE);
71
72 $f_arr = array();
73 $f_arr['file'] = $db_obj->backupzip_file;
74 $f_arr['name'] = basename($f_arr['file']);
75 $f_arr['size'] = filesize($f_arr['file']);
76 $f_arr['mime'] = finfo_file($finfo,$f_arr['file']);
77
78 echo "<pre>";
79 print_r($f_arr);
80 echo "</pre>";
81
82 $random_hash = md5(date('r', time()));
83
84 $status = false;
85
86 $attachment = chunk_split(base64_encode(file_get_contents($f_arr['file'])));
87
88 // Generate a random string to be used as the boundary marker
89 $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
90
91 // Building message headers
92 $headers = "From: ".$sendfrom."\r\n" .
93 "MIME-Version: 1.0\r\n" .
94 "Content-Type: multipart/mixed;\r\n" .
95 "boundary=\"{$mime_boundary}\"";
96
97 // Starting the message body
98 $body .= "This is a multi-part message in MIME format.\n\n" .
99 "--{$mime_boundary}\n" .
100 "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
101 "Content-Transfer-Encoding: 7bit\n\n" .
102 $message . "\n\n";
103
104 // Starting the message body
105 $body .= "This is a multi-part message in MIME format.\n\n" .
106 "--{$mime_boundary}\n" .
107 "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
108 "Content-Transfer-Encoding: 7bit\n\n" .
109 $message . "\n\n";
110
111 $body .= "--{$mime_boundary}\n" .
112 "Content-Type: {$f_arr['mime']};\n" .
113 " name=\"{$f_arr['name']}\"\n" .
114 "Content-Disposition: attachment;\n" .
115 " filename=\"{$f_arr['name']}\"\n" .
116 "Content-Transfer-Encoding: base64\n\n" .
117 $attachment . "\n\n";
118
119 $body .= "--{$mime_boundary}--\n";
120 if(mail($email,$subject,$body,$headers)){
121 $status = true;
122 }
123
124 echo "<pre>";
125 echo $body;
126 echo "</pre>";
127 return $status;
128}
129
130////////////////////////////////////////////////////////////////
131
132////////////////////////////////////////////////////////////////
133class mysql_functions{
134
135 private $db_host;
136 private $db_username;
137 private $db_password;
138 private $db_name;
139 private $connect;
140
141 function __construct($host, $username, $password, $dbname){
142 $this->db_host = $host;
143 $this->db_username = $username;
144 $this->db_password = $password;
145 $this->db_name = $dbname;
146 }
147
148 public function open_mysql(){
149 $this->connect = mysql_connect($this->db_host, $this->db_username, $this->db_password);
150
151 if(!$this->connect){
152 die('Error connecting to MySQL: '.mysql_error());
153 }
154 mysql_select_db($this->db_name, $this->connect);
155 }
156 public function close_mysql(){
157 mysql_close($this->connect);
158 }
159
160 public function test($table){
161 $this->open_mysql();
162 $query = "SELECT * FROM $table";
163
164 $results = mysql_query($query);
165
166 if(!$results){
167 die("Error fetching MySQL results: ".mysql_error()." /// ".$query);
168 }
169
170 if(0<mysql_num_rows($results)){
171 while($row = mysql_fetch_assoc($results)){
172 echo "<pre>";
173 print_r($row);
174 echo "</pre>";
175 }
176 }
177 $this->close_mysql();
178 }
179}
180
181class backup extends mysql_functions{
182 private $filepath;
183 private $backup_extention;
184 public $filename_base;
185 public $filename_txt;
186 public $filename_zip;
187 private $tables;
188 public $backup_file;
189 public $backupzip_file;
190 private $data;
191
192 public function __construct($host, $username, $password, $dbname){
193 parent::__construct($host, $username, $password, $dbname);
194 $this->filepath = 'backups/';
195 $this->backup_extention = "txt";
196 $this->filename_base = $dbname."-".date("Y-m-d");
197 $this->filename_txt = $this->filename_base.".".$this->backup_extention;
198 $this->filename_zip = $this->filename_base.".zip";
199
200 $this->backup_file = $this->filepath.$this->filename_txt;
201 $this->backupzip_file = $this->filepath.$this->filename_zip;
202 $this->tables = $this->get_tables($dbname);
203 }
204 public function get_tables($dbname){
205 $this->open_mysql();
206 $query = "SHOW TABLES FROM ".$dbname;
207 $results = mysql_query($query);
208
209 if(!$results){
210 die("DB Error, Could not list tables:\n ".mysql_error()." /// ".$query);
211 }
212
213 while($row = mysql_fetch_row($results)){
214 $output[] = $row[0];
215 }
216
217 $this->close_mysql();
218 return $output;
219 }
220
221 public function create_backup(){
222 $backupfile = $this->filepath.$this->filename_base.".".$this->backup_extention;
223 $this->data = $this->create_backupdata();
224 $fh = fopen($backupfile,'w+');
225 fwrite($fh, $this->data);
226 fclose($fh);
227 }
228
229 public function create_backupdata(){
230 foreach($this->tables as $table){
231 $this->open_mysql();
232 $return.= 'DROP TABLE IF EXISTS '.$table.';';
233 $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
234 $return.= "\n\n".$row2[1].";\n\n";
235
236
237 $result = mysql_query('SELECT * FROM '.$table);
238 $num_fields = mysql_num_fields($result);
239 for ($i=0;$i<$num_fields;$i++){
240 while($row = mysql_fetch_row($result)){
241 $return.= 'INSERT INTO '.$table.' VALUES(';
242 for($j=0; $j<$num_fields; $j++){
243 $row[$j] = addslashes($row[$j]);
244 $row[$j] = ereg_replace("\n","\\n",$row[$j]);
245 if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
246 if ($j<($num_fields-1)) { $return.= ','; }
247 }
248 $return.= ");\n";
249 }
250 }
251 $return.="\n\n\n";
252 $this->close_mysql();
253 }
254 return $return;
255 }
256}
257?>