Beispiel #1
0
function export_db($is_backup = false)
{
    global $db, $db_host, $db_user, $db_pass, $db_name, $db_port;
    $stime = microtime(true);
    if ($is_backup) {
        $output_file = str_replace('\\', '/', getcwd()) . "/backup/" . date("Y-m-d_H-i-s") . ".php";
    } else {
        $changes = get_changes();
        $i = 0;
        while (true) {
            $i += 1;
            $file_name = date("Y-m-d") . "-" . sprintf("%02d", $i);
            $ver = (int) str_replace('-', '', $file_name);
            if (array_key_exists($ver, $changes) == false) {
                break;
            }
        }
        $output_file = str_replace('\\', '/', getcwd()) . "/changes/" . $file_name . ".dump.php";
    }
    $file = fopen($output_file, 'c');
    $tables = get_tables($db_host, $db_user, $db_pass, $db_name, $db_port);
    $max_length = get_max_length($tables);
    fwrite($file, "<?php\n\n");
    fwrite($file, "echo \"\\n\\n\";\n");
    fwrite($file, "\n            execute(\"\n            /*!40101 SET NAMES utf8 */;\n\n            /*!40101 SET SQL_MODE=''*/;\n\n            /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n            /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n            /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n            /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n            \");\n        ");
    foreach ($tables as $table) {
        if ($table == "db_version") {
            continue;
        }
        if ($is_backup) {
            echo "backup ";
        } else {
            echo "dump ";
        }
        echo $table . " ";
        $dots = generate_char($max_length, strlen($table), '.');
        echo $dots . "......... ";
        $sql = get_create_table_sql($db, $table);
        fwrite($file, 'if ($db_version == 0)' . "\n{\n");
        fwrite($file, "    echo \"    " . $table . " " . $dots . "......... \";\n\n");
        fwrite($file, "    execute(\"\n");
        fwrite($file, $sql);
        fwrite($file, "\n\");\n\n");
        fwrite($file, "    echo \"[created]\\n\"; \n");
        fwrite($file, "}\n\n");
        if ($is_backup == false && strpos($table, "player") === 0) {
            echo "[ignore]\n";
            continue;
        }
        $fields = get_table_fields($db, $table);
        $sql = get_insert_into_sql($db, $table, $fields);
        fwrite($file, "echo \"    " . $table . " " . $dots . "......... \";\n\n");
        fwrite($file, "execute(\"DELETE FROM `" . $table . "`\");\n\n");
        if ($sql != "") {
            fwrite($file, "execute(\"\n");
            fwrite($file, $sql);
            fwrite($file, "\");\n\n");
        }
        fwrite($file, "echo \"[loaded]\\n\"; \n");
        fwrite($file, "\n");
        echo "[done]\n";
    }
    fwrite($file, "\n            execute(\"\n            /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n            /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n            /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n            /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n            \");\n        ");
    fwrite($file, "echo \"\\n\";\n");
    fwrite($file, "?>\n");
    fclose($file);
    if ($is_backup == false) {
        $sql = "UPDATE `db_version` SET `version` = " . $ver . ", `change_time` = UNIX_TIMESTAMP()";
        if ($db->query($sql) === FALSE) {
            die("can't update db_version");
        }
    }
    $etime = microtime(true);
    echo "\ndatabase ";
    if ($is_backup) {
        echo "backup ";
    } else {
        echo "dump ";
    }
    echo "complete in " . round($etime - $stime, 2) . "s\n";
}
Beispiel #2
0
function create_tables($db1, $tables1, $tables2)
{
    global $options;
    $sql = '';
    $table_names = array_diff(array_keys($tables1), array_keys($tables2));
    foreach ($table_names as $t) {
        $sql .= get_create_table_sql($t, $db1) . ";\n\n";
    }
    fputs($options->ofh, $sql);
}