mirror of
https://github.com/Chouchen/Shikiryu_Backup.git
synced 2021-06-30 16:02:14 +02:00
Added options and Dropbox
This commit is contained in:
parent
bf551dd6ff
commit
41391be79c
@ -40,16 +40,20 @@ class Scenario {
|
||||
$this->transport = TransportFactory::build($this->backup, $scenario['transport']);
|
||||
}
|
||||
|
||||
/**
|
||||
* check if backup is valid and then launch the transfer
|
||||
*
|
||||
* @see BackupAbstract::isValid
|
||||
* @see TransportAbstract::send
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
try {
|
||||
if ($this->backup->isValid()) {
|
||||
$this->transport->send();
|
||||
} else {
|
||||
throw new \Exception("Backup configuration is invalid.");
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
if ($this->backup->isValid()) {
|
||||
$this->transport->send();
|
||||
} else {
|
||||
throw new \Exception("Backup configuration is invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
|
48
app/code/Transport/Dropbox.php
Normal file
48
app/code/Transport/Dropbox.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Backup\Transport;
|
||||
|
||||
use Dropbox\Client;
|
||||
use Shikiryu\Backup\Transport\TransportAbstract;
|
||||
|
||||
class Dropbox extends TransportAbstract
|
||||
{
|
||||
|
||||
private $dropbox;
|
||||
|
||||
protected $token = '';
|
||||
protected $app = '';
|
||||
protected $folder = '';
|
||||
|
||||
public function __construct($backup, $config)
|
||||
{
|
||||
parent::__construct($backup, $config);
|
||||
$this->dropbox = new Client($this->token, $this->app);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
$sent = true;
|
||||
$files = $this->backup->getFilesToBackup();
|
||||
foreach ($files as $file => $name) {
|
||||
$file = fopen($file, 'r');
|
||||
$upload = $this->dropbox->uploadFile($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $file);
|
||||
if (!$upload) {
|
||||
$sent = false;
|
||||
echo 'DROPBOX upload manquée de '.$file.' vers '.$this->folder.$name;
|
||||
}
|
||||
}
|
||||
$streams = $this->backup->getStreamsToBackup();
|
||||
foreach ($streams as $stream => $name) {
|
||||
$upload = $this->dropbox->uploadFileFromString($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $stream);
|
||||
if (!$upload) {
|
||||
$sent = false;
|
||||
echo 'DROPBOX upload manquée de '.$file.' vers '.$this->folder.$name;
|
||||
}
|
||||
}
|
||||
return $sent;
|
||||
}
|
||||
}
|
@ -5,19 +5,25 @@ namespace Shikiryu\Backup\Backup;
|
||||
abstract class BackupAbstract
|
||||
{
|
||||
|
||||
protected $_filesToBackup;
|
||||
protected $_streamsToBackup;
|
||||
protected $options;
|
||||
/** @var string[] */
|
||||
protected $_filesToBackup = [];
|
||||
/** @var string[] */
|
||||
protected $_streamsToBackup = [];
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
*/
|
||||
function __construct($config = array())
|
||||
{
|
||||
|
||||
$this->options = !empty($config['options']) ? $config['options'] : [];
|
||||
unset($config['options']);
|
||||
|
||||
foreach ($config as $name => $value) {
|
||||
$this->$name = $value;
|
||||
}
|
||||
$this->_filesToBackup = array();
|
||||
$this->_streamsToBackup = array();
|
||||
$this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -47,6 +53,93 @@ abstract class BackupAbstract
|
||||
return $this->_streamsToBackup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if all files got the minimum given size.
|
||||
*
|
||||
* @param int $fs
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function checkMinimumFilesize($fs)
|
||||
{
|
||||
foreach ($this->_filesToBackup as $file => $name) {
|
||||
if (filesize($file) < $fs) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
foreach ($this->_streamsToBackup as $name => $file) {
|
||||
if (mb_strlen($file, 'utf-8') < $fs) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function init()
|
||||
{
|
||||
$this->preBuild();
|
||||
$this->build();
|
||||
$this->postBuild();
|
||||
$this->applyOptions();
|
||||
}
|
||||
|
||||
abstract protected function preBuild();
|
||||
abstract protected function postBuild();
|
||||
abstract protected function build();
|
||||
abstract public function isValid();
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
protected function applyOptions()
|
||||
{
|
||||
// TODO isValid here ?
|
||||
foreach($this->options as $name => $value)
|
||||
{
|
||||
$method = sprintf('setOption%s', ucfirst($name));
|
||||
if (method_exists($this, $method)) {
|
||||
call_user_func([$this, $method], $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zip every backup files and streams into one zip
|
||||
* Enabled via options
|
||||
*
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function setOptionZip()
|
||||
{
|
||||
$zip = new \ZipArchive();
|
||||
$zip_name = sprintf('%s.zip', (!empty($this->options['name']) ? $this->options['name'] : time())); // Zip name
|
||||
if (touch(TEMP_DIR.$zip_name) === false) {
|
||||
throw new \Exception('Backup::Zip::Permission denied.');
|
||||
}
|
||||
if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::OVERWRITE)==TRUE) {
|
||||
foreach($this->_filesToBackup as $file => $name)
|
||||
{
|
||||
$zip->addFile($file, $name); // Adding files into zip
|
||||
}
|
||||
|
||||
foreach($this->_streamsToBackup as $file => $name)
|
||||
{
|
||||
$zip->addFromString($file, $name); // Adding streams into zip
|
||||
}
|
||||
$zip->close();
|
||||
} else {
|
||||
throw new \Exception('Backup::Zip::Can\'t zip the given backup.');
|
||||
}
|
||||
|
||||
$this->_filesToBackup = [TEMP_DIR.$zip_name => $zip_name];
|
||||
$this->_streamsToBackup = [];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the current date with the given format into the files names
|
||||
*
|
||||
@ -54,8 +147,11 @@ abstract class BackupAbstract
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addDate($format = 'Ymd')
|
||||
protected function setOptionAddDate($format = 'Ymd')
|
||||
{
|
||||
if ($format === true) {
|
||||
$format = 'Ymd';
|
||||
}
|
||||
$tmpFiles = array();
|
||||
foreach ($this->_filesToBackup as $file => $name) {
|
||||
$nameA = explode('.', $name);
|
||||
@ -82,8 +178,11 @@ abstract class BackupAbstract
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addTime($format = 'his')
|
||||
protected function setOptionAddTime($format = 'his')
|
||||
{
|
||||
if ($format === true) {
|
||||
$format = 'his';
|
||||
}
|
||||
$tmpFiles = array();
|
||||
foreach ($this->_filesToBackup as $file => $name) {
|
||||
$nameA = explode('.', $name);
|
||||
@ -103,38 +202,17 @@ abstract class BackupAbstract
|
||||
return $this;
|
||||
}
|
||||
|
||||
function backupToDropbox()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if all files got the minimum given size.
|
||||
*
|
||||
* @param int $fs
|
||||
*
|
||||
* @return bool
|
||||
* @param $name
|
||||
* @throws \Exception
|
||||
*/
|
||||
function checkMinimumFilesize($fs)
|
||||
protected function setOptionName($name)
|
||||
{
|
||||
foreach ($this->_filesToBackup as $file => $name) {
|
||||
if (filesize($file) < $fs) {
|
||||
return false;
|
||||
}
|
||||
if (empty($this->options['zip'])) {
|
||||
throw new \Exception('name option is for zip only.');
|
||||
}
|
||||
foreach ($this->_streamsToBackup as $name => $file) {
|
||||
if (mb_strlen($file, 'utf-8') < $fs) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function isValid();
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -13,12 +13,12 @@ class Files extends BackupAbstract
|
||||
if (!isset($config['files'])) {
|
||||
throw new \Exception('Files needs a "files" configuration.');
|
||||
}
|
||||
parent::__construct();
|
||||
$filesToBackup = $config['files'];
|
||||
if(!empty($filesToBackup) && is_array($filesToBackup)){
|
||||
$names = array_map("basename",$filesToBackup);
|
||||
$this->_filesToBackup = array_combine($filesToBackup,$names);
|
||||
}
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
public function isValid()
|
||||
@ -33,5 +33,19 @@ class Files extends BackupAbstract
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function preBuild()
|
||||
{
|
||||
// TODO: Implement preBuild() method.
|
||||
}
|
||||
|
||||
protected function postBuild()
|
||||
{
|
||||
// TODO: Implement postBuild() method.
|
||||
}
|
||||
|
||||
protected function build()
|
||||
{
|
||||
// TODO: Implement build() method.
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -8,8 +8,6 @@ class Folder extends BackupAbstract
|
||||
public function __construct(array $config = array())
|
||||
{
|
||||
parent::__construct($config);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -19,4 +17,19 @@ class Folder extends BackupAbstract
|
||||
{
|
||||
// TODO: Implement isValid() method.
|
||||
}
|
||||
|
||||
protected function preBuild()
|
||||
{
|
||||
// TODO: Implement preBuild() method.
|
||||
}
|
||||
|
||||
protected function postBuild()
|
||||
{
|
||||
// TODO: Implement postBuild() method.
|
||||
}
|
||||
|
||||
protected function build()
|
||||
{
|
||||
// TODO: Implement build() method.
|
||||
}
|
||||
}
|
@ -9,6 +9,10 @@ class Mysql extends BackupAbstract
|
||||
* @var $pdo \PDO
|
||||
*/
|
||||
private $pdo;
|
||||
|
||||
/*
|
||||
* from config
|
||||
*/
|
||||
protected $tables;
|
||||
protected $host;
|
||||
protected $db;
|
||||
@ -20,15 +24,13 @@ class Mysql extends BackupAbstract
|
||||
*/
|
||||
public function __construct(array $config = array()) {
|
||||
parent::__construct($config);
|
||||
$this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->db, $this->login, $this->pwd);
|
||||
empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $tables
|
||||
* @return $this|string
|
||||
*/
|
||||
public function fromTables($tables = array())
|
||||
private function fromTables($tables = array())
|
||||
{
|
||||
if(!empty($tables)) {
|
||||
$this->tables = $tables;
|
||||
@ -42,7 +44,7 @@ class Mysql extends BackupAbstract
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function everything() {
|
||||
private function everything() {
|
||||
$this->tables = array();
|
||||
foreach($this->pdo->query('SHOW TABLES') as $table) {
|
||||
$this->tables[] = $table;
|
||||
@ -97,6 +99,21 @@ class Mysql extends BackupAbstract
|
||||
{
|
||||
return !empty($this->_streamsToBackup);
|
||||
}
|
||||
|
||||
protected function preBuild()
|
||||
{
|
||||
$this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->db, $this->login, $this->pwd);
|
||||
}
|
||||
|
||||
protected function postBuild()
|
||||
{
|
||||
// TODO: Implement postBuild() method.
|
||||
}
|
||||
|
||||
protected function build()
|
||||
{
|
||||
empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -11,7 +11,8 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"phpseclib/phpseclib": ">=1.0.0"
|
||||
"phpseclib/phpseclib": ">=1.0.0",
|
||||
"dropbox/dropbox-sdk": "~1.1"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": ["app/code/", "vendor/"]
|
||||
|
Loading…
Reference in New Issue
Block a user