Home Backend Development PHP Tutorial php mysql database backup program_PHP tutorial

php mysql database backup program_PHP tutorial

Jul 13, 2016 pm 05:05 PM
mysql php by Can backup Example supply database method of program

php mysql 数据库备份程序 /* 提供一款实例的php mysql 数据库备份程序,很好方法的可以对你的数据库进行在线实时备份,这样可以保存数据库的安全,并且他是以.sql文件保存在bakdata目录还日期生成的数据库备份文件的。

php教程 mysql教程 数据库教程备份程序
/*
提供一款实例的php mysql 数据库备份程序,很好方法的可以对你的数据库进行在线实时备份,这样可以保存数据库的安全,并且他是以.sql文件保存在bakdata目录还日期生成的数据库备份文件的。
*/

?>







数据管理



esweb系统数据管理


/*--------------界面--------------*/if(!$_post['act']){/*----------------------*/
$msgs[]="服务器备份目录为$backup";
$msgs[]="对于较大的数据表,强烈建议使用分卷备份";
$msgs[]="只有选择备份到服务器,才能使用分卷备份功能";
//show_msg($msgs);
?>

如果需要备份、还原或者优化 大量的数据 , 推荐使用"帝国备份王2010"
























esweb system data backup
Select backup method Back up all data
Back up single table data
Select target location Back up to server
Back up to local
Use volume backup
Split volume backup k (You can only use split volumes if you choose to back up to the server. Backup function)
Program description 1. For larger data tables, it is strongly recommended to use volume backup.

2. The volume backup function can only be used if you choose to back up to the server.


/*----*/else{/*--------------Main program---------------------- ------------------*/
if($_post['weizhi']=="localpc"&&$_post['fenjuan']=='yes')
{$msgs[]="The volume backup function can only be used if you choose to back up to the server";
show_msg($msgs); pageend();}
if($_post['fenjuan']=="yes"&&!$_post['filesize'])
{$msgs[]="You selected the volume backup function, but did not fill in the volume file size";
show_msg($msgs); pageend();}
if($_post['weizhi']=="server"&&!writeable($backup))
{$msgs[]="The backup file storage directory '$backup' is not writable, please modify the directory attributes";
show_msg($msgs); pageend();}

/*----------Back up all tables-------------*/if($_post['bfzl']=="quanbubiao"){/ *----*/
/*----Not divided into volumes*/if(!$_post['fenjuan']){/*------------------------- -------*/
if(!$tables=$d->query("show table status from $mysqldb"))
{$msgs[]="Error in reading database structure"; show_msg($msgs); pageend();}
$sql="";
while($d->nextrecord($tables))
{
$table=$d->f("name");
$sql.=make_header($table);
$d->query("select * from $table");
$num_fields=$d->nf();
while($d->nextrecord())
{$sql.=make_record($table,$num_fields);}
}
$filename=date("ymd",time())."_all.sql";
if($_post['weizhi']=="localpc") down_file($sql,$filename);
elseif($_post['weizhi']=="server")
{if(write_file($sql,$filename))
$msgs[]="All data table data backup completed, backup file '$backup/$filename' is generated";
else $msgs[]="Backup of all data tables failed";
show_msg($msgs);
pageend();
}
/*-----------------Don't end the roll*/}/*----------------------- */
/*-----------------Volume*/else{/*----------------------- --*/
if(!$_post['filesize'])
{$msgs[]="Please fill in the backup file volume size"; show_msg($msgs);pageend();}
if(!$tables=$d->query("show table status from $mysqldb"))
{$msgs[]="Error in reading database structure"; show_msg($msgs); pageend();}
$sql=""; $p=1;
$filename=date("ymd",time())."_all";
while($d->nextrecord($tables))
{
$table=$d->f("name");
$sql.=make_header($table);
$d->query("select * from $table");
$num_fields=$d->nf();
while($d->nextrecord())
{$sql.=make_record($table,$num_fields);
if(strlen($sql)>=$_post['filesize']*1000){
$filename.=("_v".$p.".sql");
If(write_file($sql,$filename))
$msgs[]="All data tables-volume-".$p."-Data backup completed, backup file '$backup/$filename'" is generated;
         else $msgs[]="Backup table-".$_post['tablename']."-Failed";
$p++;
$filename=date("ymd",time())."_all";
$sql="";}
}
}
if($sql!=""){$filename.=("_v".$p.".sql");
if(write_file($sql,$filename))
$msgs[]="All data tables-volume-".$p."-data backup completed, generate backup file '$backup/$filename'";}
show_msg($msgs);
/*---------------------End of volume*/}/*------------------ ------------------*/
/*--------End of backing up all tables*/}/*---------------------------------- ---------------*/

/*--------Backup single table------*/elseif($_post['bfzl']=="danbiao"){/*-------- ----*/
if(!$_post['tablename'])
{$msgs[]="Please select the data table to be backed up"; show_msg($msgs); pageend();}
/*--------Not divided into volumes*/if(!$_post['fenjuan']){/*--------------------- ----------*/
$sql=make_header($_post['tablename']);
$d->query("select * from ".$_post['tablename']);
$num_fields=$d->nf();
while($d->nextrecord())
{$sql.=make_record($_post['tablename'],$num_fields);}
$filename=date("ymd",time())."_".$_post['tablename'].".sql";
if($_post['weizhi']=="localpc") down_file($sql,$filename);
elseif($_post['weizhi']=="server")
{if(write_file($sql,$filename))
$msgs[]="Table-".$_post['tablename']."-Data backup is completed and the backup file '$backup/$filename'" is generated;
else $msgs[]="Backup table-".$_post['tablename']."-failed";
show_msg($msgs);
pageend();
}
/*----------------Don't end the roll*/}/*------------------------ ------------*/
/*----------------Volume*/else{/*-------------------------- ---------------*/
if(!$_post['filesize'])
{$msgs[]="Please fill in the backup file volume size"; show_msg($msgs);pageend();}
$sql=make_header($_post['tablename']); $p=1;
$filename=date("ymd",time())."_".$_post['tablename'];
$d->query("select * from ".$_post['tablename']);
$num_fields=$d->nf();
while ($d->nextrecord())
{
$sql.=make_record($_post['tablename'],$num_fields);
If(strlen($sql)>=$_post['filesize']*1000){
$filename.=("_v".$p.".sql");
If(write_file($sql,$filename))
$msgs[]="Table-".$_post['tablename']."-Volume-".$p."-Data backup is completed, and the backup file '$backup/$filename'" is generated;
        else $msgs[]="Backup table-".$_post['tablename']."-Failed";
$p++;
$filename=date("ymd",time())."_".$_post['tablename'];
$sql="";}
}
if($sql!=""){$filename.=("_v".$p.".sql");
if(write_file($sql,$filename))
$msgs[]="Table-".$_post['tablename']."-Volume-".$p."-Data backup is completed and the backup file '$backup/$filename'" is generated;}
show_msg($msgs);
/*----------End of volume*/}/*-------------------------------- ------------------*/
/*----------End of backup single table*/}/*-------------------------------- ------------------*/

/*---*/}/*-------------End of main program--------------------- ---------------------*/

function write_file($sql,$filename)
{
$re=true;
global $backup;
if(!@$fp=fopen($backup."/".$filename,"w+")) {$re=false; echo "failed to open target file";}
if(!@fwrite($fp,$sql)) {$re=false; echo "failed to write file";}
if(!@fclose($fp)) {$re=false; echo "failed to close target file";}
return $re;
}

function down_file($sql,$filename)
{
ob_end_clean();
header("content-encoding: none");
header("content-type: ".(strpos($_server['http_user_agent'], 'msie') ? 'application/octetstream' : 'application/octet-stream'));

header("content-disposition: ".(strpos($_server['http_user_agent'], 'msie') ? 'inline; ' : 'attachment; ')."filename=".$filename);

header("content-length: ".strlen($sql));
header("pragma: no-cache");

header("expires: 0");
echo $sql;
$e=ob_get_contents();
ob_end_clean();
}

function writeable($dir)
{

if(!is_dir($dir)) {
@mkdir($dir, 0777);
}

if(is_dir($dir))
{

if($fp = @fopen("$dir/test.test", 'w'))
{
@fclose($fp);
@unlink("$dir/test.test");
$writeable = 1;
}
else {
$writeable = 0;
}

}

return $writeable;

}

function make_header($table)
{global $d;
$sql="drop table if exists ".$table."n";
$d->query("show create table ".$table);
$d->nextrecord();
$tmp=preg_replace("/n/","",$d->f("create table"));
$sql.=$tmp."n";
return $sql;
}

function make_record($table,$num_fields)
{global $d;
$comma="";
$sql .= "insert into ".$table." values(";
for($i = 0; $i < $num_fields; $i++)
{$sql .= ($comma."'".mysql_escape_string($d->record[$i])."'"); $comma = ",";}
$sql .= ")n";
return $sql;
}

function show_msg($msgs)
{
$title="提示:";
echo "

";
echo "";
echo "
".$title."

    ";
    while (list($k,$v)=each($msgs))
    {
    echo "
  • ".$v."
  • ";
    }
    echo "
";
}

function pageend()
{
exit();
}
?>





class db{

var $linkid;
var $sqlid;
var $record;

function db($host="",$username="",$password="",$database="")
{
if(!$this->linkid)    @$this->linkid = mysql_connect($host, $username, $password) or die("连接服务器失败.");
@mysql_select_db($database,$this->linkid) or die("无法打开数据库");
return $this->linkid;}

function query($sql)
{if($this->sqlid=mysql_query($sql,$this->linkid)) return $this->sqlid;
else {
    $this->err_report($sql,mysql_error);
return false;}
}

function nr($sql_id="")
{if(!$sql_id) $sql_id=$this->sqlid;
return mysql_num_rows($sql_id);}

function nf($sql_id="")
{if(!$sql_id) $sql_id=$this->sqlid;
return mysql_num_fields($sql_id);}

function nextrecord($sql_id="")
{if(!$sql_id) $sql_id=$this->sqlid;
if($this->record=mysql_fetch_array($sql_id))    return $this->record;
else return false;
}

function f($name)
{
if($this->record[$name]) return $this->record[$name];
else return false;
}

function close() {mysql_close($this->linkid);}

function lock($tblname,$op="write")
{if(mysql_query("lock tables ".$tblname." ".$op)) return true; else return false;}

function unlock()
{if(mysql_query("unlock tables")) return true; else return false;}

function ar() {
      return @mysql_affected_rows($this->linkid);
    }

function i_id() {
    return mysql_insert_id();
}

function err_report($sql,$err)
{
echo "mysql查询错误
";
echo "查询语句:".$sql."
";
echo "错误信息:".$err;
}
/**************************************End of class********** *****************/


global $mysqlhost, $mysqluser, $mysqlpwd, $mysqldb, $backup;
$mysqlhost = $mydbhost;          //host name
$mysqluser = $mydbuser;          //login name
$mysqlpwd = $mydbpw;             //password
$mysqldb = $mydbname;            //name of database

$d=new db($mysqlhost,$mysqluser,$mysqlpwd,$mysqldb);
$d->query("set names 'utf8'");

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/630798.htmlTechArticlephp mysql 数据库备份程序/*提供一款实例的php mysql 数据库备份程序,很好方法的可以对你的数据库进行在线实时备份,这样可以保存数据库的...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Redis: Understanding Its Architecture and Purpose Redis: Understanding Its Architecture and Purpose Apr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

MongoDB's Future: The State of the Database MongoDB's Future: The State of the Database Apr 25, 2025 am 12:21 AM

MongoDB's future is full of possibilities: 1. The development of cloud-native databases, 2. The fields of artificial intelligence and big data are focused, 3. The improvement of security and compliance. MongoDB continues to advance and make breakthroughs in technological innovation, market position and future development direction.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles