php/ffid.php
<?php
require_once 'cloud.php';
const MEDIA = '/media/walter'
, DESTS = ['sea' => MEDIA . '/Seagate1805', 'ele' => MEDIA . '/Elements0805', 'arc' => '/wkArchive']
, KEYP = '/ppKey'
, DIVP = '/ppDiv'
, MNTP = DIVP . "/mnt"
, RCCONLOG = "--config " . KEYP . "/cfg/rclone.conf --log-file " . DIVP . "/log/rclone.log"
;
function sysEx($cmd) {
out("+++$cmd --- begin", toLocalTst('now'));
$rc = 'bad';
$ll = system($cmd, $rc);
if ($rc === 0)
out("+++$cmd --- endOk", toLocalTst('now'));
else
err("$cmd --- failed rc=$rc, lastLine $ll.", toLocalTst('now'));
}
function dbDump() {
$c = file_get_contents('/ppKey/cfg/mysqlOpt');
preg_match("/user=(\S+)\s+password=(\S+)\s/", $c, $m);
out("c $c, match", $m);
$dbL = new PDO('mysql:host=localhost', $m[1], $m[2]);
foreach($dbL->query('show databases', PDO::FETCH_ASSOC) as $row) {
out('db', $row);
if (! isset(['information_schema' => 1, 'performance_schema' => 1, 'sys' => 1][$db = $row['Database']]))
sysEx("mysqldump --defaults-file=/ppKey/cfg/mysqlOpt $db > /wkData/save/mysqlDumpDB$db.sql");
}
}
function syncCloud($aa, $dDir) {
$oId=true;
$oSync=true;
$f = new CloudFactory;
$bakP = "$dDir/backupCloud";
$synP = "/wkArchive/backupCloud/sync";
$aa or $aa = array_keys($f->key());
foreach ($aa as $one) {
out("--- $one", toLocalTst('now'));
$k = $f->key()[$one] ?? err("cloud $one unknown");
if ($oId) {
$cc = $f->make($one);
$ff = $cc->ff($ppl = $cc->pipeline('%,metaRowCsv,PHST RECUR'));
out(" ff $one", ($ppl->ppc->rowWrite2 ?? $ppl->ppc->rowWriter)->writeC, "rows,", sprintf('%7.2e', ftell($ff)), "bytes", toLocalTst('now'));
out(' creUpd', $cc->creUpd('%all,row1,PHST KRF WRIPA', "ffid-$one.csv", 'text/csv', fRewContClo($ff)), "{$cc->fun}d", toLocalTst('now'));
}
if ($oSync) {
$inc = is_readable($incFi= KEYP . "/cfg/rclone-$one.inc") ? "--include-from $incFi" : '';
@mkdir(MNTP . "/$one", 0755); # ignore error, it normally means exists already
@mkdir("$bakP", 0755, true); # ignore error, it normally means exists already
if ($k[1] === 'godr') {
sysEx("rclone " . RCCONLOG . " sync $inc $one: $synP/$one");
sysEx("rdiff-backup $synP/$one $bakP/$one");
} else {
sysEx("rclone -v " . RCCONLOG . " mount --read-only --daemon $inc $one: " . MNTP . "/$one");
sysEx("rdiff-backup " . MNTP . "/$one $bakP/$one");
sysEx("fusermount -u " . MNTP . "/$one");
}
}
}
}
function rdiffBackup($fr, $dst) {
$ex = ".* lost+fou* *backu* book* del* lili* log* old* proj* s[0-9]* install* nextcloud old* tes* timeshi* tmp* tst* wk/extra zMysql";
if ($fr === '/wkData') {
$dTo = "$dst/backupData";
} else {
$dTo = "$dst/backupArchive";
if ($dst = '/wkArchive')
$ex .= " bilder book music";
}
$c = 'rdiff-backup -b --exclude-special-files';
foreach(preg_split('/\s+/', $ex, -1, PREG_SPLIT_NO_EMPTY) as $e)
$c .= " --exclude '$fr/$e'";
$c .= " $fr $dTo";
err("c $c");
}
function syncWWW() {
sysEx("rclone " . RCCONLOG . " sync -v --exclude '/*' /wkData/www wlklFT:wlkl.ch/public_html");
# rclone --config /ppKey/cfg/rclone.conf sync -n --include '/*' /wkData/www/variant/wlkl/html wlklFT:wlkl.ch/public_html
}
function work($aa) {
foreach($aa as $a) {
if (isset(DESTS[$a])) {
$dst = $a;
continue;
}
if ($a === 'w') {
syncWWW();
continue;
}
if ($a !== 'd' and $a !== 'a')
err("bad arg $a in", $aa);
if ( ! isset($dst)) {
foreach(DESTS as $dst => $v) {
out($dst, is_dir($v), is_writeable($v), scandir($v));
if (is_dir($v) and is_writeable($v) and count($ff = scandir($v)) > 2)
break;
}
out("default dest $dst", $ff);
}
$dDir = DESTS[$dst];
if (0)
dbDump();
if (0)
syncCloud([], $dDir);
$fr = $a === 'd' ? '/wkData' : ($a === 'a' ? '/wkArchive' : err("bad fun $a"));
rdiffBackup($fr, $dDir);
}
}
work(($ea=envArgs()) ? $ea : ['d']);
/*---- old
if (EnvCL) {
if (count($a = envArgs()) === 1 and reset($a) === 'all')
$a = ['fiwikoGo', 'spzhNC', 'wlHoNC', 'wlklNC', 'keNC', 'wlklGo', 'spwaGo'];
foreach ($a as $one) {
out("extracting $one at " . toLocalTst('now'));
$cc = $f->make($one);
$e = fopen('php://temp', 'w+');
$o = fn ($n) => fputcsv($e, $n);
$cc->ff('', $o
, fn($m) => [$o([']]tbh meta']), $o($m[0]), $o($m[1]), $o([']]tbh ffid']), $o($m[2])]);
out("$one $cc->ffFo folders, $cc->ffFi files, " . sprintf('%7.2e', $cc->ffSz) . ' Bytes (file) at ' . toLocalTst('now'));
$sz = ftell($e);
rewind($e);
out("$one writing $sz bytes to $one:ffid-$one.csv, at " . toLocalTst('now'));
$cc->upload("ffid-$one.csv" ,'text/csv', $sz < 1 ? '' : fread($e, $sz));
fclose($e);
}
outEnd();
} else {
$cc = $f->make('wlklGo');
outBegin();
$cc->ff('', fn ($n) => outTRD(...$n)
, fn($m) => [out(']]tbh meta'), outTb(), outTRH(...$m[0]), outTRD(...$m[1]), outTbEnd(), out(']]tbh ffid'), outTb(), outTRH(...$m[2])]);
outTbEnd();
out("$cc->ffFo folders, $cc->ffFi files, " . sprintf('%7.2e', $cc->ffSz) . ' Bytes (file) at ' . toLocalTst('now'));
outEnd();
} ----- end old */
/* ----------------
# $cc->create('1Ff0f9XZx_K9o20hk0DqBnP-kENaspLbY', 'testCreate.txt', 'text/plain', 'text in testCreate.txt');
# $cc->uploadVersion('1XkaWCp8lz5PrCx_MNd5Gby68Hrl5Wjmi', 'text in testCreate.txt from uploadVersion' );
$cc->upload('testUploa2.txt' ,'text/plain', 'text in testUpload.txt from upload at ' . toLocalTst('now') ."\n und so oder");
exit();
$nc->rename($nc->ff(), 'fn2valid', function($nx, $n, $fP, $fM, $tP) use($nc) {
out("$nc->cloudId, $n[0], fP", $fP, ", tP", $tP, ", fM", $fM );
echo "$nx path $n[0] invalid, renaming " . implode('/', $fM) . " ==> " . implode('/', $tP) . "\n";
$nc->mv($fM, $tP);
});
# require '/wkData/install/google-api-services/autoload.php';
// include_once __DIR__ . '/google-api/templates/base.php';
# echo "rrurl '" . RRURL . "' $_SERVER[REQUEST_SCHEME]<br>";
# echo "start " . __FILE__ . "\n";
const SCOPES =
[ 'https://www.googleapis.com/auth/drive.metadata.readonly' # drive readonly
, 'https://www.googleapis.com/auth/drive' # drive file update
, 'https://www.googleapis.com/auth/documents.readonly' # docs readonly
#, 'https://www.googleapis.com/auth/documents' # docs readWrite
];
define('KYPR', str_starts_with($rp = realpath(__FILE__), '/wkData/www/') ? '/wkData/wk/extr2/' # path prefix for locallly stored keys
: (str_starts_with($rp, '/home/ch45859/web/') ? '/home/ch45859/web/files/extr2/' : err("realpath(__FILE__) $rp not supported")));
# , RRURL = 'http://localhost/home/inf/php/e05GetServer.php'
const RRCPA = '/wkData/pub/oauth2.log'; # redirect URL writes code to this path
;
if (php_sapi_name() === 'cli') {
$tokenH = 'goRorAuthTokenFile';
$codeH = 'goRorAuthCodeCli';
define('RRURL', 'https://localhost/home/inf/php/goauth2callback.php'); # redirect URL, authorization server sends token to this URL
} else {
$tokenH = 'goRorAuthTokenSess';
$codeH = 'goRorAuthCodeWeb';
define('RRURL', "$_SERVER[REQUEST_SCHEME]://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]") ; # redirect URL, authorization server sends token to this URL
goAuthBeginWeb();
}
$c = goClient('QuW', 'Wlklxy', $tokenH, $codeH);
$drv = new Google\Service\Drive($c);
if (isset($_GET['csv'])) {
header('content-type: text/csv');
$out = fopen('php://temp', 'w+');
$r = goDrList($drv, 'root', '', fn (...$a) => fputcsv($out, $a));
rewind($out);
fpassthru($out);
fclose($out);
} else {
outBegin(__FILE__);
outTb();
$r = goDrList($drv, 'root', '', 'outTRD', 'outTRH');
outTbEnd();
outEnd(__file__);
}
------------------------------*/