PHP ile Veritabanı Yedekleme - PHP Veritabanı Dışa Aktarma

PHP ile Veritabanı Yedekleme işlemleri

PHP ile Veritabanı Yedekleme - PHP Veritabanı Dışa Aktarma

Veritabanı Yedekleme PHP

disa_aktar.php dosyamızı oluşturuyoruz.

<?php 
$dbhost = "localhost"; //Veritabanın bulunduğu host
$dbuser = "root"; //Veritabanı Kullanıcı Adı
$dbpass = ""; //Veritabanı Şifresi
$dbdata = "mysql_vt"; //Veritabanı Adı

Veritabanı bilgilerimizi ekliyoruz.

include 'DBBackupRestore.class.php'; //DBBackup.class.php dosyamızı dahil ediyoruz
$dbBackup = new DBYedek(); // class'imizla $dbBackup nesnemizi olusturduk

DBBackupRestore.class.php dosyamızı include ediyoruz. ve nesnemizi oluşturuyoruz.

 //$kayityeri klasor yolu belirtirken sonunda mutlaka / olmali (klasoradi/) seklinde
$kayityeri	= "temp/";	// ayni dizin için $kayityeri degiskeni bos birakilmali - dosya yolumuz yedek alacağımız
$arsiv		= false;	//Yedeği zip arsivi olarak almak için true // .sql olarak almak için false
$tablosil	= true;		//DROP TABLE IF EXISTS satırı eklemek için true // istenmiyorsa false

Yedek alacağımız dosya yolunu temp klasorü olarak belirtiyoruz. eğer zip olarak yedek almak istiyorsanız. arsiv değişkenini true olarak değiştiriniz.

//Veri için kullanılacak sözdizimi:
	$veritipi	= 1; // INSERT INTO tbl_adı VALUES (1,2,3);
	//$veritipi	= 2; // INTO tbl_adı VALUES (1,2,3), (4,5,6), (7,8,9);
	//$veritipi	= 3; // INSERT INTO tbl_adı (sütun_A,sütun_B,sütun_C) VALUES (1,2,3);
	//$veritipi	= 4; // INSERT INTO tbl_adı (col_A,col_B,col_C) VALUES (1,2,3), (4,5,6), (7,8,9);

Ayarlamak istediğiniz veri tipini seçiniz. ve diğerlerini yorum satırına alınız.

$backup = $dbBackup->Disa_Aktar($kayityeri, $arsiv, $tablosil, $veritipi);

	if($backup){
		echo '' . $backup . ' <<<<< İndir..';
	} else {
		echo 'Beklenmedik hata oluştu!';
	}
$dbBackup->kapat();

ob_end_flush();

$dbBackup nesnemizi kapattık. 

DBBackupRestore.class.php dosyamızı oluşturuyoruz. ve aşağıdaki kodları ekliyoruz.

 ini_set('memory_limit', '1000M');

class DBYedek {

	private $tablolar = array();
	private $baglan;
	private $sonuc;
    private $error = array();

public function __construct() {
    global $dbhost, $dbuser, $dbpass, $dbdata;
	try {
		$this->baglan = new PDO("mysql:host={$dbhost};dbname={$dbdata}",$dbuser,$dbpass,
		array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
		echo "Veritabanı bağlantısı PDO:: >>>> ΟΚ

";
	} catch (PDOException $e) {
		echo "HATA:Baglantı hatası ". $e->getMessage();
		$this->kapat(); exit;
	}
}


public function kapat() {
    if($this->baglan) { $this->baglan = null; }
}


public function Ice_Aktar($dosya,$maxKomut){
    $sonzaman = time()+$maxKomut; 
    $progressdosya = $dosya.'_temp'; // işlenmiş kayıtlar için temp dosyası

    ($fp = fopen($dosya, 'r')) OR die('
' . $dosya . ': Dosyası Açılamıyor!');
    // önceki dosya konumuna git
    $doPozisyon = 0;
    if( file_exists($progressdosya) ){
        $doPozisyon = file_get_contents($progressdosya);
        fseek($fp, $doPozisyon);
    }
    $donguSayaci = 0;
    $query = '';
    while( $sonzaman>time() AND ($line=fgets($fp, 1024000)) ){
        if(substr($line,0,2)=='--' OR trim($line)=='' OR substr($line,0,2)=='/*'){
            continue;
        }
        $query .= $line;
        if( substr(trim($query),-1)==';' ){
			try {
				$islem = $this->baglan->prepare($query);
				$islem->execute();
			} catch (PDOException $e) {
				echo $this->hatabul($e->getTrace(), $e->getCode(), $e->getMessage());
				$this->kapat(); exit;
			}	
            $query = '';
            file_put_contents($progressdosya, ftell($fp)); // geçerli dosya konumunu kaydet
            $donguSayaci++;
        }
    }

    if( feof($fp) ){
		@unlink($progressdosya);
        echo 'Döküm başarıyla içe aktarıldı!
Geçici temp dosyası temizlendi..';
    }else{
		// otomatik yeniden yükleme
		echo ' ';
		// sayfaya bilgi gönder
        echo ftell($fp).'/'.filesize($dosya).' - '.(round(ftell($fp)/filesize($dosya), 2)*100).'%'."
";
        echo $donguSayaci.' sorgular işlendi! lütfen yeniden yükleyin, veya tarayıcının otomatik yenilenmesini bekleyin!';
    }
}

	
public function Disa_Aktar($yol,$zip,$drop,$vtip){
		global $dbdata;
		echo "" . $dbdata . " veritabanı için yedek alma işlemi başladı....

";
		$this->sonuc = "-- " . $dbdata ." veritabanı için sql yedeği.\n\n";
		$this->TabloCek($vtip);
		$this->Olustur($drop);

        $date=date('d-m-Y_H-i-s');
    if ($zip == true){
        $dosya = $yol . "backup-$dbdata-$date.sql.zip";
        $zip = new ZipArchive();
        if ($zip->open($dosya, ZIPARCHIVE::CREATE) !== TRUE) {
            exit("Zip Dosya açılamadı <$dosya>\n");
        }
        $zip->addFromString("backup-$dbdata-$date.sql", $this->sonuc);
        $zip->close();
	} else {
        $dosya = $yol . "backup-$dbdata-$date.sql";
        $fp = fopen($dosya, 'w');
	    fwrite($fp, $this->sonuc);
	    fclose($fp);
	}

		return $dosya;
}


private function Olustur($drop){
		foreach ($this->tablolar as $tbl) {
			$this->sonuc .= "-- `" . $tbl['t_adi'] . "` tablosu için tablo yapısı.\n\n";
			if($drop) { $this->sonuc .= "DROP TABLE IF EXISTS `" . $tbl['t_adi'] . "`;\n"; }
			$this->sonuc .= $tbl['t_yapisi'] . ";\n\n";
			$this->sonuc .= $tbl['t_verisi']."\n\n\n";
		}
}


private function TabloCek($vtip){
		try {
			$sql = $this->baglan->query('SHOW TABLES');
			$sonuc = $sql->fetchAll();
			$i=0;
			foreach($sonuc as $tablo){
				$this->tablolar[$i]['t_adi'] = $tablo[0];
			ob_flush();
			flush();
				$this->tablolar[$i]['t_yapisi'] = $this->tabloYapisi($tablo[0]);
			ob_flush();
			flush();
				$this->tablolar[$i]['t_verisi'] = $this->dokumVeri($tablo[0],$vtip);
			ob_flush();
			flush();
			    echo "" . $tablo[0] . " tablosu >>>> ΟΚ

";
				$i++;
			}
			unset($sql);
			unset($sonuc);
			unset($i);
			return true;
		} catch (PDOException $e) {
	          echo $this->hatabul($e->getTrace(), $e->getCode(), $e->getMessage());
	          $this->kapat(); exit;
		}
}


private function tabloYapisi($tabloAdi){
		try {
		    echo "" . $tabloAdi . " tablosu için tablo yapısı alınıyor....
";
			$sql = $this->baglan->query('SHOW CREATE TABLE '.$tabloAdi);
			$sonuc = $sql->fetchAll();
			$sonuc[0][1] = preg_replace("/AUTO_INCREMENT=[\w]*./", '', $sonuc[0][1]);
			return $sonuc[0][1];
		} catch (PDOException $e){
	          echo $this->hatabul($e->getTrace(), $e->getCode(), $e->getMessage());
	          $this->kapat(); exit;
		}
}


private function sutunAdlari($tabloAdi) {
		try {
        $sutun = "";
        $sql = $this->baglan->query("SELECT * FROM $tabloAdi LIMIT 1");
        $toplm_sutun = $sql->columnCount();
        for ($counter = 0; $counter < $toplm_sutun; $counter ++) {
            $clmn_meta = $sql->getColumnMeta($counter);
			if ($counter+1 != $toplm_sutun){
				$sutun .= '`' .$clmn_meta['name'] . '`' . ', ';
			} else {
				$sutun .= '`' .$clmn_meta['name'] . '`';
			} 
        }
		
        return $sutun;
		} catch (PDOException $e){
	          echo $this->hatabul($e->getTrace(), $e->getCode(), $e->getMessage());
	          $this->kapat(); exit;
		}
}


private function Degistir($veri){
	$veri = str_replace("'", "\'", $veri);
	$veri = str_replace("\n", "\\n", $veri);
	$veri = str_replace('"', '\"', $veri);
	return $veri;
}


private function dokumVeri($tabloAdi,$vtip){
		try {
			$sql = $this->baglan->query('SELECT * FROM '.$tabloAdi);
			$sonuc = $sql->fetchAll(PDO::FETCH_NUM);
			$veri = '';
			$sayim = count($sonuc);
		    echo "" . $tabloAdi . " tablosu için döküm verisi alınıyor....
";
		if ($vtip == 1){
			
			if ($sayim > 0){
				$veri = "-- `" . $tabloAdi . "` tablosu için döküm verisi.\n\n";
				for ($counter = 0; $counter < $sayim; $counter ++) {
					$veri .= "INSERT INTO `". $tabloAdi ."` VALUES ('" . implode("','", $this->Degistir($sonuc[$counter])) . "');\n";
				}
			}
			
		} else if ($vtip == 2){
			
			if ($sayim > 0){
				$veri = "-- `" . $tabloAdi . "` tablosu için döküm verisi.\n\n";
				$veri .= 'INSERT INTO `'. $tabloAdi .'` VALUES'."\n";
				for ($counter = 0; $counter < $sayim; $counter ++) {
					if ($counter+1 != $sayim){
						$veri .= "('" . implode("','", $this->Degistir($sonuc[$counter])) . "'),\n";
					} else {
						$veri .= "('" . implode("','", $this->Degistir($sonuc[$counter])) . "');\n";
					}
				}
			}
			
		} else if ($vtip == 3){
			
			if ($sayim > 0){
				$veri = "-- `" . $tabloAdi . "` tablosu için döküm verisi.\n\n";
				for ($counter = 0; $counter < $sayim; $counter ++) {
					$veri .= "INSERT INTO `". $tabloAdi ."` (" . $this->sutunAdlari($tabloAdi) . ") VALUES ('" . implode("','", $this->Degistir($sonuc[$counter])) . "');\n";
				}
			}
			
		} else if ($vtip == 4){
			
			if ($sayim > 0){
				$veri = "-- `" . $tabloAdi . "` tablosu için döküm verisi.\n\n";
				$veri .= 'INSERT INTO `'. $tabloAdi .'` (' . $this->sutunAdlari($tabloAdi) . ') VALUES'."\n";
				for ($counter = 0; $counter < $sayim; $counter ++) {
					if ($counter+1 != $sayim){
						$veri .= "('" . implode("','", $this->Degistir($sonuc[$counter])) . "'),\n";
					} else {
						$veri .= "('" . implode("','", $this->Degistir($sonuc[$counter])) . "');\n";
					}
				}
			}
			
		}
		
			return $veri;
		} catch (PDOException $e){
	          echo $this->hatabul($e->getTrace(), $e->getCode(), $e->getMessage());
	          $this->kapat(); exit;
		}
}


private function hatabul($hata, $kodu, $mesaj) {
	$htmsj = "PHP PDO HATA: " . strval($kodu) . "

";
	$i=0;
    foreach ($hata as $a){
	if($i==0){ $htmsj .="Class tarafı hata bilgileri
"; }else{ $htmsj .="Dosya tarafı hata bilgileri
"; }
	$htmsj .= "Hatalı Function: ". $a["function"] . "
";
	$htmsj .= "Hatalı Dosya: ". $a["file"] . "
";
	$htmsj .= "Hatalı Satır: ". $a["line"] . "

";
	$i++;
    }
	$htmsj .= "Hata MSJ: " . $mesaj;
	return $htmsj;
}	
	
}

sitemize giderek  x..com/disa_aktar.php  diyerek yedek alabilirsiniz.

Dosyalar

Tepkiniz Nedir?

like
3
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0