Запись [x:x_explode.php] < dev.sky.1.001
Версия 1.000
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 | <?php isset($argv[1]) and file_exists($argv[1]) or die('Can\'t find input file'); $par = isset($argv[2]) ? $argv[2] : ''; # --all --dirs or path(files) or file(one) function next_file(&$file, $name = '') { # . - emulate write, empty string - don't crop buffer global $size, $str, $bz, $q; if ($name && '.' != $name) $fw = fopen($name, 'wb'); $pos = $m = $written = $offset = 0; do { if (strlen($str) < 10240) feof($bz) or $str .= bzread($bz, 10240); $pos = strpos($str, $_ = $size ? "\n### " : '### ', $offset); if (false !== $pos && preg_match("/^$_(file: ([^:]+)(: (\d+))$q|end package:) ###/sm", substr($str, $pos), $m)) { if ($m[1] == 'end package:' && !feof($bz)) $m = false; if (!$q && ($size != $written + $pos)) { # check filesize! $m = false; if ($size < $written + $pos) break; } } if (false === $pos) { if ($name) { if ('.' != $name) fwrite($fw, substr($str, 0, 5120), 5120); # FILE WRITE $str = substr($str, 5120); $written += 5120; $offset = 0; } if ($_ = feof($bz)) break; else $str .= bzread($bz, 1024); continue; } if (!$m) { if (false !== $pos) $offset = 1 + $pos; elseif ($_ && strlen($str) <= $offset) break; # ! continue; } if (strlen($str) - $pos < 1024) feof($bz) or $str .= bzread($bz, 1024); } while (!$m); $m or exit("bz2 archive corrupted at '$name $size < $written + $pos'"); $file = isset($m[2]) ? $m[2] : $m[1]; $size = isset($m[4]) ? $m[4] : -1; print '.'; $data = substr($str, 0, $pos); $str = substr($str, $pos + 1 + strlen($m[0])); if (!$size && "\n" == $str[0]) $str = substr($str, 1); # work 0 size both cases if ($name && '.' != $name) false !== fwrite($fw, $data, $pos) and fclose($fw); # FILE WRITE return $data; } $q = '?'; $bz = bzopen($argv[1], 'r'); $str = bzread($bz, 1024); $file = $size = 0; substr($str, 0, 17) == "### folders: ###\n" or die('error in bz2 archive'); $str = substr($str, 17); if ($data = trim(next_file($file))) foreach(explode("\n", $data) as $dir) if ($par == '--all' || $par == '--dirs' || 0 === strpos($par, $dir)) '' === $dir || file_exists($dir) || mkdir($dir); if ($file == 'database.sql' && $size == -1) next_file($file, $par == '--all' || $par == $file ? $file : '.'); $q = ''; while ($file != 'end package:' || $size != -1) next_file($file, $par == '--all' || $par == substr($file, 0, strlen($par)) ? $file : '.'); bzclose($bz); print "\nx_explode OK"; |
• N1: MESSAGE: both
001 002 003 004 005 006 | 2do: Переделать систему инкрементальной архивации,
команды
folders => dirs
### time: время ###
### mode: incremental ###
mode one of [incremental|full]
|
• N2: WRITE FILE: x_bz2_extract.php
001 002 003 004 005 006 007 008 009 010 011 | <? $bz = bzopen($argv[1], 'r'); $wr = fopen($argv[1] . '.extracted', 'w'); while(!feof($bz)) { $str = bzread($bz); fwrite($wr, $str); } |
• N3: WRITE FILE: x_md5.php
001 002 003 004 005 | <?php
isset($argv[1]) or die;
echo $argv[1].' '.md5($argv[1]);
|
• N4: WRITE FILE: http_backup.php
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 | <?php $arch = 'main/archive.bz2'; if (isset($argv[1])) { chdir(realpath(dirname(__FILE__))); $_GET[$argv[1]] = 'arch'; } $_GET or die; $code = key($_GET); $mode = array_shift($_GET); $code == 'mode' and $code = @$_POST['code']; md5($code) == '33b67423658aba388539f3df28cb78d3' or die; $skip_dir = $skip_file = $skip_files_in_dir = $skip_insert = array(); file_exists('rules.ini') and eval(file_get_contents('rules.ini')); $dir_list = array(''); if (isset($_POST['rules'])) eval(get_magic_quotes_gpc() ? stripslashes($_POST['rules']) : $_POST['rules']); array_push($skip_file, $arch, basename(__FILE__)); function write($str) { global $bz; bzwrite($bz, $str, strlen($str)); } function folders($path, $skip = false) { global $skip_dir, $dir_list; $list = glob($path); foreach($list as $name) if (is_dir($name)) { write("$name\n"); $no_files = array_search($name, $skip_dir, true) !== false || $skip; $no_files or array_push($dir_list, $name); folders("$name/*", $no_files); } } define('START', 'cron'); require 'conf.php'; if ($mode == 'full' || $mode == 'arch'): /////////////////////////////////// $bz = bzopen(ROOT . '/' . $arch, 'w'); write("### folders: ###\n"); folders('*'); write("### file: database.sql ###\n"); $tables = fetch(sql("show tables"), 0); foreach($tables as $table) { $code = fetch(sql("show create table $table")); write(preg_replace("/\s+/sm", ' ', $code[1]).";\n"); } foreach($tables as $table) if (array_search($table, $skip_insert, true) === false) { $q = sql("select * from $table"); while($r = fetch($q)) { foreach($r as $i => $col) $r[$i] = isset($r[$i]) ? (is_numeric($r[$i]) ? $col : "'".mysql_real_escape_string($col)."'") : 'NULL'; write("insert into $table values(".join(',', $r).");\n"); } } foreach($dir_list as $dir) { $file_list = isset($skip_files_in_dir[$dir]) ? explode(',', $skip_files_in_dir[$dir]) : array(); $dh = opendir($dir ? $dir : '.'); while ($file = readdir($dh)) is_file($file = $dir ? "$dir/$file" : $file) && false === array_search($file, $skip_file, true) && false === array_search(basename($file), $file_list, true) && write("### file: $file: ".filesize($file)." ###\n".file_get_contents($file)."\n"); closedir($dh); } write('### end package: ###'); bzclose($bz); endif; if ($mode == 'full' || $mode == 'get'): //////////////////////////////////// $stat = stat($arch); header('Content-Length: '.filesize($arch)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="prj-'.date("Y-m-d", $stat[10]).'.bz2"'); readfile($arch); endif; |