>백엔드 개발 >PHP 튜토리얼 >PHP 백업 데이터베이스

PHP 백업 데이터베이스

WBOY
WBOY원래의
2016-08-08 09:26:161013검색

<span><?<span>php
    </span><span>/*</span><span>*
    *    
    *    @name  php备份数据库 
    *    @param string   $DbHost       连接主机
    *    @param string   $DbUser       用户名
    *    @param string   $DbPwd        连接密码
    *    @param string   $DbName       要备份的数据库
    *    @param string   $saveFileName 要保存的文件名, 默认文件保存在当前文件夹中,以日期作区分
    *    @return Null
    *    @example backupMySqlData('localhost', 'root', '123456', 'YourDbName');
    *
    </span><span>*/</span>
    <span>function</span> backupMySqlData(<span>$DbHost</span>, <span>$DbUser</span>, <span>$DbPwd</span>, <span>$DbName</span>, <span>$saveFileName</span> = ''<span>)
    {
        </span><span>header</span>("Content-type:text/html;charset=utf-8"<span>);
        </span><span>error_reporting</span>(0<span>);
        </span><span>set_time_limit</span>(0<span>);

        </span><span>echo</span> '数据备份中,请稍候......<br />'<span>;

        </span><span>$link</span> = <span>mysql_connect</span>(<span>$DbHost</span>, <span>$DbUser</span>, <span>$DbPwd</span>) or <span>die</span>('数据库连接失败: ' . <span>mysql_error</span><span>());
        </span><span>mysql_select_db</span>(<span>$DbName</span>) or <span>die</span>('数据库连接失败: ' . <span>mysql_error</span><span>());
        </span><span>mysql_query</span>('set names utf8'<span>);
        
        </span><span>//</span><span> 声明变量</span>
        <span>$isDropInfo</span>     = ''<span>;
        </span><span>$insertSQL</span>      = ''<span>;
        </span><span>$row</span>            = <span>array</span><span>();
        </span><span>$tables</span>         = <span>array</span><span>();
        </span><span>$tableStructure</span> = <span>array</span><span>();
        </span><span>$fileName</span>       = (<span>$saveFileName</span> ? <span>$saveFileName</span> : 'MySQL_data_bakeup_') . <span>date</span>('YmdHis') . '.sql'<span>;

        </span><span>//</span><span> 枚举该数据库所有的表</span>
        <span>$res</span> = <span>mysql_query</span>("SHOW TABLES FROM <span>$DbName</span>"<span>);
        </span><span>while</span> (<span>$row</span> = <span>mysql_fetch_row</span>(<span>$res</span><span>)) {

            </span><span>$tables</span>[] = <span>$row</span>[0<span>];

        }
        </span><span>mysql_free_result</span>(<span>$res</span><span>);

        </span><span>//</span><span> 枚举所有表的创建语句</span>
        <span>foreach</span> (<span>$tables</span> <span>as</span> <span>$val</span><span>) {

            </span><span>$res</span> = <span>mysql_query</span>("show create table <span>$val</span>", <span>$link</span><span>);
            </span><span>$row</span> = <span>mysql_fetch_row</span>(<span>$res</span><span>);

            </span><span>$isDropInfo</span>     = "DROP TABLE IF EXISTS `" . <span>$val</span> . "`;\r\n"<span>;
            </span><span>$tableStructure</span> = <span>$isDropInfo</span> . <span>$row</span>[1] . ";\r\n"<span>;

            </span><span>file_put_contents</span>(<span>$fileName</span>, <span>$tableStructure</span>,<span> FILE_APPEND);
            </span><span>mysql_free_result</span>(<span>$res</span><span>);
        }

        </span><span>//</span><span> 枚举所有表的INSERT语句</span>
        <span>foreach</span> (<span>$tables</span> <span>as</span> <span>$val</span><span>) {

            </span><span>$res</span> = <span>mysql_query</span>("select * from <span>$val</span>"<span>);

            </span><span>//</span><span> 没有数据的表不执行insert</span>
            <span>while</span> (<span>$row</span> = <span>mysql_fetch_row</span>(<span>$res</span><span>)) {

                </span><span>$sqlStr</span> = "INSERT INTO `".<span>$val</span>."` VALUES ("<span>;

                </span><span>foreach</span>(<span>$row</span> <span>as</span> <span>$v</span><span>){

                    </span><span>$sqlStr</span> .= "'<span>$v</span>',"<span>;

                }
                </span><span>//</span><span>去掉最后一个逗号</span>
                <span>$sqlStr</span>  = <span>substr</span>(<span>$sqlStr</span>, 0, <span>strlen</span>(<span>$sqlStr</span>) - 1<span>);
                </span><span>$sqlStr</span> .= ");\r\n"<span>;

                </span><span>file_put_contents</span>(<span>$fileName</span>, <span>$sqlStr</span>,<span> FILE_APPEND);
            }
            </span><span>mysql_free_result</span>(<span>$res</span><span>);
        }

        </span><span>echo</span> '数据备份成功!'<span>;
    }
   // 调用此方法 
    backupMySqlData(</span>'localhost', 'root', '123456', 'YouDbName'<span>);    
</span>?></span>

위 내용은 PHP 백업 데이터베이스에 대한 내용을 포함하여 소개되었으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.