Ergenekon Forum Kurucusu
Mesaj Sayısı : 405 Nerden : Cehennem
| Konu: Php ile Veritabanını Yedeklemek Cuma Tem. 11 2008, 11:31 | |
| PHP Kodu - Kod:
-
#!/usr/bin/php
<?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.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 "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.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>"); } ?>
-------------------------------------------------------------------------------- <?php
ob_start();
$username = "root"; $password = "pass"; $hostname = "localhost"; $sConnString = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$connection = mysql_select_db("vt",$sConnString) or die("Could not select DB");
$command = "mysqldump --add-drop-table --host=localhost --user='root' --password='pass' vt"; system($command);
$dump = ob_get_contents(); ob_end_clean();
$dizin = "Appserv/www"; $dosya = "$dizin/yedek.sql"; $fp = fopen($dosya, "w"); fputs($fp, $dump); fclose($fp);
// echo $dump;
?>
-------------------------------------------------------------------------------- <?php require_once('Connections/mts.php'); mysql_select_db($database_mts, $mts); ?> <?php function datadump ($table) {
$result .= "# Dump of $table \n"; $result .= "# Dump DATE : " . date("d-M-Y") ."\n\n";
$query = mysql_query("select * from $table"); $num_fields = @mysql_num_fields($query); $numrow = mysql_num_rows($query);
for ($i =0; $i<$numrow; $i++) { $row=mysql_fetch_row($query); $result .= "INSERT INTO ".$table." VALUES("; for($j=0; $j<$num_fields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = ereg_replace("\n","\\n",$row[$j]); if (isset($row[$j])) $result .= "\"$row[$j]\"" ; else $result .= "\"\""; if ($j<($num_fields-1)) $result .= ","; } $result .= ");\n"; } return $result . "\n\n\n"; }
function datadumpDB(){ $query = mysql_query("show tables"); while($row=mysql_fetch_row($query)){ $query2 = mysql_query("SHOW CREATE TABLE " . $row[0]); $row2=mysql_fetch_row($query2); $result .= $row2[1] . ";\n\n"; $result .= datadump($row[0]); }
while($row=mysql_fetch_row($query)){ $result .= datadump($row[0]); } return $result; }
$content = datadumpDB();
$file_name = "Backup/mts_Backup". date("ymd") .".sql"; $tmpfname = tempnam ("", $file_name); $handle = fopen($tmpfname, "w"); fwrite($handle, $content); fclose($handle); if (!copy($tmpfname, $file_name)) { print ("failed to copy $file...<br>\n"); } unlink($tmpfname); header("Content-type: application/force-download"); header("Content-Disposition: attachment; filename=\"".$file_name."\";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ". filesize($file_name)); $fh = fopen($file_name, "r"); fpassthru($fh); fclose($fh); unlink($file_name); exit(); | |
|