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. (Kodda kullanılan sendmail() fonksiyonu bana ait değildir.)
PHP Kodu:
<?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='user@user.com';
$kime_isim='USER';
$kimden='dbbackup@user.net';
$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.0n";
$headers.="Content-type: text/html; charset=iso-8859-9n";
$headers.="X-Mailer: PHPn";
$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 "my@address.com"
$FromName ... sender name like "My Name"
$To ... recipient mail address like "your@address.com"
$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.0rn";
$headers.="From: ".$FromName." <".$From.">n";
$headers.="To: ".$ToName." <".$To.">n";
$headers.="Reply-To: ".$FromName." <".$From.">n";
$headers.="X-Priority: 1n";
$headers.="X-MSMail-Priority: Highn";
$headers.="X-Mailer: My PHP Mailern";
$headers.="Content-Type: multipart/mixed;ntboundary="".$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;ntboundary="".$IB.""nn";
//plaintext section
$Msg.="n--".$IB."n";
$Msg.="Content-Type: text/plain;ntcharset="iso-8859-1"n";
$Msg.="Content-Transfer-Encoding: quoted-printablenn";
// plaintext goes here
$Msg.=$Text."nn";
// html section
$Msg.="n--".$IB."n";
$Msg.="Content-Type: text/html;ntcharset="iso-8859-1"n";
$Msg.="Content-Transfer-Encoding: base64nn";
// html goes here
$Msg.=chunk_split(base64_encode($Html))."nn";
// 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;ntname="".$FileName.""n";
$Msg.="Content-Transfer-Encoding: base64n";
$Msg.="Content-Disposition: attachment;ntfilename="".$FileName.""nn";
//file goes here
$fd=fopen ($AttmFile, "r");
$FileContent=fread($fd,filesize($AttmFile));
fclose ($fd);
$FileContent=chunk_split(base64_encode($FileContent));
$Msg.=$FileContent;
$Msg.="nn";
}
}
//message ends
$Msg.="n--".$OB."--n";
mail($To,$Subject,$Msg,$headers);
//syslog(LOG_INFO,"Mail: Message sent to $ToName <$To>");
}
?>

Leave a Reply