ACC SHELL

Path : /srv/www/vhosts/tsisystem/app/webroot/
File Upload :
Current File : //srv/www/vhosts/tsisystem/app/webroot/sql_dump_mysql.php

<?php

ini_set('error_reporting', E_ALL);
ini_set('magic_quotes_runtime', 0);
set_magic_quotes_runtime(0);

set_time_limit(300);

$file_buffer = '';

function write_data( &$str, $finish = false ) {

	global $gzfp, $file_buffer;
	$file_buffer .= $str;
	if ((strlen($file_buffer) > 1024*256) || $finish) {
		if ($gzfp) {
			gzwrite($gzfp, $file_buffer);
			$file_buffer = '';
		}
	}
}

function mydump_get_table_content(&$cn, $table)
{
    $buffer = array();

    if (! ($res = mysql_unbuffered_query("SELECT * FROM $table", $cn)))
        return NULL;

    $rows = array();
    $data = array();
    $insert_begin = "\nINSERT INTO `$table` VALUES (";

    while ($row = mysql_fetch_row($res)) {
        $tmp_data = array();
        foreach ($row as $dt) {
            if (is_null($dt)) {
                $tmp_data[] = 'NULL';
                continue;
            }

            $dt = str_replace('\\', '\\\\', $dt);
            $dt = str_replace('\'', '\\\'', $dt);

            $tmp_data[] = '\'' . $dt . '\'';
        }

        $str = join(',', $tmp_data) . ");";
        write_data($insert_begin);
        write_data($str);
    }

    $ret = "\n";
    write_data($ret);
    mysql_free_result($res);
}

function mydump_get_table_def(&$cn, $table)
{
    $res = mysql_query("show create table `$table`", $cn);
    $def = mysql_fetch_row($res);

    $drop = "\nDROP TABLE IF EXISTS `$table`;\n";
    $def[1] .= ";\n\n";
    write_data($drop);
    write_data($def[1]);
}

function mydump_get_db_content(&$cn, $db)
{
    mysql_select_db($db, $cn);

    $buffer = array();

    $res = mysql_unbuffered_query("SHOW TABLES from `$db`", $cn);

    while ($row = mysql_fetch_row($res))
        $tables[] = $row[0];

    mysql_free_result($res);

    foreach ($tables as $table) {
        printf("Begin table: %s ...\n", $table);
        flush();
        mydump_get_table_def($cn, $table);
        mydump_get_table_content($cn, $table);
    }
}



$file = 'aut.sql.gz';
$db = 'tsisystem';

if (($gzfp = gzopen($file, 'w'))) {
    printf("Begin dump %s db...\n", $db);
    $use = "# Start at: ".strftime("%d. %m. %Y %H:%M:%S")."\n";
    write_data($use); //
    $cn = mysql_connect('127.0.0.1', 'tsisystem', 'tsi1970sys');
    $utf = "SET NAMES utf8;\n";
    write_data($utf);
    mysql_query($utf, $cn);
    mydump_get_db_content($cn, $db);
    $finish = "\n# Done at: ".strftime("%d. %m. %Y %H:%M:%S")."\n";
    write_data($finish, true);
    gzclose($gzfp);
    mysql_close($cn);
 }

exit("\n-- done --\n");

?>

ACC SHELL 2018