Plugin system modification

This commit is contained in:
GamerClassN7
2020-10-22 16:42:49 +02:00
parent 1a3e659ca7
commit 946a93a23b
9 changed files with 282 additions and 155 deletions

View File

@@ -1,27 +1,46 @@
<?php
class DatabaseBackup {
public function scheme(){
$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR ."/backup/" . DBNAME.'_scheme_'.date("Y-m-d", time()).'.sql';
$command = "mysqldump --skip-comments --no-create-info -h localhost -u ". DBUSER . " -p" . DBPASS ." ". DBNAME ." -r $backupfile 2>&1";
class DatabaseBackup
{
public function make()
{
try {
$filenames = [];
$backupWorker = new DatabaseBackup;
$filenames[] = $backupWorker->scheme();
$filenames[] = $backupWorker->data();
$backupWorker->compress($_SERVER['DOCUMENT_ROOT'] . BASEDIR . '/backup/' . date("Y-m-d", time()) . '.zip', $filenames);
return 'sucessful';
} catch (Exception $e) {
return 'exception: ' . $e->getMessage();
}
}
private function scheme()
{
$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR . "/backup/" . DBNAME . '_scheme_' . date("Y-m-d", time()) . '.sql';
$command = "mysqldump --skip-comments --no-create-info -h localhost -u " . DBUSER . " -p" . DBPASS . " " . DBNAME . " -r $backupfile 2>&1";
$this->executeCommand($command);
return $backupfile;
}
public function data(){
$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR ."/backup/" . DBNAME.'_data_'.date("Y-m-d", time()).'.sql';
$command = "mysqldump --skip-comments --no-data -h localhost -u ". DBUSER . " -p" . DBPASS ." ". DBNAME ." -r $backupfile 2>&1";
private function data()
{
$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR . "/backup/" . DBNAME . '_data_' . date("Y-m-d", time()) . '.sql';
$command = "mysqldump --skip-comments --no-data -h localhost -u " . DBUSER . " -p" . DBPASS . " " . DBNAME . " -r $backupfile 2>&1";
$this->executeCommand($command);
return $backupfile;
}
private function executeCommand($command){
private function executeCommand($command)
{
ini_set('date.timezone', 'Europe/Prague');
exec($command);
}
public function compress($filename, $files = []) {
private function compress($filename, $files = [])
{
$zip = new ZipArchive();
if($zip->open($filename,ZipArchive::CREATE|ZipArchive::OVERWRITE)) {
if ($zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
foreach ($files as $file) {
$zip->addFile($file);
}
@@ -31,4 +50,28 @@ class DatabaseBackup {
}
}
}
private function cleaningDir($dir, $seconds)
{
$todayFileName = date("Y-m-d") . '.zip';
$logFiles = scandir($dir);
foreach ($logFiles as $key => $file) {
if (in_array($file, array(".", "..", ".gitkeep", $todayFileName))) {
continue;
}
if (!is_dir($dir . $file)) {
if (strtotime(str_replace(".zip", "", $file)) < (strtotime("now") - $seconds)) {
unlink($dir . $file);
}
} else {
$this->cleaningDir($dir . $file . "/", $seconds);
}
}
}
public function purge($days)
{
$seconds = $days * 86400;
$this->cleaningDir('../backup/', $seconds);
}
}