TeknoTR  

Geri Dön   TeknoTR > Webmasterler için > Webmaster Genel
Üye Ol SSS Sxe indir Sosyal Gruplar Takvim Resim Galerisi Etiketler Bütün Forumları okunmuş kabul et

Php ile Veritabanını Yedeklemek

Webmaster Genel bölümünde Php ile Veritabanını Yedeklemek konusu , PHP Kodu: #!/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 = ...

Cevapla
 
LinkBack Konu Seçenekleri
Eski 12-01-2007, 11:05   #1 (permalink)
Üye
 
immortal - ait Avatar
 
Giriş: 28-01-2006
Yaş: 2
Mesajlar: 259
Rep Puanı: 305
immortal Rütbe Artı +1immortal Rütbe Artı +1immortal Rütbe Artı +1immortal Rütbe Artı +1
Rep Gücü: 36
E-Güven: (0/0)

Php ile Veritabanını Yedeklemek


PHP Kodu:
#!/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>"); 

?>

__________________
buraya ne yazım önerirlerinizi pm atın
[Bu Linki Görüntüleyebilmeniz İçin Üye Olmanız Gerekiyor. ]
immortal isimli üyemiz çevrimdışıdır. (Offline)  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Alıntı ile Cevapla
Eski 12-01-2007, 11:06   #2 (permalink)
Üye
 
immortal - ait Avatar
 
Giriş: 28-01-2006
Yaş: 2
Mesajlar: 259
Rep Puanı: 305
immortal Rütbe Artı +1immortal Rütbe Artı +1immortal Rütbe Artı +1immortal Rütbe Artı +1
Rep Gücü: 36
E-Güven: (0/0)

Ce: Php ile Veritabanını Yedeklemek


PHP Kodu:
<?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;

?>

__________________
buraya ne yazım önerirlerinizi pm atın
[Bu Linki Görüntüleyebilmeniz İçin Üye Olmanız Gerekiyor. ]
immortal isimli üyemiz çevrimdışıdır. (Offline)  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Alıntı ile Cevapla
Eski 12-01-2007, 11:08   #3 (permalink)
Üye
 
immortal - ait Avatar
 
Giriş: 28-01-2006
Yaş: 2
Mesajlar: 259
Rep Puanı: 305
immortal Rütbe Artı +1immortal Rütbe Artı +1immortal Rütbe Artı +1immortal Rütbe Artı +1
Rep Gücü: 36
E-Güven: (0/0)

Ce: Php ile Veritabanını Yedeklemek


PHP Kodu:
<?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();
çalıştırdığında direk bütün db nin backupını oluşturup (CREATE lerle birlikte) indirme penceresi çıkarır. bu arada hepsi işe yarıo

__________________
buraya ne yazım önerirlerinizi pm atın
[Bu Linki Görüntüleyebilmeniz İçin Üye Olmanız Gerekiyor. ]
immortal isimli üyemiz çevrimdışıdır. (Offline)  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Alıntı ile Cevapla
Cevapla


Konuyu Toplam 1 üye okuyor. (0 Kayıtlı üye ve 1 Misafir)
 
Konu Seçenekleri

Yetkileriniz
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Açık
Smileler Açık
[IMG] Kodları Açık
HTML-KodlarıKapalı
Trackbacks are Açık
Pingbacks are Açık
Refbacks are Açık
Gitmek istediğiniz klasörü seçiniz


Şu an saat 14:38 .
Tarih 08-10-2008


Powered by vBulletin Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd
Türkçe Çeviri : ach

eXTReMe Tracker


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192