PHP Kullanarak MySQL Yedeği Almak

Yazan: türker | Tarih 8 Ocak 2008 | Yorum  5 Yorum
BerbatKötüOrtaGüzelHarika Henüz puan verilmemiş
Loading ... Loading ...

Bu betiği ile veritabanının yedeğini alıp isterseniz sunucuda saklayabilir, isterseniz email adresinize gönderilmesini sağlayabilirsiniz. Ayrıca eski yedek dosyalarının silinmesini sağlayabileceğiniz gibi, yedek oluşturulduğunda size bilgilendirme maili gönderilmesini de ayarlayabilirsiniz.

Bu dosyayi halen kullanmaktayim ve bir sorun cikarmiyor. Sizin sunucunuz PHPyi safe_mode da calistiriyorsa passthru fonksiyonunu calistirmanizi engelleyebilir. Bu yerine baska fonksiyonlar kullanilabilir ancak muhtemelen onlar da engellenmis olacaktir. Dolayisiyla kendinize yedek almak icin baska yontem arayin.

Bu dosyayi cron jobs veya scheduled tasks kullanarak belli zaman araliklarinda calistirabileceginiz gibi manuel olarak da calistirabilirsiniz.

SendMail fonksiyonunu ben yazmadim ama asil yazan kisiyi de bulamadim. Bilen varsa bana haber verebilir.

Dosyayı indirmek için tıklayın. / Click here to download.

:
  1. /*
  2. ___=== Otomatik Yedek ===___
  3. Yazan: turker (turker.biz@gmail.com)
  4. Bağımlılıklar: yok
  5. #----------------------------
  6.  
  7. ___=== Auto Backup ===___
  8. Author: turker (turker.biz@gmail.com)
  9. Requirements: none
  10. */
  11.  
  12. # settings
  13. $siteName='mysite';
  14.  
  15. $dbName='db'; // database name
  16. $dbUser='db_user'; // database username
  17. $dbPass='db_pass'; // database password
  18.  
  19. $backupFileName=$siteName.'-backup-'.date('dmYHis').'.sql.gz';
  20. $backupDirectory='/home/httpd/vhosts/mysite/public_html/backups';
  21. $backupDownloadURL='http://www.mysite.com/backups/'.$backupFileName;
  22.  
  23. $deleteOldBackupFiles='yes'; // yes|no
  24. $deleteBackupFile='no'; // yes|no
  25. $sendInfoMail='yes'; // yes|no
  26. $sendBackupFileToMail='yes'; // yes|no
  27.  
  28. $mailFrom='backup@mysite.com';
  29. $mailFromName=$siteName.' AutoBackup';
  30. $mailTo='mybackups@gmail.com';
  31. $mailToName='John Doe';
  32. $mailSubject=$siteName.' Backup';
  33. # /settings
  34.  
  35.  
  36. /* ############################################################################################## */
  37. /* NE YAPTIGINIZI BILMIYORSANIZ BURADAN SONRASINA DOKUNMAYIN BENCE                                */
  38. /* DONT CHANGE ANYTHING BELOW IF YOU DONT KNOW WHAT YOU ARE DOING                                 */
  39. /* ############################################################################################## */
  40.  
  41. # create backup
  42. $x=passthru("mysqldump -u$dbUser -p$dbPass $dbName | gzip> $backupDirectory/$backupFileName");
  43. $y=passthru("chmod 755 $backupDirectory/$backupFileName");
  44. if (!$x) { echo "Error 1: $x ".die(); }
  45. if (!$y) { echo "Error 2: $y ".die(); }
  46. # /create backup
  47.  
  48.  
  49. # send information mail
  50. if ($sendInfoMail=='yes') {
  51.   $Html='Database backup created on '.date('d/m/Y H:i:s')."<br>\n";
  52.   $Html.="Backup file path: $backupDirectory/$backupFileName<br>\n";
  53.   $Html.="Backup file URL: $backupDownloadURL<br>\n";
  54.   $Text=strip_tags($HTML);
  55.   SendMail($mailFrom,$mailFromName,$mailTo,$mailToName,$mailSubject,$Text,$Html,'false');
  56. }
  57. # /send information mail
  58.  
  59. # send backup file to e-mail
  60. if ($sendBackupFileToMail=='yes') {
  61.   $Html='Database backup created on '.date('d/m/Y H:i:s')."<br>\n";
  62.   if ($deleteBackupFile=='no') {
  63.     $Html.="Backup file path: $backupDirectory/$backupFileName<br>\n";
  64.     $Html.="Backup file URL: $backupDownloadURL<br>\n";
  65.   }
  66.   $Text=strip_tags($HTML);
  67.   $ATTM=array($backupDirectory/$backupFileName); // more files can be added: $ATTM=array("/home/myself/test/go.jpg", "/home/myself/test/SomeDoc.pdf");  
  68.   SendMail($mailFrom,$mailFromName,$mailTo,$mailToName,$mailSubject,$Text,$Html,$ATTM);
  69. }
  70. # /send backup file to e-mail
  71.  
  72.  
  73. # delete old backup files
  74. if ($deleteOldBackupFiles=='yes') {
  75.   chdir($backupDirectory);
  76.   $link=@opendir($backupDirectory);
  77.   if(!$link) {
  78.     echo 'Error 3: No such directory or path is wrong'; die();
  79.   }
  80.   else {
  81.     while($file=readdir($link)){
  82.       if ($file!='.' &amp;&amp; $file!='..' &amp;&amp; $file!=$backupFileName &amp;&amp; is_file($file))
  83.         unlink($file);
  84.     }
  85.     closedir($link);
  86.   }
  87. }
  88. # /delete old backup files
  89.  
  90.  
  91. # delete backup file
  92. if ($deleteBackupFile=='yes') {
  93.     chdir($backupDirectory);
  94.     $delete=@unlink($backupDirectory/$backupFileName);
  95.     if(!$delete)  {
  96.       echo 'Error 4: No such file or path is wrong'; die();
  97.     }
  98. }
  99. # /delete backup file
  100.  
  101.  
  102. /*  
  103. This might be some useful stuff to send out emails in either text  
  104. or html or multipart version, and attach one or more files or even  
  105. none to it. Inspired by Kieran's msg above, I thought it might be  
  106. useful to have a complete function for doing this, so it can be used  
  107. wherever it's needed. Anyway I am not too sure how this script will  
  108. behave under Windows.  
  109.  
  110. {br} represent the HTML-tag for line break and should be replaced,  
  111. but I did not know how to not get the original tag  parsed here.  
  112.  
  113. function SendMail($From, $FromName, $To, $ToName, $Subject, $Text, $Html, $AttmFiles)  
  114. $From      ... sender mail address like "my@address.com"  
  115. $FromName  ... sender name like "My Name"  
  116. $To        ... recipient mail address like "your@address.com"  
  117. $ToName    ... recipients name like "Your Name"  
  118. $Subject  ... subject of the mail like "This is my first testmail"  
  119. $Text      ... text version of the mail  
  120. $Html      ... html version of the mail  
  121. $AttmFiles ... array containing the filenames to attach like array("file1","file2")  
  122. */  
  123.  
  124. function SendMail($From,$FromName,$To,$ToName,$Subject,$Text,$Html,$AttmFiles){  
  125.   $OB="----=_OuterBoundary_000";  
  126.   $IB="----=_InnerBoundery_001";  
  127.   $Html=$Html?$Html:preg_replace("/\n/","{br}",$Text)  
  128.     or die("neither text nor html part present.");  
  129.   $Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";  
  130.   $From or die("sender address missing");  
  131.   $To or die("recipient address missing");  
  132.        
  133.   $headers ="MIME-Version: 1.0\r\n";  
  134.   $headers.="From: ".$FromName." &lt;".$From."&gt;\n";  
  135.   $headers.="To: ".$ToName." &lt;".$To."&gt;\n";  
  136.   $headers.="Reply-To: ".$FromName." &lt;".$From."&gt;\n";  
  137.   $headers.="X-Priority: 1\n";  
  138.   $headers.="X-MSMail-Priority: High\n";  
  139.   $headers.="X-Mailer: My Mailer\n";  
  140.   $headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";  
  141.  
  142.   //Messages start with text/html alternatives in OB  
  143.   $Msg ="This is a multi-part message in MIME format.\n";  
  144.   $Msg.="\n--".$OB."\n";  
  145.   $Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";  
  146.  
  147.   //plaintext section  
  148.   $Msg.="\n--".$IB."\n";  
  149.   $Msg.="Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n";  
  150.   $Msg.="Content-Transfer-Encoding: quoted-printable\n\n";  
  151.   // plaintext goes here  
  152.   $Msg.=$Text."\n\n";  
  153.  
  154.   // html section  
  155.   $Msg.="\n--".$IB."\n";  
  156.   $Msg.="Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";  
  157.   $Msg.="Content-Transfer-Encoding: base64\n\n";  
  158.   // html goes here  
  159.   $Msg.=chunk_split(base64_encode($Html))."\n\n";  
  160.  
  161.   // end of IB  
  162.   $Msg.="\n--".$IB."--\n";  
  163.  
  164.   // attachments  
  165.   if($AttmFiles){  
  166.     foreach($AttmFiles as $AttmFile){  
  167.      $patharray = explode ("/", $AttmFile);  
  168.      $FileName=$patharray[count($patharray)-1];  
  169.      $Msg.= "\n--".$OB."\n";  
  170.      $Msg.="Content-Type: application/octetstream;\n\tname=\"".$FileName."\"\n";  
  171.      $Msg.="Content-Transfer-Encoding: base64\n";  
  172.      $Msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\n\n";  
  173.                
  174.      //file goes here  
  175.      $fd=fopen ($AttmFile, "r");  
  176.      $FileContent=fread($fd,filesize($AttmFile));  
  177.      fclose ($fd);  
  178.      $FileContent=chunk_split(base64_encode($FileContent));  
  179.      $Msg.=$FileContent;  
  180.      $Msg.="\n\n";  
  181.     }  
  182.   }  
  183.        
  184.   //message ends  
  185.   $Msg.="\n--".$OB."--\n";  
  186.   mail($To,$Subject,$Msg,$headers);      
  187.   //syslog(LOG_INFO,"Mail: Message sent to $ToName &lt;$To&gt;");  
  188. }

Yazdır Yazdır | 1,143 Görüntülenme | Kategori: PHP, Veritabanları & SQL | Trackback  Geri İzleme
Etiketler  Etiketler: , , , , ,

Benzer Yazılar


Yorum Yap


(gerekli)

(gerekli,yayınlanmaz)




XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>