Пример #1
0
/**
 * Functions creates dump of tables in $file.
 *
 * @param mixed $tables Array of tables
 * @param string $dir Directory for saving file with table names
 * @param string $file Dump file name  \
 * @param Logger $log
 * @return bool Boolean False on failure
 */
function fn_uc_backup_tables($tables, $dir, $file, $log)
{
    if (empty($tables)) {
        return array();
    }
    if (!is_array($tables)) {
        $tables = array($tables);
    }
    $new_license_data = fn_uc_get_new_license_data();
    if (!empty($new_license_data['new_license']) && !in_array(Registry::get('config.table_prefix') . 'settings', $tables)) {
        $tables[] = Registry::get('config.table_prefix') . 'settings';
    }
    $file_backuped_tables = "{$dir}/db_backup_tables.txt";
    $backuped_tables = is_file($file_backuped_tables) ? explode("\n", fn_get_contents($file_backuped_tables)) : array();
    foreach ($tables as $key => &$table) {
        $table = fn_check_db_prefix($table, Registry::get('config.table_prefix'));
        if (in_array($table, $backuped_tables)) {
            unset($tables[$key]);
        }
    }
    if (empty($tables)) {
        return $tables;
    }
    if (false === fn_uc_is_enough_disk_space($file, $tables)) {
        fn_set_notification('W', __('warning'), __('text_uc_no_enough_space_to_backup_database'));
        return false;
    }
    if (fn_uc_is_mysqldump_available(Registry::get('config.db_password'))) {
        $log->write('Using mysqldump', __FILE__, __LINE__);
        $command = 'mysqldump --compact --no-create-db --add-drop-table --default-character-set=utf8 --skip-comments --verbose --host=' . Registry::get('config.db_host') . ' --user='******'config.db_user') . ' --password=\'' . Registry::get('config.db_password') . '\' --databases ' . Registry::get('config.db_name') . ' --tables ' . implode(' ', $tables) . ' >> ' . $file;
        system($command, $retval);
        if (0 === $retval) {
            $backuped_tables = array_merge($backuped_tables, $tables);
            fn_put_contents($file_backuped_tables, implode("\n", $backuped_tables));
            return $tables;
        }
        $log->write('mysqldump has reported failure', __FILE__, __LINE__);
    }
    $rows_per_pass = 40;
    $max_row_size = 10000;
    $t_status = db_get_hash_array("SHOW TABLE STATUS", 'Name');
    $f = fopen($file, 'ab');
    if (!empty($f)) {
        foreach ($tables as &$table) {
            $log->write('Backing up table: ' . $table, __FILE__, __LINE__);
            fwrite($f, "\nDROP TABLE IF EXISTS " . str_replace('?:', Registry::get('config.table_prefix'), $table) . ";\n");
            if (empty($t_status[str_replace('?:', Registry::get('config.table_prefix'), $table)])) {
                // new table in upgrade, we need drop statement only
                continue;
            }
            $scheme = db_get_row("SHOW CREATE TABLE {$table}");
            fwrite($f, array_pop($scheme) . ";\n\n");
            $total_rows = db_get_field("SELECT COUNT(*) FROM {$table}");
            // Define iterator
            if ($t_status[str_replace('?:', Registry::get('config.table_prefix'), $table)]['Avg_row_length'] < $max_row_size) {
                $it = $rows_per_pass;
            } else {
                $it = 1;
            }
            fn_echo(' .');
            for ($i = 0; $i < $total_rows; $i = $i + $it) {
                $table_data = db_get_array("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($f, "INSERT INTO " . str_replace('?:', Registry::get('config.table_prefix'), $table) . " (`" . implode('`, `', array_keys($_tdata)) . "`) VALUES (" . implode(', ', $values) . ");\n");
                }
            }
            $backuped_tables[] = "{$table}";
        }
        fclose($f);
        @chmod($file, DEFAULT_FILE_PERMISSIONS);
        fn_put_contents($file_backuped_tables, implode("\n", $backuped_tables));
        return $tables;
    }
}