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-07-10 19:07:14 +02:00
|
|
|
class BackupAbstract
|
2015-07-03 00:00:30 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
protected $_filesToBackup;
|
|
|
|
protected $_streamsToBackup;
|
|
|
|
|
|
|
|
/**
|
2015-07-12 20:34:54 +02:00
|
|
|
* @param array $config
|
2015-07-03 00:00:30 +02:00
|
|
|
*/
|
2015-07-12 20:34:54 +02:00
|
|
|
function __construct($config = array())
|
2015-07-03 00:00:30 +02:00
|
|
|
{
|
2015-07-12 20:34:54 +02:00
|
|
|
foreach ($config as $name => $value) {
|
|
|
|
$this->$name = $value;
|
|
|
|
}
|
|
|
|
$this->_filesToBackup = array();
|
2015-07-03 00:00:30 +02:00
|
|
|
$this->_streamsToBackup = array();
|
|
|
|
}
|
|
|
|
|
2015-07-12 20:34:54 +02:00
|
|
|
/**
|
|
|
|
* Magic setter method
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function __set($name, $value)
|
|
|
|
{
|
|
|
|
$this->$name = $value;
|
|
|
|
}
|
|
|
|
|
2015-07-09 00:46:17 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFilesToBackup()
|
|
|
|
{
|
|
|
|
return $this->_filesToBackup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getStreamsToBackup()
|
|
|
|
{
|
|
|
|
return $this->_streamsToBackup;
|
|
|
|
}
|
|
|
|
|
2015-07-03 00:00:30 +02:00
|
|
|
/**
|
|
|
|
* Add the current date with the given format into the files names
|
|
|
|
*
|
|
|
|
* @param string $format
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
function addDate($format = 'Ymd')
|
|
|
|
{
|
|
|
|
$tmpFiles = array();
|
2015-07-12 20:34:54 +02:00
|
|
|
foreach ($this->_filesToBackup 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;
|
|
|
|
}
|
|
|
|
$this->_filesToBackup = $tmpFiles;
|
|
|
|
|
|
|
|
$tmpStream = array();
|
2015-07-12 20:34:54 +02:00
|
|
|
foreach ($this->_streamsToBackup as $name => $stream) {
|
2015-07-03 00:00:30 +02:00
|
|
|
$tmpStream[$name . '-' . date($format)] = $stream;
|
|
|
|
}
|
|
|
|
$this->_streamsToBackup = $tmpStream;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the current time with the given format into the files names
|
|
|
|
*
|
|
|
|
* @param string $format
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
function addTime($format = 'his')
|
|
|
|
{
|
|
|
|
$tmpFiles = array();
|
2015-07-12 20:34:54 +02:00
|
|
|
foreach ($this->_filesToBackup 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;
|
|
|
|
}
|
|
|
|
$this->_filesToBackup = $tmpFiles;
|
|
|
|
|
|
|
|
$tmpStream = array();
|
2015-07-12 20:34:54 +02:00
|
|
|
foreach ($this->_streamsToBackup as $name => $stream) {
|
2015-07-03 00:00:30 +02:00
|
|
|
$tmpStream[$name . '-' . date($format)] = $stream;
|
|
|
|
}
|
|
|
|
$this->_streamsToBackup = $tmpStream;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function backupToDropbox()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-09 00:46:17 +02:00
|
|
|
/**
|
|
|
|
* @param $folder
|
|
|
|
*/
|
2015-07-03 00:00:30 +02:00
|
|
|
function backupToFolder($folder)
|
|
|
|
{
|
|
|
|
if (!empty($folder)) {
|
2015-07-12 20:34:54 +02:00
|
|
|
$folder = sprintf('%s/', rtrim($folder, '/'));
|
2015-07-03 00:00:30 +02:00
|
|
|
}
|
|
|
|
// if($folder != '')
|
|
|
|
// {
|
|
|
|
// if(substr($folder, 0, -1) != '/')
|
|
|
|
// $folder .= '/';
|
|
|
|
// }
|
2015-07-12 20:34:54 +02:00
|
|
|
foreach ($this->_filesToBackup as $file => $name) {
|
2015-07-03 00:00:30 +02:00
|
|
|
copy($file, $folder . $name);
|
|
|
|
}
|
2015-07-12 20:34:54 +02:00
|
|
|
foreach ($this->_streamsToBackup as $name => $file) {
|
2015-07-03 00:00:30 +02:00
|
|
|
if (count(explode('.', $name)) < 2) {
|
|
|
|
$name = 'backup' . $name . '.txt';
|
|
|
|
}
|
|
|
|
file_put_contents($folder . $name, $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if all files got the minimum given size.
|
|
|
|
*
|
|
|
|
* @param int $fs
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-12 20:34:54 +02:00
|
|
|
function checkMinimumFilesize($fs)
|
|
|
|
{
|
|
|
|
foreach ($this->_filesToBackup as $file => $name) {
|
|
|
|
if (filesize($file) < $fs) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-03 00:00:30 +02:00
|
|
|
}
|
2015-07-12 20:34:54 +02:00
|
|
|
foreach ($this->_streamsToBackup as $name => $file) {
|
2015-07-03 00:00:30 +02:00
|
|
|
if (mb_strlen($file, 'utf-8') < $fs) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-07-12 20:34:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValid()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-03 00:00:30 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|