首页 > 代码库 > 备份,恢复数据库

备份,恢复数据库

<?php
class dbBackup {
    public $host=‘127.0.0.1‘;    //数据库地址
    public $user=‘root‘;    //登录名
    public $pwd=‘root‘;    //密码
    public $database;    //数据库名
    public $charset=‘utf8‘;    //数据库连接编码:mysql_set_charset
    //连接数据库
    function db() {
        $con = mysql_connect($this->host,$this->user,$this->pwd);
        if (!$con){
            die(‘Could not connect‘);
        }

        $db_selected = mysql_select_db($this->database, $con);
        if (!$db_selected) {
            die(‘Can\‘t use select db‘);
        }

        mysql_set_charset($this->charset);//设置编码

        return $con;
    }
    //所有的表的数组
    function tblist() {
        $list=array();

        $rs=mysql_query("SHOW TABLES FROM $this->database");
        while ($temp=mysql_fetch_row($rs)) {
            $list[]=$temp[0];
        }

        return $list;
    }
    //查询所有表的表结构
    function sqlcreate() {
        $sql=‘‘;

        $tb=$this->tblist();
        foreach ($tb as $v) {
            $rs=mysql_query("SHOW CREATE TABLE $v");
            $temp=mysql_fetch_row($rs);
            $sql.="-- 表的结构:{$temp[0]} --\r\n";
            $sql.="{$temp[1]}";
            $sql.=";-- <xjx> --\r\n\r\n";
        }
        return $sql;
    }
    //插入的方法
    function sqlinsert() {
        $sql=‘‘;

        $tb=$this->tblist();
        foreach ($tb as $v) {
            $rs=mysql_query("SELECT * FROM $v");
            if (!mysql_num_rows($rs)) {//无数据返回
                continue;
            }
            $sql.="-- 表的数据:$v --\r\n";
            $sql.="INSERT INTO `$v` VALUES\r\n";
            while ($temp=mysql_fetch_row($rs)) {
                $sql.=‘(‘;
                foreach ($temp as $v2) {
                    if ($v2===null) {
                        $sql.="NULL,";
                    }
                    else {
                        $v2=mysql_real_escape_string($v2);
                        $sql.="‘$v2‘,";
                    }
                }
                $sql=mb_substr($sql, 0, -1);
                $sql.="),\r\n";
            }
            $sql=mb_substr($sql, 0, -3);
            $sql.=";-- <xjx> --\r\n\r\n";
        }

        return $sql;
    }
    //备份操作
    function beifen($filename) {
        $this->db();    //连接数据库

        $sql=$this->sqlcreate();
        $sql2=$this->sqlinsert();
        $data=http://www.mamicode.com/$sql.$sql2;

        return file_put_contents($filename, $data);
    }
    //还原操作
    function huanyuan($filename) {
        $this->db();    //连接数据库

        //删除数据表
        $list=$this->tblist();
        $tb=‘‘;
        foreach ($list as $v) {
            $tb.="`$v`,";
        }
        $tb=mb_substr($tb, 0, -1);
        if ($tb) {
            $rs=mysql_query("DROP TABLE $tb");
            if ($rs===false) {
                return false;
            }
        }

        //执行SQL
        $str=file_get_contents($filename);
        $arr=explode(‘-- <xjx> --‘, $str);
        array_pop($arr);

        foreach ($arr as $v) {
            $rs=mysql_query($v);
            if ($rs===false) {
                return false;
            }
        }

        return true;
    }
}
/*-------------------------------调用--------------------------------------*/


<?php
include("./test2.php");
$x=new dbBackup();
$x->database=‘login‘;
//备份
//$rs=$x->beifen(‘db.sql‘);
/技术分享ar_dump($rs);
//还原
$rs=$x->huanyuan(‘db.sql‘);
var_dump($rs);









































备份,恢复数据库