Shikiryu_Backup/app/code/backup/BackupAbstract.php

274 lines
6.3 KiB
PHP
Raw Normal View History

2015-07-03 00:00:30 +02:00
<?php
2015-07-10 19:07:14 +02:00
namespace Shikiryu\Backup\Backup;
2015-07-03 00:00:30 +02:00
2015-08-17 13:16:28 +02:00
abstract class BackupAbstract
2015-07-03 00:00:30 +02:00
{
2016-07-03 21:54:35 +02:00
/**
* Options
*
* @var array
*/
2015-08-18 17:02:37 +02:00
protected $options;
2016-07-03 21:54:35 +02:00
/**
* File path to backup
*
* @var string[]
*/
protected $files_to_backup = [];
/**
* Streams to backup
*
* @var string[]
*/
protected $streams_to_backup = [];
2015-07-03 00:00:30 +02:00
/**
2016-07-03 21:54:35 +02:00
* Constructor
*
* @param array $config array of options and parameters
2015-07-03 00:00:30 +02:00
*/
public function __construct($config = [])
2015-07-03 00:00:30 +02:00
{
2015-08-18 17:02:37 +02:00
$this->options = !empty($config['options']) ? $config['options'] : [];
unset($config['options']);
2015-07-12 20:34:54 +02:00
foreach ($config as $name => $value) {
$this->$name = $value;
}
2015-08-18 17:02:37 +02:00
$this->init();
2015-07-03 00:00:30 +02:00
}
2015-07-12 20:34:54 +02:00
/**
* Magic setter method
*
2016-07-03 21:54:35 +02:00
* @param string $name attribute name
* @param mixed $value attribute value
2019-01-25 21:39:18 +01:00
*
2016-07-03 21:54:35 +02:00
* @return $this
2015-07-12 20:34:54 +02:00
*/
public function __set($name, $value)
{
$this->$name = $value;
2016-07-03 21:54:35 +02:00
return $this;
2015-07-12 20:34:54 +02:00
}
2015-07-09 00:46:17 +02:00
/**
2016-07-03 21:54:35 +02:00
* Getter
*
2015-07-09 00:46:17 +02:00
* @return array
*/
public function getFilesToBackup()
{
2016-07-03 21:54:35 +02:00
return $this->files_to_backup;
2015-07-09 00:46:17 +02:00
}
/**
2016-07-03 21:54:35 +02:00
* Getter
*
2015-07-09 00:46:17 +02:00
* @return array
*/
public function getStreamsToBackup()
{
2016-07-03 21:54:35 +02:00
return $this->streams_to_backup;
2015-07-09 00:46:17 +02:00
}
2015-08-18 17:02:37 +02:00
/**
* Check if all files got the minimum given size.
*
2016-07-03 21:54:35 +02:00
* @param int $file_size file size
2015-08-18 17:02:37 +02:00
*
* @return bool
*/
public function checkMinimumFilesize($file_size)
2015-08-18 17:02:37 +02:00
{
2016-07-03 21:54:35 +02:00
foreach ($this->files_to_backup as $file => $name) {
if (filesize($file) < $file_size) {
2015-08-18 17:02:37 +02:00
return false;
}
}
2016-07-03 21:54:35 +02:00
foreach ($this->streams_to_backup as $name => $file) {
if (mb_strlen($file, 'utf-8') < $file_size) {
2015-08-18 17:02:37 +02:00
return false;
}
}
return true;
}
2016-07-03 21:54:35 +02:00
/**
* Initialize everything
2019-01-25 21:39:18 +01:00
*
2016-07-03 21:54:35 +02:00
* @return $this
*/
2015-08-18 17:02:37 +02:00
protected function init()
{
$this->preBuild();
$this->build();
$this->postBuild();
$this->applyOptions();
2016-07-03 21:54:35 +02:00
return $this;
2015-08-18 17:02:37 +02:00
}
2016-07-03 21:54:35 +02:00
/**
* Function that can be used to initialize the backup
2019-01-25 21:39:18 +01:00
*
2016-07-03 21:54:35 +02:00
* @return void
*/
2015-08-18 17:02:37 +02:00
abstract protected function preBuild();
2016-07-03 21:54:35 +02:00
2016-07-01 21:59:39 +02:00
/**
2016-07-03 21:54:35 +02:00
* Function that can be used after the backup
2019-01-25 21:39:18 +01:00
*
2016-07-03 21:54:35 +02:00
* @return void
*/
2015-08-18 17:02:37 +02:00
abstract protected function postBuild();
2016-07-03 21:54:35 +02:00
2016-07-01 21:59:39 +02:00
/**
2016-07-03 21:54:35 +02:00
* Mandatory function doing the backup
2019-01-25 21:39:18 +01:00
*
2016-07-03 21:54:35 +02:00
* @return void
*/
2015-08-18 17:02:37 +02:00
abstract protected function build();
2016-07-03 21:54:35 +02:00
2016-07-01 21:59:39 +02:00
/**
2016-07-03 21:54:35 +02:00
* Check if the backup is valid
*
* @return bool
*/
2015-08-18 17:02:37 +02:00
abstract public function isValid();
/**
2016-07-03 21:54:35 +02:00
* Apply options
2019-01-25 21:39:18 +01:00
*
2015-08-18 17:02:37 +02:00
* @return $this
*/
protected function applyOptions()
{
// TODO isValid here ?
2016-07-03 21:54:35 +02:00
foreach ($this->options as $name => $value) {
2015-08-18 17:02:37 +02:00
$method = sprintf('setOption%s', ucfirst($name));
if (method_exists($this, $method)) {
2019-01-25 22:19:50 +01:00
$this->$method($value);
2015-08-18 17:02:37 +02:00
}
}
return $this;
}
/**
* Zip every backup files and streams into one zip
* Enabled via options
*
* @return $this
* @throws \Exception
*/
protected function setOptionZip()
{
$zip = new \ZipArchive();
2016-07-03 21:54:35 +02:00
// Zip name
$zip_name = !empty($this->options['name']) ? $this->options['name'] : time();
$zip_name = sprintf('%s.zip', $zip_name);
if (touch(TEMP_DIR . $zip_name) === false) {
2015-08-18 17:02:37 +02:00
throw new \Exception('Backup::Zip::Permission denied.');
}
2019-01-25 22:19:50 +01:00
if ($zip->open(TEMP_DIR . $zip_name, \ZipArchive::OVERWRITE) === true) {
2016-07-03 21:54:35 +02:00
foreach ($this->files_to_backup as $file => $name) {
2015-08-18 17:02:37 +02:00
$zip->addFile($file, $name); // Adding files into zip
}
2016-07-03 21:54:35 +02:00
foreach ($this->streams_to_backup as $file => $name) {
2015-08-18 17:02:37 +02:00
$zip->addFromString($file, $name); // Adding streams into zip
}
$zip->close();
} else {
throw new \Exception('Backup::Zip::Can\'t zip the given backup.');
}
2016-07-03 21:54:35 +02:00
$this->files_to_backup = [TEMP_DIR . $zip_name => $zip_name];
$this->streams_to_backup = [];
2015-08-18 17:02:37 +02:00
return $this;
}
2015-07-03 00:00:30 +02:00
/**
* Add the current date with the given format into the files names
*
2016-07-03 21:54:35 +02:00
* @param string $format date format
2015-07-03 00:00:30 +02:00
*
* @return $this
*/
2015-08-18 17:02:37 +02:00
protected function setOptionAddDate($format = 'Ymd')
2015-07-03 00:00:30 +02:00
{
2015-08-18 17:02:37 +02:00
if ($format === true) {
$format = 'Ymd';
}
2015-07-03 00:00:30 +02:00
$tmpFiles = array();
2016-07-03 21:54:35 +02:00
foreach ($this->files_to_backup as $file => $name) {
2015-07-03 00:00:30 +02:00
$nameA = explode('.', $name);
$nameA[] = end($nameA);
2015-07-12 20:34:54 +02:00
$nameA[count($nameA) - 2] = date($format);
2015-07-03 00:00:30 +02:00
$name = implode('.', $nameA);
$tmpFiles[$file] = $name;
}
2016-07-03 21:54:35 +02:00
$this->files_to_backup = $tmpFiles;
2015-07-03 00:00:30 +02:00
$tmpStream = array();
2016-07-03 21:54:35 +02:00
foreach ($this->streams_to_backup as $name => $stream) {
2015-07-03 00:00:30 +02:00
$tmpStream[$name . '-' . date($format)] = $stream;
}
2016-07-03 21:54:35 +02:00
$this->streams_to_backup = $tmpStream;
2015-07-03 00:00:30 +02:00
return $this;
}
/**
* Add the current time with the given format into the files names
*
2016-07-03 21:54:35 +02:00
* @param string $format time format
2015-07-03 00:00:30 +02:00
*
* @return $this
*/
2015-08-18 17:02:37 +02:00
protected function setOptionAddTime($format = 'his')
2015-07-03 00:00:30 +02:00
{
2015-08-18 17:02:37 +02:00
if ($format === true) {
$format = 'his';
}
2015-07-03 00:00:30 +02:00
$tmpFiles = array();
2016-07-03 21:54:35 +02:00
foreach ($this->files_to_backup as $file => $name) {
2015-07-03 00:00:30 +02:00
$nameA = explode('.', $name);
$nameA[] = end($nameA);
2015-07-12 20:34:54 +02:00
$nameA[count($nameA) - 2] = date($format);
2015-07-03 00:00:30 +02:00
$name = implode('.', $nameA);
$tmpFiles[$file] = $name;
}
2016-07-03 21:54:35 +02:00
$this->files_to_backup = $tmpFiles;
2015-07-03 00:00:30 +02:00
$tmpStream = array();
2016-07-03 21:54:35 +02:00
foreach ($this->streams_to_backup as $name => $stream) {
2015-07-03 00:00:30 +02:00
$tmpStream[$name . '-' . date($format)] = $stream;
}
2016-07-03 21:54:35 +02:00
$this->streams_to_backup = $tmpStream;
2015-07-03 00:00:30 +02:00
return $this;
}
/**
2016-07-03 21:54:35 +02:00
* Set option name
2019-01-25 21:39:18 +01:00
*
2016-07-03 21:54:35 +02:00
* @param mixed $name option's name
*
2015-08-18 17:02:37 +02:00
* @throws \Exception
2016-07-03 21:54:35 +02:00
*
2016-07-01 21:59:39 +02:00
* @SuppressWarnings("unused")
2015-07-03 00:00:30 +02:00
*/
2015-08-18 17:02:37 +02:00
protected function setOptionName($name)
2015-07-12 20:34:54 +02:00
{
2015-08-18 17:02:37 +02:00
if (empty($this->options['zip'])) {
throw new \Exception('name option is for zip only.');
2015-07-03 00:00:30 +02:00
}
2015-07-12 20:34:54 +02:00
}
2015-07-03 00:00:30 +02:00
}