PHP_SMART_HOME_V3/app/plugins/DatabaseBackup.php

94 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2020-10-19 14:23:39 +00:00
<?php
2020-10-22 14:42:49 +00:00
class DatabaseBackup
{
public function make()
{
//Register the settings
$time = '00:00';
$settingMng = new SettingsManager();
if (!($settingField = $settingMng->getByName("backup_time","db_backup"))) {
$settingMng->create("backup_time", $time, "db_backup");
} else {
$time = $settingField['value'];
}
//Time to Backup ?
if (date("H:i",time()) != $time)
return 'pending';
2020-10-22 14:42:49 +00:00
try {
$filenames = [];
$backupWorker = new DatabaseBackup;
2020-10-26 15:27:15 +00:00
$filenames[] = $backupWorker->scheme(); //Backup Database scheme
$filenames[] = $backupWorker->data(); //Backup Database Data
//$filenames[] = $_SERVER['DOCUMENT_ROOT'] . '/config/config.php'; //Backup Configuration File
2021-01-05 09:37:32 +00:00
$backupWorker->compress($_SERVER['DOCUMENT_ROOT'] . BASEDIR . '/backup/' . date("Y-m-d", time()) . '.zip', $filenames);
2020-10-22 14:42:49 +00:00
return 'sucessful';
} catch (Exception $e) {
return 'exception: ' . $e->getMessage();
}
}
2021-01-05 09:37:32 +00:00
private function data()
2020-10-22 14:42:49 +00:00
{
2021-01-05 09:37:32 +00:00
$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR . "/backup/" . DBNAME . '_data_' . date("Y-m-d", time()) . '.sql';
if (file_exists($backupfile)) return null;
2020-10-22 14:42:49 +00:00
$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;
}
2021-01-05 09:37:32 +00:00
private function scheme()
2020-10-22 14:42:49 +00:00
{
2021-01-05 09:37:32 +00:00
$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR . "/backup/" . DBNAME . '_scheme_' . date("Y-m-d", time()) . '.sql';
if (file_exists($backupfile)) return null;
2020-10-22 14:42:49 +00:00
$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) {
2021-01-05 09:37:32 +00:00
$zip->addFile($file);
2020-10-19 14:23:39 +00:00
}
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
}