Example #1
0
/**
 * Exports database to file
 *
 * @param string $file_name path to file will be created
 * @param array $dbdump_tables List of tables to be exported
 * @param bool $dbdump_schema Export database schema
 * @param bool $dbdump_data Export tatabase data
 * @param bool $log Log database export action
 * @param bool $show_progress Show or do not show process by printing ' .'
 * @param bool $move_progress_bar Move COMET progress bar or not on show progress
 * @param array $change_table_prefix Array with 2 keys (from, to) to change table prefix
 * @return bool false, if file is not accessible
 */
function db_export_to_file($file_name, $dbdump_tables, $dbdump_schema, $dbdump_data, $log = true, $show_progress = true, $move_progress_bar = true, $change_table_prefix = array())
{
    $fd = @fopen($file_name, 'w');
    if (!$fd) {
        fn_set_notification('E', __('error'), __('dump_cant_create_file'));
        return false;
    }
    if ($log) {
        // Log database backup
        fn_log_event('database', 'backup');
    }
    // set export format
    Database::query("SET @SQL_MODE = 'MYSQL323'");
    $create_statements = array();
    $insert_statements = array();
    if ($show_progress && $move_progress_bar) {
        fn_set_progress('step_scale', sizeof($dbdump_tables) * ((int) $dbdump_schema + (int) $dbdump_data));
    }
    // get status data
    $t_status = Database::getHash("SHOW TABLE STATUS", 'Name');
    foreach ($dbdump_tables as $k => $table) {
        $_table = !empty($change_table_prefix) ? str_replace($change_table_prefix['from'], $change_table_prefix['to'], $table) : $table;
        if ($dbdump_schema) {
            if ($show_progress) {
                fn_set_progress('echo', '<br />' . __('backupping_schema') . ': <b>' . $table . '</b>', $move_progress_bar);
            }
            fwrite($fd, "\nDROP TABLE IF EXISTS " . $_table . ";\n");
            $scheme = Database::getRow("SHOW CREATE TABLE {$table}");
            $_scheme = array_pop($scheme);
            if ($change_table_prefix) {
                $_scheme = str_replace($change_table_prefix['from'], $change_table_prefix['to'], $_scheme);
            }
            fwrite($fd, $_scheme . ";\n\n");
        }
        if ($dbdump_data) {
            if ($show_progress) {
                fn_set_progress('echo', '<br />' . __('backupping_data') . ': <b>' . $table . '</b>&nbsp;&nbsp;', $move_progress_bar);
            }
            $total_rows = Database::getField("SELECT COUNT(*) FROM {$table}");
            // Define iterator
            if (!empty($t_status[$table]) && $t_status[$table]['Avg_row_length'] < DB_MAX_ROW_SIZE) {
                $it = DB_ROWS_PER_PASS;
            } else {
                $it = 1;
            }
            for ($i = 0; $i < $total_rows; $i = $i + $it) {
                $table_data = Database::getArray("SELECT * FROM {$table} LIMIT {$i}, {$it}");
                foreach ($table_data as $_tdata) {
                    $_tdata = fn_add_slashes($_tdata, true);
                    $values = array();
                    foreach ($_tdata as $v) {
                        $values[] = $v !== null ? "'{$v}'" : 'NULL';
                    }
                    fwrite($fd, "INSERT INTO {$_table} (`" . implode('`, `', array_keys($_tdata)) . "`) VALUES (" . implode(', ', $values) . ");\n");
                }
                if ($show_progress) {
                    fn_echo(' .');
                }
            }
        }
    }
    fclose($fd);
    @chmod($file_name, DEFAULT_FILE_PERMISSIONS);
    return true;
}