예제 #1
0
function mysql_bak_out($tabledb)
{
    global $db, $rowsnum, $fileNUM, $bak_path, $baksize, $tableid, $page;
    //此page指的是每个表数据大的时候.需要多次跳转页面读取
    if (!$page) {
        $page = 1;
    }
    $min = ($page - 1) * $rowsnum;
    $tableid = intval($tableid);
    if ($show = mysql_get_table_value($tabledb[$tableid], $min, $rowsnum)) {
        //表中有数据,还要继续读取
        $page++;
    } else {
        //表中无数据的情况,继续下一个表
        $page = 0;
        $tableid++;
    }
    //分卷是从0开始的
    $fileNUM = intval($fileNUM);
    $filename = "{$fileNUM}.sql";
    $show && write_file("{$bak_path}/{$filename}", $show, 'a+');
    //对文件做精确大小分卷处理
    $show && cksize("{$bak_path}/{$filename}", $baksize);
    //如果还存在表时.继续,否则结束
    if ($tabledb[$tableid]) {
        //不断变化的变量有 $page,$tableid,$fileNUM
        return true;
    } else {
        @unlink(ROOT_PATH . "cache/bak_mysql.txt");
        return false;
    }
}
예제 #2
0
파일: mysql.php 프로젝트: GHubgenius/qbbj
function bak_out($tabledb)
{
    global $db, $pre, $rowsnum, $tableid, $page, $timestamp, $step, $rand_dir, $lfj, $baksize;
    //还没有随机生成目录之前
    if (!$rand_dir) {
        /*特地处理有些服务器不能创建目录的情况,此时必须手工创建mysql目录*/
        if (file_exists(ROOT_PATH . "cache/mysql_bak/mysql")) {
            if (!is_writable(ROOT_PATH . "cache/mysql_bak/mysql")) {
                showmsg(ROOT_PATH . "cache/mysql_bak/mysql目录不可写,请改属性为0777");
            }
            $rand_dir = "mysql";
            $d = opendir(ROOT_PATH . "cache/mysql_bak/mysql/");
            while ($f = readdir($d)) {
                if (eregi("\\.sql\$", $f)) {
                    unlink(ROOT_PATH . "cache/mysql_bak/mysql/{$f}");
                }
            }
            write_file(ROOT_PATH . "cache/mysql_bak/mysql/index.php", str_replace('<?php die();', '<?php', read_file('mysql_into.php')));
            $show = create_table_all($tabledb);
            //备份数据表结构
            //$db->query("TRUNCATE TABLE {$pre}bak");
            //bak_dir('../data/');		//备份缓存
        } else {
            $rand_dir = date("Y-m-d.", time()) . strtolower(rands(3));
            $show = create_table_all($tabledb);
            //备份数据表结构
            if (!file_exists(ROOT_PATH . "cache/mysql_bak")) {
                if (!@mkdir(ROOT_PATH . "cache/mysql_bak", 0777)) {
                    showmsg(ROOT_PATH . "cache/mysql_bak目录不能创建");
                }
            }
            if (!@mkdir(ROOT_PATH . "cache/mysql_bak/{$rand_dir}", 0777)) {
                showmsg(ROOT_PATH . "cache/mysql_bak/{$rand_dir},目录不可写,请改属性为0777");
            }
            //复制一个自动还原的文件到SQL目录.方便日后还原
            write_file(ROOT_PATH . "cache/mysql_bak/{$rand_dir}/index.php", str_replace('<?php die();', '<?php', read_file('mysql_into.php')));
            //$db->query("TRUNCATE TABLE {$pre}bak");
            //bak_dir('../data/');		//备份缓存
        }
    }
    !$rowsnum && ($rowsnum = 500);
    //每次读取多少条数据
    //此page指的是每个表大的时候.需要多次跳转页面读取
    if (!$page) {
        $page = 1;
    }
    $min = ($page - 1) * $rowsnum;
    $tableid = intval($tableid);
    //$show.=$tablerows=bak_table($tabledb[$tableid],$min,$rowsnum);
    //当前表能取到数据时,继续此表下一页取数据,否则从下一个表的0开始
    if ($tablerows = bak_table($tabledb[$tableid], $min, $rowsnum)) {
        $show .= $tablerows;
        unset($tablerows);
        //释放内存
        $page++;
    } else {
        $page = 0;
        $tableid++;
    }
    //分卷是从0开始的
    $step = intval($step);
    $filename = "{$step}.sql";
    write_file(ROOT_PATH . "cache/mysql_bak/" . $rand_dir . "/" . $filename, $show, 'a+');
    //如果不指定每卷大小.将默认为1M
    $baksize = $baksize ? $baksize : 1024;
    //对文件做精确大小分卷处理
    $step = cksize(ROOT_PATH . "cache/mysql_bak/" . $rand_dir . "/" . $filename, $step, 1024 * $baksize);
    //如果还存在表时.继续,否则结束
    if ($tabledb[$tableid]) {
        foreach ($tabledb as $value) {
            $Table .= "{$value}|";
        }
        //记录下来.防止中途备份失败
        write_file(ROOT_PATH . "cache/bak_mysql.txt", "index.php?lfj={$lfj}&action=out&page={$page}&rowsnum={$rowsnum}&tableid={$tableid}&rand_dir={$rand_dir}&step={$step}&tabledbreto={$Table}&baksize={$baksize}");
        echo "<CENTER>已备份 <font color=red>{$step}</font> 卷, 进度条 <font color=blue>{$page}</font> 当前正在备份数据库 <font color=red>{$tabledb[$tableid]}</font></CENTER>";
        print <<<EOT
<form name="form1" method="post" action="index.php?lfj={$lfj}&action=out&page={$page}&rowsnum={$rowsnum}&tableid={$tableid}&rand_dir={$rand_dir}&step={$step}&baksize={$baksize}">
  <input type="hidden" name="tabledbreto" value="{$Table}">
</form>
<SCRIPT LANGUAGE="JavaScript">
<!--
function autosub(){
\tdocument.form1.submit();
}
autosub();
//-->
</SCRIPT>
EOT;
        //echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=index.php?lfj=$lfj&action=out&page=$page&rowsnum=$rowsnum&tableid=$tableid&rand_dir=$rand_dir&step=$step&tabledbreto=$Table&baksize=$baksize'>";
        exit;
    } else {
        $dir = opendir(ROOT_PATH . "cache/mysql_bak/{$rand_dir}");
        while ($file = readdir($dir)) {
            if (eregi('.sql$', $file)) {
                $totalsize += $sqlfilesize = @filesize(ROOT_PATH . "cache/mysql_bak/{$rand_dir}/{$file}");
                $rs[sqlsize][] = number_format($sqlfilesize / 1024, 3);
            }
        }
        $totalsize = number_format($totalsize / 1048576, 3);
        @unlink(ROOT_PATH . "cache/bak_mysql.txt");
        $rs[totalsize] = $totalsize;
        $rs[timedir] = $rand_dir;
        if (!@is_writable(ROOT_PATH . "cache/mysql_bak/{$rand_dir}/0.sql")) {
            showmsg("备份失败,请在cache/mysql_bak/目录下创建一个目录mysql然后改其属性为0777,如果此目录已存在,请删除他,重新创建,并改属性为0777");
        }
        return $rs;
    }
}