Db Backup Plugin

This commit is contained in:
JonatanRek 2020-10-19 16:23:39 +02:00
parent 0b954b79c0
commit a3a482c652
2 changed files with 41 additions and 0 deletions

View File

@ -13,6 +13,13 @@ class CronApi extends ApiController {
echo (new OpenWeatherMap)->fetch('');
echo (new UsaElection)->fetch('');
// Database Backup
$filenames = [];
$backupWorker = new DatabaseBackup;
$filenames[] = $backupWorker->scheme();
$filenames[] = $backupWorker->data();
$backupWorker->compress($_SERVER['DOCUMENT_ROOT'] . BASEDIR . '/backup/'.date("Y-m-d", time()).'.zip', $filenames);
$this->response(['Value' => 'OK']);
}
}

View File

@ -0,0 +1,34 @@
<?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";
$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";
$this->executeCommand($command);
return $backupfile;
}
private function executeCommand($command){
ini_set('date.timezone', 'Europe/Prague');
exec($command);
}
public function compress($filename, $files = []) {
$zip = new ZipArchive();
if($zip->open($filename,ZipArchive::CREATE|ZipArchive::OVERWRITE)) {
foreach ($files as $file) {
$zip->addFile($file);
}
echo $zip->close();
foreach ($files as $file) {
unlink($file);
}
}
}
}