PHP ile MySQL Yedeği Almak ve E-Maile Göndermek

Devil

Script Hacker
Katılım
25 Haz 2005
Mesajlar
2,499
Reaction score
0
Puanları
0
Konum
Neredesin ?
Bu PHP kodu ile MySQL veritabanı yedeğini alabilirsiniz. Eğer isterseniz yedeğin alındğına dair e-posta gönderebilir ve yedeği de e-postaya ekleyebilirsiniz.

Kod:
<?php 
// Ayarlar 
$saatfarki = 0; 
$tarihsaat = date("d-m-Y H:i:s",(time()+3600*$saatfarki)); 
$tarih=date("dmY",(time()+3600*$saatfarki)); 
$site="site"; //site adı 
$cpanel_user="site.net"; //site user name (cpanel kullanıcı adı) 
$dizin="/home/httpd/vhosts/$cpanel_user/private"; // yedek alınacak dizinin adı 
$yedek_dosya='backup-'.$tarih.'sql.gz'; // yedek dosyasının adı 
$dosya_sil='evet'; // yedek oluşturulduktan sonra eski yedek dosyaları silinsin mi? 
$yedek_sil='hayır'; // oluşturulan yedek dosyası silinsin mi? 
$email_gonder='evet'; // yedek alındığında e-mail gönderilsin mi? 
$dosya_gonder='evet'; // yedek, emaile gönderilsin mi? 
$dbuser='user_user'; // veritabanı kullanıcı adı 
$dbpass='user_pass'; // veritabanı kullanıcı şifresi 
$dbname='user_db'; // veritabanı adı 
$dosya_adres="$dizin/$yedek_dosya"; 

// E-Mail Ayarları 
$gonderme_tarihi=$tarihsaat; 
$kime='[email protected]'; 
$kime_isim='USER'; 
$kimden='[email protected]'; 
$kimden_isim='DBUSER'; 
$baslik=$site.' -Veritabanı Yedeği-'.$gonderme_tarihi; 


// Veritabanı yedeğini al 
$x=passthru("mysqldump -u$dbuser -p$dbpass $dbname | gzip > $dizin/$yedek_dosya"); 
$y=passthru("chmod 755 $dizin/$yedek_dosya"); 
if (!$x || !$y) { echo "Veritabanı yedeği alınamıyor. -$x , $y-";die(); } 

// yedeğin alındığını bildiren email gönder 
if ($email_gonder=='evet') { 
    $headers="MIME-Version: 1.0\n"; 
    $headers.="Content-type: text/html; charset=iso-8859-9\n"; 
    $headers.="X-Mailer: PHP\n"; 
    $headers.="X-Sender: <PHP>\n"; 
    $headers.="From: <$kimden>\n"; 
    $headers.="Return-Path: <$kimden>\n"; 
    $mesaj=$gonderme_tarihi.' tarhinde alınan '.$site.' MySQL veritabanı yedeği '.$dizin.' dizini içindeki '.$yedek_dosya.' dosyasıdır.'; 
    mail($kime,$baslik,$mesaj,$headers); 
} 

// eski yedek dosyalarını sil 
if ($dosya_sil=='evet') { 
    chdir($dizin); 
    $link=@opendir($dizin); 
    if(!$link){ 
        echo 'Belirtilen isimde bir klasör bulunamadı veya belirtilen yol yanlış!';die(); 
    } 
    else { 
        while($dosya=readdir($link)){ 
            if ($dosya!='.' && $dosya!='..' && $dosya!=$yedek_dosya && is_file($dosya)){ 
                 unlink($dosya); 
             } 
        } 
        closedir($link); 
    } 
} 

// yedek dosyasını e-maile gönder 
if ($dosya_gonder=='evet') { 
$TEXT=""; 
$HTML='<b>'.$gonderme_tarihi.'</b> tarhinde alınan<b> '.$site.'</b> MySQL veritabanı yedeği<br><b> '.$dizin.'</b> dizini içindeki <b>'.$yedek_dosya.' </b>dosyasıdır.'; 
#$ATTM=array("/home/myself/test/go.jpg", "/home/myself/test/SomeDoc.pdf"); 
$ATTM=array($dosya_adres); 
SendMail( "$kimden","$kimden_isim", "$kime","$kime_isim",$baslik,$TEXT,$HTML,$ATTM); 


//echo $HTML; 
} 

// yedek dosyasını sil 
if ($yedek_sil=='evet') { 
    chdir($dizin); 
    $link=@opendir($dizin); 
    if(!$link){ echo 'Belirtilen isimde bir klasör bulunamadı veya belirtilen yol yanlış!';die(); } 
    else { unlink($yedek_dosya); } 
    closedir($link); 
} 



/* 
This might be some useful stuff to send out emails in either text 
or html or multipart version, and attach one or more files or even 
none to it. Inspired by Kieran's msg above, I thought it might be 
useful to have a complete function for doing this, so it can be used 
wherever it's needed. Anyway I am not too sure how this script will 
behave under Windows. 

{br} represent the HTML-tag for line break and should be replaced, 
but I did not know how to not get the original tag  parsed here. 

function SendMail($From, $FromName, $To, $ToName, $Subject, $Text, $Html, $AttmFiles) 
$From      ... sender mail address like "[email protected]" 
$FromName  ... sender name like "My Name" 
$To        ... recipient mail address like "[email protected]" 
$ToName    ... recipients name like "Your Name" 
$Subject  ... subject of the mail like "This is my first testmail" 
$Text      ... text version of the mail 
$Html      ... html version of the mail 
$AttmFiles ... array containing the filenames to attach like array("file1","file2") 
*/ 

function SendMail($From,$FromName,$To,$ToName,$Subject,$Text,$Html,$AttmFiles){ 
$OB="----=_OuterBoundary_000"; 
$IB="----=_InnerBoundery_001"; 
$Html=$Html?$Html:preg_replace("/\n/","{br}",$Text) 
  or die("neither text nor html part present."); 
$Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail."; 
$From or die("sender address missing"); 
$To or die("recipient address missing"); 
     
$headers ="MIME-Version: 1.0\r\n"; 
$headers.="From: ".$FromName." <".$From.">\n"; 
$headers.="To: ".$ToName." <".$To.">\n"; 
$headers.="Reply-To: ".$FromName." <".$From.">\n"; 
$headers.="X-Priority: 1\n"; 
$headers.="X-MSMail-Priority: High\n"; 
$headers.="X-Mailer: My PHP Mailer\n"; 
$headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n"; 

//Messages start with text/html alternatives in OB 
$Msg ="This is a multi-part message in MIME format.\n"; 
$Msg.="\n--".$OB."\n"; 
$Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n"; 

//plaintext section 
$Msg.="\n--".$IB."\n"; 
$Msg.="Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n"; 
$Msg.="Content-Transfer-Encoding: quoted-printable\n\n"; 
// plaintext goes here 
$Msg.=$Text."\n\n"; 

// html section 
$Msg.="\n--".$IB."\n"; 
$Msg.="Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n"; 
$Msg.="Content-Transfer-Encoding: base64\n\n"; 
// html goes here 
$Msg.=chunk_split(base64_encode($Html))."\n\n"; 

// end of IB 
$Msg.="\n--".$IB."--\n"; 

// attachments 
if($AttmFiles){ 
  foreach($AttmFiles as $AttmFile){ 
   $patharray = explode ("/", $AttmFile); 
   $FileName=$patharray[count($patharray)-1]; 
   $Msg.= "\n--".$OB."\n"; 
   $Msg.="Content-Type: application/octetstream;\n\tname=\"".$FileName."\"\n"; 
   $Msg.="Content-Transfer-Encoding: base64\n"; 
   $Msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\n\n"; 
             
   //file goes here 
   $fd=fopen ($AttmFile, "r"); 
   $FileContent=fread($fd,filesize($AttmFile)); 
   fclose ($fd); 
   $FileContent=chunk_split(base64_encode($FileContent)); 
   $Msg.=$FileContent; 
   $Msg.="\n\n"; 
  } 
} 
     
//message ends 
$Msg.="\n--".$OB."--\n"; 
mail($To,$Subject,$Msg,$headers);      
//syslog(LOG_INFO,"Mail: Message sent to $ToName <$To>"); 
} 
?>

Alıntıdır
 
Geri
Üst