From 41391be79cc419d473e88c091f0475038afabf0e Mon Sep 17 00:00:00 2001 From: Shikiryu Date: Tue, 18 Aug 2015 17:02:37 +0200 Subject: [PATCH] Added options and Dropbox --- app/code/Scenario.php | 20 ++-- app/code/Transport/Dropbox.php | 48 ++++++++++ app/code/backup/BackupAbstract.php | 142 ++++++++++++++++++++++------- app/code/backup/Files.php | 16 +++- app/code/backup/Folder.php | 17 +++- app/code/backup/Mysql.php | 25 ++++- composer.json | 3 +- 7 files changed, 223 insertions(+), 48 deletions(-) create mode 100644 app/code/Transport/Dropbox.php diff --git a/app/code/Scenario.php b/app/code/Scenario.php index abd7f64..d375aea 100644 --- a/app/code/Scenario.php +++ b/app/code/Scenario.php @@ -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."); } } diff --git a/app/code/Transport/Dropbox.php b/app/code/Transport/Dropbox.php new file mode 100644 index 0000000..a3ff8a7 --- /dev/null +++ b/app/code/Transport/Dropbox.php @@ -0,0 +1,48 @@ +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; + } +} \ No newline at end of file diff --git a/app/code/backup/BackupAbstract.php b/app/code/backup/BackupAbstract.php index cd9b1f9..f8e262e 100644 --- a/app/code/backup/BackupAbstract.php +++ b/app/code/backup/BackupAbstract.php @@ -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(); - } ?> diff --git a/app/code/backup/Files.php b/app/code/backup/Files.php index a12c377..ff01b2d 100644 --- a/app/code/backup/Files.php +++ b/app/code/backup/Files.php @@ -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. + } } ?> diff --git a/app/code/backup/Folder.php b/app/code/backup/Folder.php index 4870566..f68e8e0 100644 --- a/app/code/backup/Folder.php +++ b/app/code/backup/Folder.php @@ -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. + } } \ No newline at end of file diff --git a/app/code/backup/Mysql.php b/app/code/backup/Mysql.php index c389301..df8f8bf 100644 --- a/app/code/backup/Mysql.php +++ b/app/code/backup/Mysql.php @@ -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); + } } ?> diff --git a/composer.json b/composer.json index c85c666..3895545 100644 --- a/composer.json +++ b/composer.json @@ -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/"]