weblog, content management & redaktion
RSS Feed für `PHP` Seite drucken

Simple MySQL Backup via PHP

Backup Function:

<?php

function &backup_tables($host, $user, $pass, $name, $tables = '*'){
  $data = "\n/*---------------------------------------------------------------".
          "\n  SQL DB BACKUP ".date("d.m.Y H:i")." ".
          "\n  HOST: {$host}".
          "\n  DATABASE: {$name}".
          "\n  TABLES: {$tables}".
          "\n  ---------------------------------------------------------------*/\n";
  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);
  mysql_query( "SET NAMES `utf8` COLLATE `utf8_general_ci`" , $link ); // Unicode
 
  if($tables == '*'){ //get all of the tables
    $tables = array();
    $result = mysql_query("SHOW TABLES");
    while($row = mysql_fetch_row($result)){
      $tables[] = $row[0];
    }
  }else{
    $tables = is_array($tables) ? $tables : explode(',',$tables);
  }

  foreach($tables as $table){
    $data.= "\n/*---------------------------------------------------------------".
            "\n  TABLE: `{$table}`".
            "\n  ---------------------------------------------------------------*/\n";           
    $data.= "DROP TABLE IF EXISTS `{$table}`;\n";
    $res = mysql_query("SHOW CREATE TABLE `{$table}`", $link);
    $row = mysql_fetch_row($res);
    $data.= $row[1].";\n";
    
    $result = mysql_query("SELECT * FROM `{$table}`", $link);
    $num_rows = mysql_num_rows($result);    
    
    if($num_rows>0){
      $vals = Array(); $z=0;
      for($i=0; $i<$num_rows; $i++){
        $items = mysql_fetch_row($result);
        $vals[$z]="(";
        for($j=0; $j<count($items); $j++){
          if (isset($items[$j])) { $vals[$z].= "'".mysql_real_escape_string( $items[$j], $link )."'"; } else { $vals[$z].= "NULL"; }
          if ($j<(count($items)-1)){ $vals[$z].= ","; }
        }
        $vals[$z].= ")"; $z++;
      }
      $data.= "INSERT INTO `{$table}` VALUES ";      
      $data .= "  ".implode(";\nINSERT INTO `{$table}` VALUES ", $vals).";\n";
    }
  }
  mysql_close( $link );
  return $data;
}

?>

 

Restore Function:

<?php

function restor_backup( $backupFilePath, $host, $user, $pass, $name ){
  if(file_exists("$backupFilePath")){
    // now lets restore db ;-)
    $con = @mysql_connect( $host, $user, $pass );
    mysql_query("SET NAMES 'utf8'");
    
    mysql_select_db($name, $con);
    $sql = implode(file("$backupFilePath"));
    
    $erc=0;
    $t = mb_split(";[ ]*[\r]*[\n]",$sql);
    foreach ($t as $q)
    if(trim($q)!=""){
      if(mysql_query($q,$con)){
        echo ".";
      }else{
        $erc++;
        echo "Error :". mysql_error()." <pre>".htmlspecialchars( $q )."</pre> ";
      }
    }
    mysql_close($con);  
    if($erc==0) echo "Wiederherstellung erfolgreich abgeschlossen!";
    else echo "Leider sind Fehler aufgetreten!";
  }else{
    echo "File $backupFilePath wurde nicht gefunden!";
  }  
}

?>

How To Use:

 

<?php

// create backup
//////////////////////////////////////

$backup_file = 'db-backup-'.time().'.sql';

// get backup
$mybackup = backup_tables("myhost","mydbuser","mydbpasswd","mydatabase","*");

// save to file
$handle = fopen($backup_file,'w+');
fwrite($handle,$mybackup);
fclose($handle);


// restore backup
//////////////////////////////////////

restor_backup( $backup_file, "myhost","mydbuser","mydbpasswd","mydatabase" );

?>

 

Domain Name (Clean)

  <?php  

// get host name from URL
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html",
$matches);

$host = $matches[2];
 
// get last two segments of host name
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);

echo "domain name is: {$matches[0]}\n";
 
/* Output is php.net */
 
?>

tags: get, extract, domain, cleardomain, url, part