folder backup

This commit is contained in:
shimon
2023-02-28 19:32:30 +02:00
parent 0d7c11f19b
commit 0e9286d2f5
2 changed files with 32 additions and 41 deletions
+31 -40
View File
@@ -20,41 +20,39 @@ class Backup extends Action
->callback(fn() => $this->action());
}
/**
* @throws \Exception
*/
public function action(): void
{
Console::title('Backup V1');
Console::success(APP_NAME . ' backup process v1 has started');
gc_enable();
$time = 0;
while (!connection_aborted() || PHP_SAPI == "cli") {
$now = new \DateTime();
$now->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$next = new \DateTime($now->format("Y-m-d 16:0.0"));
$next->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$sleep = $next->getTimestamp() - $now->getTimestamp();
$now = new \DateTime();
$now->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$next = new \DateTime($now->format("Y-m-d 17:25.0"));
$next->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$delay = $next->getTimestamp() - $now->getTimestamp();
/**
* If time passed the target time
*/
if ($sleep <= 0) {
$next->add(\DateInterval::createFromDateString('1 days'));
$sleep = $next->getTimestamp() - $now->getTimestamp();
}
/**
* If time passed for the target day.
*/
if ($delay <= 0) {
$next->add(\DateInterval::createFromDateString('1 days'));
$delay = $next->getTimestamp() - $now->getTimestamp();
}
Console::log('[' . $now->format("Y-m-d H:i:s.v") . '] Sleeping for ' . $sleep . ' seconds next run will be at [' . $next->format("Y-m-d H:i:s.v") . ']');
$folders = [
'cert' => APP_STORAGE_CERTIFICATES,
'config' => APP_STORAGE_CONFIG,
];
sleep($sleep);
$sleep = 86400; //24 hours
$sleep = 60;
$remote = getDevice('/');
$total = 0;
$start = \microtime(true);
$folders = [
'cert' => APP_STORAGE_CERTIFICATES,
'config' => APP_STORAGE_CONFIG,
];
$remote = getDevice('/');
Console::log('[' . $now->format("Y-m-d H:i:s.v") . '] Delaying for ' . $delay . ' setting loop to [' . $next->format("Y-m-d H:i:s.v") . ']');
Console::loop(function () use ($delay, $sleep, $folders, $remote) {
foreach ($folders as $key => $folder) {
$local = new Local($folder);
@@ -62,19 +60,20 @@ class Backup extends Action
$source = $local->getRoot() . '/' . $filename;
$destination = '/' . $key . '/' . $filename;
for ($i = 0; $i < 1000; $i++) {
file_put_contents($local->getRoot() . '/' . $i . '.txt', '');
}
// for ($i = 0; $i < 1000; $i++) {
// file_put_contents($local->getRoot() . '/' . $i . '.txt', '');
// }
$stdout = '';
$stderr = '';
Console::execute(
'cd ' . $folder . ' && tar --exclude ' . $filename . ' -zcf ' . $source . ' *',
'',
$stdout,
$stderr
);
var_dump($local->exists($source));
try {
if (!$local->exists($source)) {
continue;
@@ -84,7 +83,7 @@ class Backup extends Action
Console::info("Backing up local $source to {$remote->getName()} $destination");
/**
* Remote storage old backup files clean-up
* Backup folder, long tail cleanup.
*/
$now = new \DateTime();
$now->setTimezone(new \DateTimeZone(date_default_timezone_get()));
@@ -99,15 +98,7 @@ class Backup extends Action
} catch (\Exception $e) {
Console::error($e->getMessage());
}
if (PHP_SAPI == "cli") {
if ($time >= 60 * 5) {
$time = 0;
gc_collect_cycles();
}
}
}
$total = (microtime(true) - $start);
}
}, $sleep, $delay);
}
}