我可以在我的网站上创建一个按钮来备份我的数据库吗?

时间:2012-07-17 14:08:25

标签: php mysql sql database backup

我想在我的内容管理系统中设置一个按钮,我可以为特定页面上显示的表保存备份sql文件。我只是希望它能够将SQL文件下载到我的计算机上。我还想知道是否可以做同样的事情,但导出为表格数据的excel文件。

如果只是对整个数据库进行备份更容易,我会满足于此,我只需要知道它是否可能以及我正在寻找什么来构建脚本。

希望这是有道理的,我很难知道我需要寻找什么,因为我不知道它是否真的可能。我在php编码。

提前致谢。

1 个答案:

答案 0 :(得分:3)

这是一个小页面,您可以:

  • 检查您想要的表格
  • 下载
  • 或将它们写在磁盘(服务器端)

但是

如果你关心安全问题,你不应该这样做。

如果您想进行此类操作,我建议您改为使用phpMyAdmin

如果您只想在不下载的情况下进行自动备份,最好使用cronmysqldump命令。 Cron在许多服务器上配置为每晚进行备份。

看看这个:

http://www.comentum.com/mysqldump-cron.html

http://www.noupe.com/how-tos/10-ways-to-automatically-manually-backup-mysql-database.html

好。这是PHP页面:

<?

$db_base="set_your_base";
$db=mysql_connect("xxxx","xxxx","xxxx")
or die ("unable to connect");
$select_base=mysql_selectdb($db_base,$db);


function get_para_value($name, $default_value) {
    global $HTTP_GET_VARS, $HTTP_POST_VARS, $_GET, $_POST;
    if( isset($_GET[$name])) {
        return $_GET[$name];
    }
    if( isset($_POST[$name])) {
        return $_POST[$name];
    }
    return $default_value;
}


$action=get_para_value('action', 'ask');

if ($action=='ask') {
    ?>
    <html>
    <body>
    <SCRIPT LANGUAGE="JavaScript">
    var checkflag = "false";
    function check(form) {
        var objCheckBoxes = form.elements;
        if (checkflag == "false") {
            for (i = 0; i < objCheckBoxes.length; i++) {
                objCheckBoxes[i].checked = true;
            }
            checkflag = "true";
            return "Uncheck all";
        } else {
            for (i = 0; i < objCheckBoxes.length; i++) {
                objCheckBoxes[i].checked = false;
            }
            checkflag = "false";
            return "Check all";
        }
    }
    </script>

    <center>

    <form name=dump_form action=dump_dys.php method="post" enctype="multipart/form-data" accept-charset="utf-8">
    <br>
    Choose the tables you want:
    <br>
    <br>
    <input type=button value="Check all" onClick="this.value=check(this.form)">
    <br>
    <br>
    <table>
    <?php
    $sql_tables = "SHOW TABLES";
    $req_tables = mysql_query($sql_tables);
    while (list($table) = mysql_fetch_row($req_tables)) {
        echo "<tr><td><input type=checkbox name=".$table."> ".$table."</td></tr>";
    }
    ?>
    </table>
    <input type=hidden name=action value=dump>
    <br>
    <input type=button value="Download" onClick="this.form.action.value='dump';this.form.submit();">
    &nbsp; <input type=button value="Write on server" onClick="this.form.action.value='dump_to_disk';this.form.submit();">
    </form>
    </center>
    </body>
    </html>
    <?php
} else if ($action=='dump') {
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment;filename=\"$db_base.sql\"");
    header("Content-Transfer-Encoding: binary");
    echo "--\n";
    echo "-- Dump of database $db_base\n";
    echo "--\n";
    $sql_tables = "SHOW TABLES";
    $req_tables = mysql_query($sql_tables);
    while (list($table) = mysql_fetch_row($req_tables)) {
        if (get_para_value($table,'no')=='no') continue;
        echo "\n--\n-- Table $table\n\n";
        echo "DROP TABLE IF EXISTS $table;\n";
        $sql_create_table = "SHOW CREATE TABLE $table";
        $req_create_table = mysql_query($sql_create_table);
        $create_table = mysql_fetch_array($req_create_table);
        echo $create_table[1].";\n";
        echo "\n--\n-- Filling de $table\n\n";
        $sql_fill_table = "SELECT * FROM $table";
        $req_fill_table = mysql_query($sql_fill_table);
        while ($row = mysql_fetch_assoc($req_fill_table)) {
        $line_insert = "INSERT INTO $table (";
        $l_value = ") VALUES (";
        foreach ($row as $field => $value) {
            $line_insert .= "`$field`, ";
            $l_value .= "'".mysql_real_escape_string($value)."', ";
        }
        $line_insert = substr($line_insert, 0, -2);
        $l_value = substr($l_value, 0, -2);
        echo $line_insert.$l_value.");\n";
       }
    }

    echo "--\n";
    echo "-- Dump of base $db_base finished )\n";
    echo "--\n";
} else if ($action=='dump_to_disk') {
    $file = fopen($db_base.".sql", 'w') or die('bug!');
    fwrite($file, "--\n");
    fwrite($file,  "-- Dump of the base $db_base\n");
    fwrite($file,  "--\n");
    $sql_tables = "SHOW TABLES";
    $req_tables = mysql_query($sql_tables);
    while (list($table) = mysql_fetch_row($req_tables)) {
        if (get_para_value($table,'no')=='no') continue;
        fwrite($file,  "\n--\n-- Table $table\n\n");
        fwrite($file,  "DROP TABLE IF EXISTS $table;\n");
        $sql_create_table = "SHOW CREATE TABLE $table";
        $req_create_table = mysql_query($sql_create_table);
        $create_table = mysql_fetch_array($req_create_table);
        fwrite($file,  $create_table[1].";\n");
        fwrite($file,  "\n--\n-- Remplissage de $table\n\n");
        $sql_fill_table = "SELECT * FROM $table";
        $req_fill_table = mysql_query($sql_fill_table);
        while ($row = mysql_fetch_assoc($req_fill_table)) {
        $line_insert = "INSERT INTO $table (";
        $l_value = ") VALUES (";
        foreach ($row as $field => $value) {
            $line_insert .= "`$field`, ";
            $l_value .= "'".mysql_real_escape_string($value)."', ";
        }
        $line_insert = substr($line_insert, 0, -2);
        $l_value = substr($l_value, 0, -2);
        fwrite($file,  $line_insert.$l_value.");\n");
       }
    }
    fwrite($file, "--\n");
    fwrite($file, "-- Dump of base $db_base finished (not even a crash!)\n");
    fwrite($file, "--\n");
    fclose($file);
    echo "dump done";
}
?>