<?
$q=mysql_query("SELECT * FROM `cron` WHERE `id` = 'backup_mysql'");
if (mysql_num_rows($q)==0)mysql_query("INSERT INTO `cron` (`id`, `time`) VALUES ('backup_mysql', '".time()."')");
$backup = mysql_fetch_array($q);
if (eregi('^[^@]*@[^@]*\.[^@]*$',$set['mail_backup']) && ($backup==NULL || $backup['time']<time()-60*60*24)){
mysql_query("UPDATE `cron` SET `time` = '".time()."' WHERE `id` = 'backup_mysql'");
@unlink(H."sys/tmp/MySQL.sql.gz");
$list_tables=NULL;
$tab=mysql_list_tables($set['mysql_db_name']);



for($i=0;$i<mysql_num_rows($tab);$i++)
{
$sql=NULL;
$table=mysql_tablename($tab,$i);

$sql.="DROP TABLE IF EXISTS `$table`;\r\n";
$res=@mysql_query("SHOW CREATE TABLE `$table`");
$row=@mysql_fetch_row($res);
$sql.=$row[1].";\r\n\r\n";
$res = @mysql_query("SELECT * FROM `$table`");
if (@mysql_num_rows($res) > 0) {
while (($row = @mysql_fetch_assoc($res))) {
$keys = @implode("`, `", @array_keys($row));
$values = @array_values($row);
foreach($values as $k=>$v) {

$values[$k] = mysql_real_escape_string($v);
$values[$k]=ereg_replace("(\n|\r){1,}", '\n', $values[$k]);

}
$values2 = @implode("', '", $values);
$values2 = "'".$values2."'";
$values2= str_replace("''", "null", $values2);

$sql .= "INSERT INTO `$table` (`$keys`) VALUES ($values2);\r\n";
}

$sql .= "\r\n\r\n";
}
$fopen_mysql=fopen(H."sys/tmp/MySQL.sql.gz",'a');
//if (strlen($sql)<5*1024*1024)
fwrite($fopen_mysql, gzencode($sql,9));
fclose($fopen_mysql);
}


$subj="Бэкап MySQL";
$bound="--".md5(uniqid(time()));
$headers="From: \"BackUP@$_SERVER[HTTP_HOST]\" <BackUp@$_SERVER[HTTP_HOST]>\r\n";
$headers.="To: $set[mail_backup]\r\n";
$headers.="Subject: $subj\r\n";
$headers.="Mime-Version: 1.0\r\n";
$headers.="Content-Type: multipart/alternative; boundary=\"$bound\"\r\n";


$body="--$bound\r\n";

$body.="Content-type: text/html; charset=\"utf-8\"\r\n";
$body.="Content-Transfer-Encoding: 8bit\r\n";
$body.="\r\n";
$body.="<h3>Автоматическая отправка BackUp базы данных</h3>";

$body.="\r\n";
$body.="--$bound\r\n";



$body.="Content-Disposition: attachment;\r\n";
$body.="Content-Type: application/x-gzip; name=\"MySQL.sql.gz\"\r\n";
$body.="Content-Transfer-Encoding:base64\r\n";
$body.="\r\n";
$body.=base64_encode(file_get_contents(H."sys/tmp/MySQL.sql.gz"))."\r\n";


$body.="--$bound\r\n";


$body.="Content-Disposition: attachment;\r\n";
$body.="Content-Type: text/plain; name=\"settings_6.2.dat\"\r\n";
$body.="Content-Transfer-Encoding:base64\r\n";


$body.=base64_encode(file_get_contents(H."sys/dat/settings_6.2.dat"))."\r\n";

$body.="--$bound";
$body.="--\r\n";

mail("$set[mail_backup]", $subj, $body, $headers);

unlink(H."sys/tmp/MySQL.sql.gz");




}
?>