Файл: sys/fnc/backup.php
Строк: 204
<?php
if (!isset($hard_process)) {
$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_assoc($q);
if (preg_match('#^[a-z0-9_-.]+@[a-z0-9_-.]+$#iu', $set['mail_backup']) && ($backup['time'] == NULL || $backup['time'] < time() - 60 * 60 * 24)) {
mysql_query("UPDATE `cron` SET `time` = '" . time() . "' WHERE `id` = 'backup_mysql'");
$hard_process = true;
time_limit(600); // Ставим ограничение на 10 минут
@unlink(H . "sys/tmp/MySQL.sql.gz");
$list_tables = NULL;
$tab = mysql_query('SHOW TABLES');
$fopen_mysql = @fopen(H . "sys/tmp/MySQL.sql.gz", 'a');
@fwrite($fopen_mysql, gzencode("SET CHARSET UTF8;rnSET NAMES UTF8;rnrn", 9));
@fclose($fopen_mysql);
while ($tables = mysql_fetch_array($tab)) {
$table = $tables[0];
if ($table == 'visit_today') {
continue;
}
$sql = "/* Создание таблицы `$table` */rn";
$res = @mysql_query("SHOW CREATE TABLE `$table`");
$row = @mysql_fetch_row($res);
$sql.=$row[1] . ";rnrn";
$c_ins = 2000;
$num_row_all = mysql_result(mysql_query("SELECT COUNT(*) FROM `$table`"), 0);
$start = 0;
if ($num_row_all) {
$sql.="/* Данные таблицы `$table` */rn";
$table_keys = @implode("`, `", @array_keys(mysql_fetch_assoc(mysql_query("SELECT * FROM `$table` LIMIT 1"))));
while ($start < $num_row_all) {
$res = mysql_query("SELECT * FROM `$table` LIMIT $start, $c_ins");
if ($num_row_all > $c_ins) {
$sql .= "/* блок записей $start - " . ($start + $c_ins) . " */rn";
}
$sql .= "INSERT INTO `$table` (`$table_keys`) VALUES ";
$num_row = mysql_num_rows($res);
$counter = 0;
while (($row = @mysql_fetch_assoc($res))) {
$values = @array_values($row);
foreach ($values as $k => $v) {
$values[$k] = "'" . mysql_real_escape_string(preg_replace("#(n|r){1,}#", 'n', $v)) . "'";
//$values[$k]=$values[$k]?"'$values[$k]'":'null';
}
$values_string = @implode(', ', $values);
$counter++;
$sql .= "($values_string)" . ($counter == $num_row ? ";rn" : ', ');
}
$start = $start + $c_ins;
}
} else {
$sql.="/* Таблица `$table` пуста */rn";
}
$sql.="rnrn";
$fopen_mysql = fopen(H . "sys/tmp/MySQL.sql.gz", 'a');
fwrite($fopen_mysql, gzencode($sql, 9));
unset($sql);
fclose($fopen_mysql);
}
$EOL = "rn";
$subj = 'Данные для восстановления сайта';
$bound = "--" . md5(uniqid(time()));
$headers = "From: "DCMS - BackUP" <autobackup@$_SERVER[HTTP_HOST]>$EOL";
$headers.="To: $set[mail_backup]$EOL";
$headers.="Subject: $subj$EOL";
$headers.="Mime-Version: 1.0$EOL";
$headers.="Content-Type: multipart/mixed; boundary="$bound"$EOL";
$body = "--$bound$EOL";
$body.="Content-Type: text/html; charset="utf-8"$EOL";
$body.="Content-Transfer-Encoding: 8bit$EOL";
$body.=$EOL;
$body.="Автоматическая отправка BackUp базы данных<br />";
$body.="<b>$set[mysql_db_name].sql.gz</b> - сжатый SQL-файл базы данных <b>$set[mysql_db_name]</b><br />";
$body.="<b>settings.ini.gz</b> - сжатый файл настроек системы <b>sys/dat/settings.ini</b><br />";
$body.="$EOL--$bound$EOL";
$body.="Content-Type: application/x-gzip; name="$set[mysql_db_name].sql.gz"$EOL";
$body.="Content-Disposition: attachment; filename="$set[mysql_db_name].sql.gz"$EOL";
$body.="Content-Transfer-Encoding: Base64$EOL";
$body.=$EOL;
$body.=chunk_split(base64_encode(file_get_contents(H . "sys/tmp/MySQL.sql.gz")));
unlink(H . "sys/tmp/MySQL.sql.gz");
$body.="$EOL--$bound$EOL";
$body.="Content-Type: application/x-gzip; name="settings.ini.gz"$EOL";
$body.="Content-Disposition: attachment; filename="settings.ini.gz"$EOL";
$body.="Content-Transfer-Encoding: Base64$EOL";
$body.=$EOL;
$body.=chunk_split(base64_encode(gzencode(file_get_contents(H . "sys/dat/settings.ini"), 9)));
$body.="$EOL--$bound$EOL";
$body.="Content-Type: application/x-gzip; name="update.files.ini.gz"$EOL";
$body.="Content-Disposition: attachment; filename="update.files.ini.gz"$EOL";
$body.="Content-Transfer-Encoding: Base64$EOL";
$body.=$EOL;
$body.=chunk_split(base64_encode(gzencode(file_get_contents(H . "sys/dat/update.files.ini"), 9)));
$body.="$EOL--$bound$EOL";
$body.="Content-Type: application/x-gzip; name="menu.main.ini.gz"$EOL";
$body.="Content-Disposition: attachment; filename="menu.main.ini.gz"$EOL";
$body.="Content-Transfer-Encoding: Base64$EOL";
$body.=$EOL;
$body.=chunk_split(base64_encode(gzencode(file_get_contents(H . "sys/dat/menu.main.ini"), 9)));
$body.="$EOL--$bound$EOL";
$body.="Content-Type: application/x-gzip; name="user.menu.ini.gz"$EOL";
$body.="Content-Disposition: attachment; filename="user.menu.ini.gz"$EOL";
$body.="Content-Transfer-Encoding: Base64$EOL";
$body.=$EOL;
$body.=chunk_split(base64_encode(gzencode(file_get_contents(H . "sys/dat/user.menu.ini"), 9)));
$body.="$EOL--$bound--$EOL";
mail("$set[mail_backup]", '=?utf-8?B?' . base64_encode($subj) . '?=', $body, $headers);
}
}