Page 1 of 1

Mysql Backup/store per php

Posted: 2004-08-04 19:53
by quicksilver
Huhu ich habe eine Frage und zwar möchte ich per php bei bedarf meine mysql datenbank backupen und per mail zugeschickt bekommen. Das backupen geht ja schon aber das per mail ! hat da wer ne lösung ? und wenns geht noch die db wieder einspielen !

Danke

Code: Select all

<?php
$username  = "uname"; // Username zum connecten zum Mysql Server
$password  = "pw"; // Password zum connecten zum Mysql Server
$host      = "localhost"; // Hostname oder ip mysql Server
$datenbank = "dbname"; // Datenbank Name
$pfad      = "/www/intern"; // Pfad zum webspace /home/www kein abschliessender /
$ipath       = "http://www.homepage.de/intern/";

$date = date("Y.m.d");
system("/usr/bin/mysqldump -u$username -p$password -h$host $datenbank > $pfad/dump_$date.sql", $fp);
if ($fp==0) echo "Daten exportiert<br>Dateiname:
$ipfad/dump_$date.sql"; else echo"Es ist ein Fehler aufgetreten";
echo"<br>";
system("gzip $pfad/dump_$date.sql", $fp);
if ($fp==0) echo "Datei gezippt <br>Dateiname: <A HREF="http://www.homepage.de/intern". $ipfad."/dump_".$date.".sql.gz" ."">dump_$date.sql.gz</A>"; else echo"Es ist ein Fehler aufgetreten";
?>

php.net

Posted: 2004-08-09 09:08
by mc5000
ich wüprde dir http://www.php.net empfehlen ... :wink:

hab mir aber schon ein bissel arbeit für dich gemacht hier mal ein beispiel von der seite, zu, erstellen von anhängen:

Code: Select all

hey, i wrote a few functions that improve on some of the functoins below, this will send emails w/ attachments, the params of the sendMail function are as follows: 

<?php 
$host = smtp server to connect to 

$to = array of emails to send email to, 
ex. array("meep@blah.moo", "blah@meep.moo") ; 

$from = single email of sender, goes into From header, and is sent as the mail from: 

$headers = array of headers, in the form of array("header" => "value") 

$msg = the message 

$attachments = array of paths to files to be attached, can be left out 

   function getMime($filename) { 
       //add more to array if needed :-P 
       $types = array( 
           ".jpg" => "image/jpeg", 
           ".jpeg" => "image/jpeg", 
           ".png" => "image/png" 
           ) ; 
   
       $filename = basename($filename) ; 
       $ext = strtolower(substr($filename, strrpos($filename, "."))) ; 
       return $types[$ext] ; 
   } 
   
   function sendRequest($fp, $req, $rc) 
   { 
       fputs($fp, "$reqrn") ; 
       $ret = sscanf(fgets($fp), "%d") ; 
       $ret = $ret[0] ; 
       
       if($ret != $rc) { 
           fputs($fp, "quitrn") ; 
           fclose($fp) ; 
           
           die("Invalid response from server, $req returned $ret, expected $rc<br/>") ; 
       } 
   }  

   function sendMail($host, $to, $from, $headers, $msg, $attachments="") { 
       $bound = uniqid(time()."_") ; 
       $content = "From: $fromrn" ; 
       $heloFrom = "" ; 
           
       foreach($headers as $header => $value) 
           $content .= "$header: $valuern" ; 
           
       if(is_array($attachments)) { 
           
           $content .= "MIME-Version: 1.0rn" ; 
           $content .= "Content-Type: multipart/mixed; boundary="$bound"rn" ; 
           
           $content .= "--$boundrn" ; 
           $content .= "Content-Type: text/html; charset="iso-8859-1"rn" ; 
           $content .= "Content-Transfer-Encoding: 8bitrnrn$msgrnrn" ; 
           
           foreach($attachments as $attch) { 
               if($fp = fopen($attch, "rb")) { 
                   $bn = basename($attch) ;    
               
                   $content .= "--$boundrn" ; 
                   $content .= "Content-Type: ".getMime($attch)."; name="$bn"rn" ; 
                   $content .= "Content-Transfer-Encoding: base64rn" ; 
                   $content .= "Content-Disposition: inline; filename="$bn"rnrn" ; 
                   $content .= chunk_split(base64_encode(fread($fp, filesize($attch))))."rn" ; 
                   fclose($fp) ; 
               } else { 
                   echo "Error, could not open $attch." ; 
                   return false ; 
               } 
           } 
           $content .= "--$boundrn" ; 
       } else { 
           $content .= "Content-Type: text/html; charset="iso-8859-1"rn" ; 
           $content .= "Content-Transfer-Encoding: 8bitrnrn$msgrnrn" ; 
       } 
       
       $to = array_chunk($to, 5) ; 
       
       foreach($to as $batch) { 
           $fp = fsockopen($host, 25, $errno, $errstr, 60) ; 
       
           if(!$fp) 
               die("Connection failed to $host:$port: ($errno) $errstr<br/>") ; 
               
           do { 
               $res = fgets($fp) ; 
           } while($res[3] == "-") ; 
       
           sendRequest($fp, "helo $heloFrom", 250) ; 
           sendRequest($fp, "mail from: $from", 250) ; 
                   
           foreach($batch as $rcpt) 
                   sendRequest($fp, "rcpt to: $rcpt", 250) ; 
                   
           sendRequest($fp, "data", 354) ; 
           sendRequest($fp, $content."rn.", 250) ; 
           sendRequest($fp, "quit", 221) ; 
           
           fclose($fp) ; 
       } 
   } 
?> 
einspielen ?!

jede zeile lesen und mit mysql_query einspielen .... :wink:

oder file sichern und mit mysql -u user -p pass database < sqlfile zurück spielen

viel spaß :)