Record [f:http_backup.php] < dev.sky.1.001
Version 1.001
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 074 075 076 077 078 079 080 081 082 083 084 085 086 087 | <?php $arch = 'main/archive.bz2'; if ('cli' == PHP_SAPI) { chdir(realpath(dirname(__FILE__))); $mode = 'cron'; } else { $code = @key($_GET) or die; $mode = current($_GET); if ('mode' == $code) $code = @$_POST['code']; md5($code) == 'bbxn298dfjkwi22mdhakw992ksm2ms2s' or die; } define('AJAX', 1); define('START', 'cron'); $skip_dir = $skip_trash = $skip_file = $skip_files_in_dir = $skip_insert = array(); $dirs = array('.'); require 'main/conf.php'; $sky = new SKY; extract($sky->load(), EXTR_PREFIX_ALL | EXTR_REFS, 's') or exit('err mem'); function write($str) { global $bz; bzwrite($bz, $str, strlen($str)); } function write_dirs($path, $skip = false) { global $skip_dir, $skip_trash, $dirs; $list = glob($path); foreach ($list as $one) if (is_dir($one) && '_' != $one[0] && !in_array($one, $skip_trash)) { write("$one\n"); $no_files = in_array($one, $skip_dir) || $skip or array_push($dirs, $one); write_dirs("$one/*", $no_files); } } if ($mode == 'ini'): sqlf("update memory set dt=now(), tmemo=%s where id=7", escape($_POST['rules'])); echo 'ok'; endif; if ($mode == 'full' || $mode == 'cron'): if ($mode == 'cron') { $_POST['rules'] = sql("+select tmemo from memory where id=7"); } if (@$_POST['rules']) { $vars = $sky->explode('b', $_POST['rules']); foreach ($vars as &$one) $one = unserialize($one); extract($vars, EXTR_OVERWRITE | EXTR_REFS); } array_push($skip_file, $arch, basename(__FILE__)); $bz = bzopen(ROOT . '/' . $arch, 'w'); write("### folders: ###\n"); write_dirs('*'); write("### file: database.sql ###\nset names utf8;\n"); $tables = array_filter(fetch(sql("show tables"), 0), create_function('$v', 'return "_dev_" != substr($v, 0, 5);')); foreach ($tables as $one) { $code = fetch(sql("show create table $one"), true); write(preg_replace("/\s+/sm", ' ', $code[1]).";\n"); } foreach ($tables as $one) if (!in_array($one, $skip_insert)) { for ($i = 0, $isi = array(), $q = sql("explain $one"); $r = fetch($q, true); $i++) $isi[$i] = false !== strpos($r[1], 'int'); for ($q = sql("select * from $one"); $r = fetch($q, true); ) { foreach ($r as $i => $col) $r[$i] = isset($r[$i]) ? ($isi[$i] && is_numeric($r[$i]) ? $col : escape($col)) : 'NULL'; write("insert into $one values(".join(',', $r).");\n"); } } foreach ($dirs as $one) { $files = isset($skip_files_in_dir[$one]) ? explode(',', $skip_files_in_dir[$one]) : array(); for ($dh = opendir($one); $file = readdir($dh); ) is_file($file = $one ? "$one/$file" : $file) && !in_array($file, $skip_file) && !in_array(basename($file), $files) && 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; |
en | Poster is a subapplication inside WORK.SKY. app. Below is a script for poster app. |
• N1: DATA: POSTER_backup
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 | ini_set('user_agent', "poster"); $dirs = glob("d:/path_to_all_backups_dir/all/*"); eval($TMEMO); foreach($dirs as $dir) if (is_dir($dir)) { if (!file_exists("$dir/rules.ini")) continue; eval(file_get_contents("$dir/rules.ini")); if ($get_method == 'none') continue; $prjs = glob("$dir/prj-*.bz2"); sort($prjs); reset($prjs); if (count($prjs) > $save_backup_count) { // delete old backups $unlink = array_splice($prjs, 0, count($prjs) - $save_backup_count); foreach($unlink as $file) unlink($file); } $mtime = $prjs ? filemtime(array_pop($prjs)) : 0; $dir_name = basename($dir); $req_arch = (time() - $mtime) / 3600 / 24 > $backup_interval; $req_sett = !(@$bu_ary[$dir_name]) && 'get' == $get_method; if ($req_arch || $req_sett) { $skip = array(); foreach ($new_for_dir as $val) { $files = array_map('basename', glob("$local_dir/$val/*")); $skip[$val] = join(',', $files); } $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => http_build_query(array( 'code' => $secret_key, 'rules' => 'skip_insert ' . serialize($skip_insert) . "\n" . 'skip_dir ' . serialize($skip_dir) . "\n" . 'skip_trash ' . serialize($skip_trash) . "\n" . 'skip_file ' . serialize($skip_file) . "\n" . 'skip_files_in_dir ' . serialize($skip) )))); $url = "$remote_addr?mode=" . ($req_sett ? 'ini' : $get_method); if ($fr = fopen($url, 'rb', false, stream_context_create($opts))): if ($req_sett) { if ('ok' == fread($fr, 8192)) $bu_ary[$dir_name] = 1; else $ERROR = 'Cannot write ini data'; } else { # write arch $fw = fopen("$dir/prj-".date("Y-m-d-H").'.bz2', 'wb'); while (!feof($fr)) fwrite($fw, fread($fr, 8192)); fclose($fw); $bu_ary[$dir_name] = 0; $IMEMO++; } fclose($fr); $TMEMO = '$bu_ary = ' . var_export($bu_ary, true) . ';'; break; // IF OK endif; } } |
• N2: WRITE FILE: rules.ini
180 | ... --file cutted-- |
• N3: WRITE FILE: x_explode.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 | <?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) { 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; } elseif (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 (!$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)) if ('' === $dir || '/' == $dir[0] || false != strpos($dir, '..')) exit('Dir error'); else 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"; |
en |
x_explode is a console script for extract archive's backups.
|
Codebase entries (89)
SKY. status
Current version: 1.001
Coresky records: 22
Local (DEV) records: 89
Web (all) records: 105
Download: dev.php
Coresky records: 22
Local (DEV) records: 89
Web (all) records: 105
Download: dev.php