I’ve seen the results from phpMyAdmin, but I don’t know what to do inside the backup code
I love this backup code
There are many options
Backup all
Backup up tables separately into a folder
Backup up selected tables
Backup up selected tables in fused or in folder separately
Provides very large database backups
No time out
Full backup code
Code is running, backing up
It only gives an error message when optimize and lock is active,
but it’s still backing up
<?php
###############################################################################
/*
// POST data sent for backup
Array
(
[grup] => 1
[prefix] => Full // option
[gz] => 0
[optimize] => 0
[lock] => 0
[combine] => 1
)
*/
###############################################################################
@ini_set('memory_limit', '-1');
@ignore_user_abort(1);
@ini_set("magic_quotes_runtime", 0);
@ini_set('max_execution_time', 60*60*24);
header('Content-Type: text/html; charset=utf-8');
require_once('includes/pdodatabase.php');
require_once("includes/localdays.php");
$tarih = datetime;
################################################################################
if(!file_exists(BACKUPDIR)){
if (!mkdir(BACKUPDIR, 0777, true)) {
die('Failed to create folders...');
}
}
$content = 'deny from all';
$file = new SplFileObject(BACKUPDIR . '/.htaccess', "w") ;
$file->fwrite($content);
################################################################################
if(!empty($_POST['prefix'])){
$prefix=strtolower($_POST['prefix'])."-";
$tarih_prefix = ucfirst($prefix).$tarih;
}else{
$tarih_prefix = datetime;
}
################################################################################
$tables = array();
################################################################################
if($_POST['combine']=='1'){ // Tam tek dosya olarak yedekleme
$tabloadi='-Tam';
$tables = '*';
}
################################################################################
if($_POST['combine']=='2'){ // Tabloları Ayrı Ayrı yedekleme
$tables = '*';
define('SUBBACKUPDIR', './'.BACKUPDIR.'/'.$tarih_prefix ) ;
if(!file_exists(SUBBACKUPDIR)){
if (!mkdir(SUBBACKUPDIR, 0777, true)) {
die('Failed to create folders...');
}
}
$content = 'deny from all';
$file = new SplFileObject(SUBBACKUPDIR . '/.htaccess', "w") ;
$file->fwrite($content) ;
}
################################################################################
if($_POST['combine']=='3' AND $_POST['elle']=='1'){ // Tabloları elle seçme
$toplam_tablo = count($_POST['tablolar']);
if($toplam_tablo==1){
$tables = $_POST['tablolar'];
sort($tables);
$tabloadi="-".ucfirst($tables[0]);
}else{
$tables = $_POST['tablolar'];
sort($tables);
$tabloadi='-Elle';
}
}
################################################################################
if($_POST['combine']=='3' AND $_POST['elle']=='2'){ // Tabloları elle seçme
define('SUBBACKUPDIR', './'.BACKUPDIR.'/'.$tarih_prefix ) ;
if(!file_exists(SUBBACKUPDIR)){
if (!mkdir(SUBBACKUPDIR, 0777, true)) {
die('Failed to create folders...');
}
}
$content = 'deny from all';
$file = new SplFileObject(SUBBACKUPDIR . '/.htaccess', "w") ;
$file->fwrite($content) ;
$tables = $_POST['tablolar'];
sort($tables);
}
################################################################################
if($_POST['grup']=='1'){
$return=null;
$mysql_version = $PDOdatabase->query('select version()')->fetchColumn();
$mysqlcharacter = $PDOdatabase->query("SHOW VARIABLES LIKE 'character_set_connection'");
$mysql_character = $mysqlcharacter->fetchColumn(1);
$return .= "\n-- Karakter Seti: ".$mysql_character."\n";
$return .= "-- PHP Sürümü: ".phpversion()."\n";
$return .= "-- Sunucu sürümü: ".$mysql_version."\n";
$return .= "-- Anamakine: ".$_SERVER['HTTP_HOST']."\n";
$return .= '-- Üretim Zamanı: ' . strftime("%d %B %Y, %H:%M:%S", time()) . "\n";
$return .= "-- Veritabanı: " . $db_name . "\n";
$return .= "--\n";
$return .= "-- --------------------------------------------------------\n";
$return .= 'SET AUTOCOMMIT = 0 ;' ."\n" ;
$return .= 'SET FOREIGN_KEY_CHECKS=0 ;';
$return .="\n-- --------------------------------------------------------\n";
$lock_write = 'LOCK TABLES';
$lock_read = 'LOCK TABLES';
// BASE TABLE SAVE
// get all of the tables
if($tables == '*'){
$tables = array();
$result = $PDOdatabase->query("SHOW TABLES");
while($row = $result->fetch(PDO::FETCH_NUM)){
$tables[] = $row[0];
$lock_write .= ' '.$row[0].' WRITE,';
$lock_read .= ' '.$row[0].' READ,';
}
}else{
$tables = is_array($tables) ? $tables : explode(',',$tables);
foreach ($tables AS $table){
$lock_write .= ' '.$table.' WRITE,';
$lock_read .= ' '.$table.' READ,';
}
}
$tablosayisi = count($tables);
// Yedek almadan önce onarma ve optimize etme
if($_POST['optimize']=='1'){
// Remove Ending of LockList
$lock_write = rtrim($lock_write,",").';';
// Lock Tables
if ($_POST['lock']=='1')
{
$PDOdatabase->query($lock_write);
}
if($_POST['combine']=='3')
{
$tables = $_POST['tablolar'];
}
// Loop Tables
foreach ($tables AS $table){
$error = 0;
$error2 = 1;
$optimize = 1;
// Check Table
$check = $PDOdatabase->query("CHECK TABLE `$table`");
while ($status = $check->fetchAll()){
// Status
if ($status[2] == 'error')
{
if ($status[3] == 'The handler for the table doesn\'t support check/repair')
{
$optimize = 0;
}
else
{
$error = 1;
}
}
}
// Check Table Error
if ($error)
{
// Repair Table
$repair = $PDOdatabase->query("REPAIR TABLE `$table`");
// Status
if ($repair[3] != 'OK')
{
$error2 = 1;
}
else
{
$error2 = 0;
$error = 0;
}
}
// Check Optimize
if (!$error && !$error2 && $optimize)
{
// Optimize Table
$optimize = $PDOdatabase->query("OPTIMIZE TABLE `$table`");
while ($status = $optimize->fetchAll())
{
// Status
if ($status[2] == 'error')
{
$error = 1;
}
}
}
}
// Unlock Tables
if ($_POST['lock']=='1')
{
$PDOdatabase->query('UNLOCK TABLES;');
}
}//if($_POST['optimize']=='1'){
// Remove Ending of LockList
$lock_read = rtrim($lock_read,",").';';
// Lock Tables
if ($_POST['lock']=='1')
{
$PDOdatabase->query($lock_read);
}
//cycle through
$t = 0;
foreach($tables as $table){
$t++;
$sutun_sayisi = $PDOdatabase->query('SELECT * FROM '.$table);
$num_fields = $sutun_sayisi->columnCount();
$numrow = $sutun_sayisi->rowCount();
################################################################################
// Tabloları ayrı ayrı yemekleme yolu ve dosya adı
if($_POST['combine']=='2' OR $_POST['combine']=='3' AND $_POST['elle']=='2'){
$handle = fopen(SUBBACKUPDIR.'/'.trim(ucfirst($table)).'.sql','a');
// GZip için dosya yolu ve dosya adı
$dosya = SUBBACKUPDIR.'/'.trim(ucfirst($table)).'.sql';
}
// Tek dosyada yedekleme yolu ve dosya adı
if($_POST['combine']=='1' OR $_POST['combine']=='3' AND $_POST['elle']!='2'){
$handle = fopen('./'.BACKUPDIR.'/'.$tarih_prefix.$tabloadi.'.sql','a');
// GZip için dosya yolu ve dosya adı
$dosya = './'.BACKUPDIR.'/'.$tarih_prefix.$tabloadi.'.sql';
}
################################################################################
$return .= "--\n" ;
$return .= '-- Tablonun yapısı `' . $table . '`' . "\n" ;
$return .= "--\n\n";
$return .= 'DROP TABLE IF EXISTS '.$table.';';
$pstm2 = $PDOdatabase->query('SHOW CREATE TABLE '.$table);
$row2 = $pstm2->fetch(PDO::FETCH_NUM);
$ifnotexists = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $row2[1]);
$return .= "\n".$ifnotexists.";\n";
$return .= "\n--\n" ;
$return .= '-- Tablonun veri dökümü `' . $table . '`' . "\n" ;
$return .= "--\n\n" ;
for ($i = 0; $i < $num_fields; $i++) {
@set_time_limit(0);
$s = 0;
while ($row = $sutun_sayisi->fetch(PDO::FETCH_NUM)) {
$s++;
$return .= 'INSERT INTO `' . trim($table) . '` VALUES(';
for ($j = 0; $j < $num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("/\r\n/", "\\r\\n", $row[$j]);
if (isset($row[$j])) {
$return .= "'" . $row[$j] . "'";
} else {
$return .= "''";
}
if ($j < ($num_fields - 1)) {
$return .= ',';
}
}
$return .= ");\n";
// Tablo verinin dökümün altına ekler
if ( $s == ( $numrow - 0 ) ){
$return .="\n-- --------------------------------------------------------\n";
}
// Okunan veriyi dosyaya yaz
fwrite($handle, $return);
$return=null;
}// end while
// Tabloda veri yoksa tablo yapısını dosyaya yaz
$satir = $sutun_sayisi->fetch(PDO::FETCH_NUM);
if(!$satir[0]){
fwrite($handle, $return);
$return=null;
}
}//end for
// Dosyanın en sonuna ekle
if ( $t == ( $tablosayisi - 0 ) AND $tablosayisi > 1){
$return .= "\n";
$return .= 'SET FOREIGN_KEY_CHECKS = 1 ; ' . "\n" ;
$return .= 'COMMIT ; ' . "\n" ;
$return .= 'SET AUTOCOMMIT = 1 ; ' . "\n" ;
fwrite($handle, $return);
$return=null;
}
// Açılmış dosyayı kapat
if($_POST['gz']=='0'){fclose($handle);}
// Tablo Tablo yedekleri GZip ile sıkıştırır
if($_POST['gz']=='1' AND ($_POST['combine']=='2' OR @$_POST['elle']=='2')){
fclose($handle);
$input = $dosya;
$output = $input.".gz";
$basarili = file_put_contents("compress.zlib://$output", file_get_contents($input));
if($basarili){@unlink($dosya);}
}
}//foreach($tables as $table){
// Tek dosyada yedeği GZip ile sıkıştırır
if($handle AND $_POST['gz']=='1' AND ($_POST['combine']=='1' OR @$_POST['elle']=='1')){
fclose($handle);
$input = $dosya;
$output = $input.".gz";
$basarili = file_put_contents("compress.zlib://$output", file_get_contents($input));
if($basarili){@unlink($dosya);}
}
// Yedekleme sırasında Tablolar kilitlendi ise kilitleri açar
if ($_POST['lock'] == 1){
$PDOdatabase->query('UNLOCK TABLES;');
unset($lock_write,$lock_read);
}
if($handle != "" OR $basarili){
echo 'Veritabanı Başarıyla Yedeklendi.';
}else{
echo 'Veritabanı Bir Hatadan Dolayı Yedeklenemedi.';
}
}//if($_POST['grup']=='1'){
?>