system: Linux mars.sprixweb.com 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
<?php
class Lib_BackupDB
{
/**
* Stores function type's name
*
* @var string $type
*/
private $type;
/**
* Flag for error
*
* @var boolean $error
*/
private $error;
/**
* Stores error numbers and description
*
* @var array $debug
*/
public $debug = array();
/**
* Stores the output
*
* @var bool $result
*/
public $result;
/**
* user name
*
* @var string $userName
*/
private $userName;
/**
* password
*
* @var string $password
*/
private $password;
/**
* server name
*
* @var string $serverName
*/
private $serverName;
/**
* database name
*
* @var string $dbName
*/
private $dbName;
/**
* backup path
*
* @var string $backupPath
*/
private $backupPath;
/**
* table name
*
* @var string $tables
*/
private $tables;
/**
* Constructs a Lib_BackupDB object with given parameters
* also it will invoke backup process
*
* @param string $type
* @param string $userName
* @param string $password
* @param string $serverName
* @param string $dbName
* @param string $backupPath
* @param string $tables
* @return Lib_BackupDB
*/
public function Lib_BackupDB($type,$userName,$password,$serverName,$dbName,$backupPath,$tables = '*')
{
$this->type = $type;
$this->userName=$userName;
$this->password=$password;
$this->serverName=$serverName;
$this->dbName=$dbName;
$this->backupPath=$backupPath;
$this->tables=$tables;
if($this->isValidCall())
{
if(strtolower($type)=='backup')
$this->backup();
}
}
/**
* Check whether the function call is valid or not
*
* @return bool
*/
private function isValidCall()
{
if(strtolower($this->type)!='backup')
{
echo '<b>Component Error!<b> Invalid argument <i>type</i> - backup expected';
exit();
}
else if(empty($this->userName))
{
echo '<b>Component Error!<b> Invalid argument <i>userName</i> - user name expected';
exit();
}
else if(empty($this->serverName))
{
echo '<b>Component Error!<b> Invalid argument <i>serverName</i> - server name expected';
exit();
}
else if(empty($this->dbName))
{
echo '<b>Component Error!<b> Invalid argument <i>dbName</i> - database name expected';
exit();
}
else if(empty($this->backupPath))
{
echo '<b>Component Error!<b> Invalid argument <i>backupPath</i> - backup path expected';
exit();
}
else if(!file_exists($this->backupPath))
{
echo '<b>Component Error!<b> Path not found <i>'.$this->backupPath.'</i> - backup path not found';
exit();
}
else if(!is_writable($this->backupPath))
{
echo '<b>Component Error!<b> Access Denied <i>'.$this->backupPath.'</i> - backup path not writable';
exit();
}
else if(empty($this->tables))
{
echo '<b>Component Error!<b> Invalid argument <i>tables</i> - table name expected';
exit();
}
return true;
}
/**
* check database connection and backup the database.
*
* @return string
*/
private function backup()
{
$link = mysql_connect($this->serverName,$this->userName,$this->password);
if( $link)
{
$dbCon=mysql_select_db($this->dbName,$link);
if($dbCon)
{
//get all of the tables
if($this->tables == '*')
{
$tables = array();
$res = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($res))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$this->tables);
}
if(count($tables) >0)
{
foreach($tables as $table)
{
$res = mysql_query('SELECT * FROM '.$table);
if(!empty($res))
{
$num_fields = mysql_num_fields($res);
//$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($res))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
else
{
$this->error = 1;
$this->debug['errinfo'] = array(1004=>'table not found');
return false;
}
}
//save file
$handle = fopen($this->backupPath."/".$this->dbName.'.sql','w+');
fwrite($handle,$return);
fclose($handle);
$this->result="true";
return true;
}
else
{
$this->error = 1;
$this->debug['errinfo'] = array(1003=>'table not found');
return false;
}
}
else
{
$this->error = 1;
$this->debug['errinfo'] = array(1002=>'database not found');
return false;
}
}
else
{
$this->error = 1;
$this->debug['errinfo'] = array(1001=>'connection not found');
return false;
}
}
}
?>