<?php
class backup{
protected $dir;
function __construct()
{
$this -> dir = TEMP.'/'.passgen();
filesystem::mkdir($this -> dir);
}

function create()
{

$tables = new tables();
foreach ($tables -> tables as $table) {
if ($table{0}=='~')continue;
	if (function_exists('set_time_limit'))set_time_limit(600);
	
	$tab = new table_structure();
	$tab -> loadFromBase($table);
	$tab -> saveToIniFile($this -> dir.'/base.create.'.$table.'.ini');
	
	$tables->save_data($this -> dir.'/base.data.'.$table.'.sql',$table);
}

$ini_s = (array)glob(H.'/sys/ini/*.ini');
foreach ($ini_s as $paths) {
	copy($paths, $this -> dir.'/settings.'.basename($paths) );
}



$zip = new PclZip($this -> dir.'/archive.zip');

if (substr($this -> dir, 1,1) == ':') {
 $v_remove = substr($this -> dir, 2);
}

$v_list = $zip->create($this -> dir, PCLZIP_OPT_REMOVE_PATH,$v_remove);

if ($v_list)return $this -> dir.'/archive.zip';

}


function restore($data_zip)
{
if (!@file_put_contents($this -> dir.'/archive.zip', $data_zip))return false;
$zip = new PclZip($this -> dir.'/archive.zip');
$list = $zip->extract(PCLZIP_OPT_PATH, $this -> dir, PCLZIP_OPT_REMOVE_ALL_PATH);
if (!$list)return false;

$sqls_create = (array)glob($this -> dir.'/base.create.*.ini');
$sqls_data = (array)glob($this -> dir.'/base.data.*.sql');
$inis = (array)glob($this -> dir.'/settings.*.ini');


$tables_exists = new tables();

foreach ($sqls_create as $path) {


preg_match('#base.create\.(.+)\.ini#ui', $path,$m);
$table_name=$m[1];
// если такая таблица уже существует, то переименовываем ее
if (in_array($table_name, $tables_exists->tables))mysql_query("ALTER TABLE `".my_esc($table_name)."` RENAME `".'~'.TIME.'~'.my_esc($table_name)."`");

$tab = new table_structure($path);
mysql_query($tab -> getSQLQueryCreate());
}

foreach ($sqls_data as $path) {
// выполнение всех запросов из файла
sql_parser::executeQueriesFromFile($path);
}



foreach ($inis as $path) {
preg_match('#settings\.(.+)\.ini#ui', $path,$m);
$settings_name=$m[1];
// файлы конфигурации
@copy($path, H.'/sys/ini/'.$settings_name.'.ini');
}
return true;
}



function __destruct()
{
filesystem::rmdir($this -> dir);
}


}
?>
