login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Database tools > Exporteer database & mail

Exporteer database & mail

Auteur: Ben - 12 juni 2005 - 09:41 - Gekeurd door: Maarten - Hits: 6207 - Aantal punten: 0.50 (2 stemmen)





Ten eerste: ik heb dit script niet zelf gemaakt, maar ik vond het zodanig handig dat ik het toch wou delen!
Wat doet het?
Je vult je database gegevens in, en de andere noodzakelijke info.
Dan kan je nog kiezen of je oftewel mail, oftewel ftp oftewel beide wil gebruiken om het op te slaan!

Een tip: typ bij ontvanger je GMail adres, en stel dit in als cronjob! Zo kan je wel tot 2GB aan gegevens kwijt.

Extra nota:

Een database van 8.31 MB werd bij mij via .gz verkleint tot zo'n 664 Kb!
Je kan dus back-ups bijhouden van enkele jaartjes, zelfs met grote databases.

Original:
http://www.sonn...php?t=1927

Code:
  1. <?php
  2.  
  3. $dbhost = 'localhost'; // Server address of your MySQL Server
  4. $dbuser = ''; // Username to access MySQL database
  5. $dbpass = ''; // Password to access MySQL database
  6. $dbname = ''; // Database Name
  7.  
  8. $use_gzip = 'yes'; // Set to No if you don't want the files sent in .gz format
  9.  
  10. $remove_file = 'yes'; // Set this to yes if you want to remove the file after sending. Yes is recommended.
  11.  
  12. $use_email = 'yes'; // Set to 'yes' if you want the backup to be sent throug email. Fill out next 3 lines.
  13. $send_to = ''; // E-mail to send the mail to
  14. $send_from = ''; // E-mail the mail comes from
  15. $subject = "MySQL Database ($dbname) Backup - " . date("j F Y"); // Subject in the email to be sent.
  16.  
  17. $use_ftp = 'yes'; // Do you want this database backup uploaded to an ftp server? Fill out the next 4 lines
  18. $ftp_server = '66.79.174.100'; // FTP hostname
  19. $ftp_user_name = ''; // FTP username
  20. $ftp_user_pass = ''; // FTP password
  21. $ftp_path = "/public_html/database/logs"; // This is the path to upload on your ftp server!
  22.  
  23. $echo_status = 'yes'; // Set to 'no' if the script should work silently (no output will be sent to the screen)
  24.  
  25.  
  26. # You probably don't need to edit below this line....
  27. #-------------------------------------------------------------------------------
  28.  
  29. $db = mysql_connect($dbhost,$dbuser,$dbpass);
  30. mysql_select_db($dbname,$db);
  31.  
  32. $path = make_dir();
  33.  
  34. if ($echo_status == 'yes') {
  35. print "Dumpfile will be written to ".$path."<br>";
  36. }
  37.  
  38. $result = mysql_query("show tables from ".$dbname);
  39. while (list($table) = mysql_fetch_row($result)) {
  40. $newfile .= get_def($table);
  41. $newfile .= "\n\n";
  42. $newfile .= get_content($table);
  43. $newfile .= "\n\n";
  44. $i++;
  45. if ($echo_status == 'yes') {
  46. print "Dumped table ".$table."<br>";
  47. }
  48. }
  49.  
  50. $file_name = $dbname . "-" . date("Ymd-Hi") . ".sql";
  51. $file_path = $path . $file_name;
  52.  
  53. if ($use_gzip == "yes") {
  54. $file_name .= ".gz";
  55. $file_path .= ".gz";
  56. $zp = gzopen($file_path, "wb9");
  57. gzwrite($zp,$newfile);
  58. gzclose($zp);
  59.  
  60. if ($echo_status == 'yes') {
  61. print "<br>Gzip-file is created...<br>";
  62. }
  63. } else {
  64. $fp = fopen($file_path, "w");
  65. fwrite($fp, $newfile);
  66. fclose($fp);
  67.  
  68. if ($echo_status == 'yes') {
  69. print "<br>SQL-file is created...<br>";
  70. }
  71. }
  72.  
  73. if ($use_email == 'yes') {
  74. $fileatt_type = filetype($file_path);
  75.  
  76. $headers = "From: ".$send_from;
  77.  
  78. // Read the file to be attached ('rb' = read binary)
  79. $fp = fopen($file_path,'rb');
  80. $data = fread($fp,filesize($file_path));
  81. fclose($fp);
  82.  
  83. // Generate a boundary string
  84. $semi_rand = md5(time());
  85. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  86.  
  87. // Add the headers for a file attachment
  88. $headers .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
  89.  
  90. // Add a multipart boundary above the plain message
  91. $message = "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" ."Content-Type: text/plain; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n" .
  92. $message .= "\n\n";
  93.  
  94. // Base64 encode the file data
  95. $data = chunk_split(base64_encode($data));
  96.  
  97. // Add file attachment to the message
  98. $message .= "--{".$mime_boundary."}\n" ."Content-Type: {".$fileatt_type."};\n" ." name=\"{".$file_name."}\"\n" ."Content-Disposition: attachment;\n" ." filename=\"{".$file_name."}\"\n" ."Content-Transfer-Encoding: base64\n\n" .
  99. $data .= "\n\n" ."--{".$mime_boundary."}--\n";
  100.  
  101. // Send the message
  102. $ok = @mail($send_to, $subject, "Here is the file you Ordered Expect another 1 in 4 hours\n\nThe file is called".$file_name."\n\n".$subject."\n".$message, $headers);
  103.  
  104. if ($echo_status == 'yes') {
  105. print "<br>Mail is sent...<br>";
  106. }
  107. }
  108.  
  109. if ($use_ftp == 'yes') {
  110. if ($use_gzip == 'yes') {
  111. $mode = FTP_BINARY;
  112. } else {
  113. $mode = FTP_ASCII;
  114. }
  115. $ftp_id = ftp_connect($ftp_server);
  116. $login_result = ftp_login($ftp_id, $ftp_user_name, $ftp_user_pass);
  117. $upload = ftp_put($ftp_id, $ftp_path . $file_name, $file_path, $mode);
  118. ftp_close($ftp_id);
  119.  
  120. if ($echo_status == 'yes') {
  121. print "<br>Backup is uploaded to ".$ftp_user_name."@".$ftp_server."...<br>";
  122. }
  123. }
  124.  
  125. if ($remove_file == "yes") {
  126. unlink($file_name);
  127. if ($echo_status == 'yes') {
  128. print "<br>File is deleted...<br>";
  129. }
  130. }
  131.  
  132. if ($echo_status == 'yes') {
  133. print "<br>I am done!<br>";
  134. }
  135.  
  136.  
  137. function make_dir() {
  138. $page = split("/", getenv('SCRIPT_NAME'));
  139. $n = count($page)-1;
  140. $page = $page[$n];
  141. $page = split("\.", $page, 2);
  142. $extension = $page[1];
  143. $page = $page[0];
  144. $script = "$page.$extension";
  145. $base_url = "http://".$_SERVER['SERVER_NAME'];
  146. $directory = $_SERVER['PHP_SELF'];
  147. $url_base = "$base_url$directory";
  148. $url_base = ereg_replace("$script", '', "$_SERVER[PATH_TRANSLATED]");
  149.  
  150. $path = $url_base;
  151.  
  152. return $path;
  153. }
  154.  
  155. function get_def($table) {
  156. $def = "";
  157. $def .= "DROP TABLE IF EXISTS ".$table.";\n";
  158. $def .= "CREATE TABLE $table (\n";
  159. $result = mysql_query("SHOW FIELDS FROM ".$table) or die("Table ".$table." not existing in database");
  160. while($row = mysql_fetch_array($result)) {
  161. $def .= " $row[Field] $row[Type]";
  162. if ($row["Default"] != "") $def .= " DEFAULT '$row[Default]'";
  163. if ($row["Null"] != "YES") $def .= " NOT NULL";
  164. if ($row[Extra] != "") $def .= " $row[Extra]";
  165. $def .= ",\n";
  166. }
  167. $def = ereg_replace(",\n$","", $def);
  168. $result = mysql_query("SHOW KEYS FROM $table");
  169. while($row = mysql_fetch_array($result)) {
  170. $kname=$row[Key_name];
  171. if(($kname != "PRIMARY") && ($row[Non_unique] == 0)) $kname="UNIQUE|$kname";
  172. if(!isset($index[$kname])) $index[$kname] = array();
  173. $index[$kname][] = $row[Column_name];
  174. }
  175. while(list($x, $columns) = @each($index)) {
  176. $def .= ",\n";
  177. if($x == "PRIMARY") $def .= " PRIMARY KEY (" . implode($columns, ", ") . ")";
  178. else if (substr($x,0,6) == "UNIQUE") $def .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
  179. else $def .= " KEY $x (" . implode($columns, ", ") . ")";
  180. }
  181. $def .= "\n);";
  182. return (stripslashes($def));
  183. }
  184.  
  185. function get_content($table) {
  186. $content="";
  187. $result = mysql_query("SELECT * FROM ".$table);
  188. while($row = mysql_fetch_row($result)) {
  189. $insert = "INSERT INTO $table VALUES (";
  190. for($j=0; $j<mysql_num_fields($result);$j++) {
  191. if(!isset($row[$j])) $insert .= "NULL,";
  192. else if($row[$j] != "") $insert .= "'".addslashes($row[$j])."',";
  193. else $insert .= "'',";
  194. }
  195. $insert = ereg_replace(",$","",$insert);
  196. $insert .= ");\n";
  197. $content .= $insert;
  198. }
  199. return $content;
  200. }
  201.  
  202.  
  203. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (3)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.091s