2020-10-19 14:23:39 +00:00
|
|
|
<?php
|
2020-10-22 14:42:49 +00:00
|
|
|
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";
|
2020-10-19 14:23:39 +00:00
|
|
|
$this->executeCommand($command);
|
|
|
|
return $backupfile;
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:42:49 +00:00
|
|
|
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";
|
2020-10-19 14:23:39 +00:00
|
|
|
$this->executeCommand($command);
|
|
|
|
return $backupfile;
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:42:49 +00:00
|
|
|
private function executeCommand($command)
|
|
|
|
{
|
2020-10-19 14:23:39 +00:00
|
|
|
ini_set('date.timezone', 'Europe/Prague');
|
|
|
|
exec($command);
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:42:49 +00:00
|
|
|
private function compress($filename, $files = [])
|
|
|
|
{
|
2020-10-19 14:23:39 +00:00
|
|
|
$zip = new ZipArchive();
|
2020-10-22 14:42:49 +00:00
|
|
|
if ($zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
|
2020-10-19 14:23:39 +00:00
|
|
|
foreach ($files as $file) {
|
|
|
|
$zip->addFile($file);
|
|
|
|
}
|
2020-10-22 15:59:45 +00:00
|
|
|
$zip->close();
|
2020-10-19 14:23:39 +00:00
|
|
|
foreach ($files as $file) {
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 14:42:49 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2020-10-19 14:23:39 +00:00
|
|
|
}
|