ACC SHELL
<?php
/**
* TF_DBBackup class
*
* @return SQL file
* @example $dbb = new TF_DBBackup($config->db->params->toArray());
* $dbb->dump();
* @author Tomáš Fejfar
* @package TF
*/
class TF_DBBackup
{
private $_host = null;
private $_user = null;
private $_password = null;
private $_database = null;
private $_dumpData = true;
private $_dropTable = true;
private $_createTable = true;
public function __construct ($config = array(), $dumpData = true, $dropTable = true, $createTable = true)
{
$this->_host = $config["host"];
$this->_user = $config["username"];
$this->_password = $config["password"];
$this->_database = $config["dbname"];
$this->_dumpData = $dumpData;
$this->_dropTable = $dropTable;
$this->_createTable = $createTable;
mysql_connect($this->_host, $this->_user, $this->_password);
mysql_select_db($this->_database);
mysql_query("SET NAMES utf8");
}
public function dump ()
{
//ob_start("ob_gzhandler");
header('Content-type: text/plain; charset=UTF-8');
header(
'Content-Disposition: attachment; filename="' . $this->_database . "_" . date('Y-m-d') . '.sql"');
echo "/*USE DbBackup */\n";
$this->_mysqldump();
//header("Content-Length: ".ob_get_length());
//ob_end_flush();
}
public function dumpCsv ($table)
{
ob_start("ob_gzhandler");
header('Content-type: text/comma-separated-values; charset=UTF-8');
header(
'Content-Disposition: attachment; filename="' . $this->_host . "_" . $this->_database . "_" . $this->_table .
"_" . date('YmdHis') . '.csv"');
//header('Content-type: text/plain');
_mysqldump_csv($table);
header("Content-Length: " . ob_get_length());
ob_end_flush();
}
private function _mysqldump_csv ($table)
{
$delimiter = ",";
if (isset($_REQUEST['csv_delimiter']))
$delimiter = $_REQUEST['csv_delimiter'];
if ('Tab' == $delimiter)
$delimiter = "\t";
$sql = "select * from `$table`;";
$result = mysql_query($sql);
if ($result) {
$num_rows = mysql_num_rows($result);
$num_fields = mysql_num_fields($result);
$i = 0;
while ($i < $num_fields) {
$meta = mysql_fetch_field($result, $i);
echo ($meta->name);
if ($i < $num_fields - 1)
echo "$delimiter";
$i ++;
}
echo "\n";
if ($num_rows > 0) {
while ($row = mysql_fetch_row($result)) {
for ($i = 0; $i < $num_fields; $i ++) {
echo mysql_real_escape_string($row[$i]);
if ($i < $num_fields - 1)
echo "$delimiter";
}
echo "\n";
}
}
}
mysql_free_result($result);
}
private function _mysqldump ()
{
$sql = "show tables;";
$result = mysql_query($sql);
if ($result) {
while ($row = mysql_fetch_row($result)) {
$this->_mysqldump_table_structure($row[0]);
if (($this->_dumpData)) {
$this->_mysqldump_table_data($row[0]);
}
}
} else {
echo "/* no tables in $this->_database */\n";
}
mysql_free_result($result);
}
function _mysqldump_table_structure ($table)
{
echo "/* Table structure for table `$table` */\n";
if ($this->_dropTable) {
echo "DROP TABLE IF EXISTS `$table`;\n\n";
}
if ($this->_createTable) {
$sql = "show create table `$table`; ";
$result = mysql_query($sql);
if ($result) {
if ($row = mysql_fetch_assoc($result)) {
echo $row['Create Table'] . ";\n\n";
}
}
mysql_free_result($result);
}
}
function _mysqldump_table_data ($table)
{
$sql = "select * from `$table`;";
$result = mysql_query($sql);
if ($result) {
$num_rows = mysql_num_rows($result);
$num_fields = mysql_num_fields($result);
if ($num_rows > 0) {
echo "/* dumping data for table `$table` */\n";
$field_type = array();
$i = 0;
while ($i < $num_fields) {
$meta = mysql_fetch_field($result, $i);
array_push($field_type, $meta->type);
$i ++;
}
//print_r( $field_type);
echo "insert into `$table` values\n";
$index = 0;
while ($row = mysql_fetch_row($result)) {
echo "(";
for ($i = 0; $i < $num_fields; $i ++) {
if (is_null($row[$i]))
echo "null";
else {
switch ($field_type[$i]) {
case 'int':
echo $row[$i];
break;
case 'string':
case 'blob':
default:
echo "'" .
mysql_real_escape_string(
$row[$i]) .
"'";
}
}
if ($i < $num_fields -
1)
echo ",";
}
echo ")";
if ($index < $num_rows - 1)
echo ",";
else
echo ";";
echo "\n";
$index ++;
}
}
}
mysql_free_result($result);
echo "\n";
}
}
ACC SHELL 2018