السلام عليكم و رحمة الله و تعالى و بركاته
اليوم و أنا أبحث في ملفاتي و جدت كلاس النسخ الاحتياطي فأحببت أن أشارككم به
الكود
<?php
class backup {
##########################################
##########################################
## All rights reseved for pmoki ##
## This a free software ##
## You can modify ##
## Please don't remove copyrights ##
## For contact : ##
## Hotmail: pmoki@live.fr ##
## Gmail: pmoki91@gmail.com ##
## Skype: pmoki1991 ##
##########################################
##########################################
private $host,$user,$pass,$base,$timelimit,$tablelist,$is_tables,$create_is_table,$creat_table,$run_creat,$query_for_tables,$num_fields,$handle,$data_fields,$data,$metod;
function __construct () {
$this->base="ip"; // database select
$this->host="localhost"; // host
$this->user="root"; // user
$this->pass="root"; // pass
mysql_connect($this->host,$this->user,$this->pass) or die(mysql_error());
mysql_select_db($this->base) or die(mysql_error());
}
function dump () {
$this->metod = $_GET['method'];
$this->timelimit = 300; // you can modify caution time must be in seconds
set_time_limit($this->timelimit); // set time limit for huge DB
if (empty($this->metod)) {
$this->handle = fopen("dump_db_db.sql","a+"); // open file to write
$this->tablelist = mysql_query("show tables"); // show tables
while ($this->is_table = mysql_fetch_array($this->tablelist)) { // get all tables
$this->create_is_table = mysql_query("show create table ".$this->is_table[0]); // show create table
while ($this->create_table = mysql_fetch_array($this->create_is_table)) { // loop for get all creating form
$this->run_creat = $this->create_table['Create Table']; // get creat table form
fwrite($this->handle,$this->run_creat);
$this->query_for_tables = mysql_query("select * from ".$this->is_table[0]);
$this->num_fields = mysql_num_fields($this->query_for_tables); // number of fields
while ( $this->data_fields = mysql_fetch_array($this->query_for_tables)) { // loop for get all the data on the table
$this->data = "insert into ".$this->is_table[0]." values (";
fwrite($this->handle,$this->data);
for ($i = 0 ; $i <$this->num_fields ; $i++) { // loop for print all fields data
if ( $i < $this->num_fields-1) {
$k = "'".$this->data_fields[$i]."'".",";
fwrite($this->handle,$k);
}
if ( $i == $this->num_fields-1) {
$b = "'".$this->data_fields[$i]."'";
fwrite($this->handle,$b);
}
}
$ist = ');';
fwrite($this->handle,$ist);
}
mysql_free_result($this->query_for_tables);
}
mysql_free_result($this->create_is_table);
}
mysql_free_result($this->tablelist);
fclose($this->handle);
if (file_exists("dump_db_db.sql")) {
echo 'dump succes; downloading will start .... ';
header( "refresh:2;url=".$_SERVER['PHP_SELF']."?method=download" );
}
mysql_close();
}elseif($this->metod == "download"){
// this download code by : Bilal El Moussaoui
ini_set("memory_limit", "100M");
$backup = file_get_contents('dump_db_db.sql');
header("Content-length: " . strlen($backup));
header("Content-type: application/x-sql");
header("Content-Disposition: attachment; filename=dump_db_db.sql");
echo $backup;
}
unset($this->base,$this->host,$this->user,$this->pass,$this->metod,$this->timelimit,$this->handle,$this->is_table,$this->create_table,$this->run_creat,$this->num_fields,$this->data_fields,$this->data,$i,$k,$b,$ist,$backup);
}
}
?>