From 0952cf2db30420ee0c953a1ae8413de7b1aebacd Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Thu, 16 Apr 2020 17:57:14 +0200 Subject: [PATCH] :sparkles: Add backup FROM SFTP to local --- app/code/Transport/Folder.php | 24 ++++--- app/code/backup/BackupAbstract.php | 2 +- app/code/backup/Files.php | 13 ++-- app/code/backup/Folder.php | 2 + app/code/backup/IsDistantTrait.php | 18 +++++ app/code/backup/IsLocalTrait.php | 16 +++++ app/code/backup/Mysql.php | 5 +- app/code/backup/SFTP.php | 112 +++++++++++++++++++++++++++++ app/scenario/scenarii.json | 9 ++- 9 files changed, 184 insertions(+), 17 deletions(-) create mode 100644 app/code/backup/IsDistantTrait.php create mode 100644 app/code/backup/IsLocalTrait.php create mode 100644 app/code/backup/SFTP.php diff --git a/app/code/Transport/Folder.php b/app/code/Transport/Folder.php index 55b0f5b..9b23158 100644 --- a/app/code/Transport/Folder.php +++ b/app/code/Transport/Folder.php @@ -29,19 +29,25 @@ class Folder extends TransportAbstract */ public function send() { - foreach ($this->backup->getFilesToBackup() as $file => $name) { - if (copy($file, $this->folder . $name) === false) { - throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder)); - }; + if ($this->backup->isDistant()) { + $this->backup = $this->backup->retrieve(); } - foreach ($this->backup->getStreamsToBackup() as $name => $file) { - if (substr_count($name, '.') + 1 < 2) { - $name = 'backup' . $name . '.txt'; + if ($this->backup->isLocal()) { + foreach ($this->backup->getFilesToBackup() as $file => $name) { + if (copy($file, $this->folder . $name) === false) { + throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder)); + } } - if (file_put_contents($this->folder . $name, $file) === false) { - throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder)); + foreach ($this->backup->getStreamsToBackup() as $name => $file) { + if (substr_count($name, '.') + 1 < 2) { + $name = 'backup' . $name . '.txt'; + } + if (file_put_contents($this->folder . $name, $file) === false) { + throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder)); + } } } + return true; } } diff --git a/app/code/backup/BackupAbstract.php b/app/code/backup/BackupAbstract.php index 95f1eb9..e759d9d 100644 --- a/app/code/backup/BackupAbstract.php +++ b/app/code/backup/BackupAbstract.php @@ -28,7 +28,7 @@ abstract class BackupAbstract * * @param array $config array of options and parameters */ - public function __construct($config = array()) + public function __construct($config = []) { $this->options = !empty($config['options']) ? $config['options'] : []; diff --git a/app/code/backup/Files.php b/app/code/backup/Files.php index 6f67d77..54cfb84 100644 --- a/app/code/backup/Files.php +++ b/app/code/backup/Files.php @@ -1,16 +1,21 @@ files_to_backup as $file => $name) { diff --git a/app/code/backup/Folder.php b/app/code/backup/Folder.php index 66561fb..a223fc2 100644 --- a/app/code/backup/Folder.php +++ b/app/code/backup/Folder.php @@ -4,6 +4,8 @@ namespace Shikiryu\Backup\Backup; class Folder extends BackupAbstract { + use IsLocalTrait; + public function __construct(array $config = array()) { parent::__construct($config); diff --git a/app/code/backup/IsDistantTrait.php b/app/code/backup/IsDistantTrait.php new file mode 100644 index 0000000..56e4fe4 --- /dev/null +++ b/app/code/backup/IsDistantTrait.php @@ -0,0 +1,18 @@ +tables = $tables; diff --git a/app/code/backup/SFTP.php b/app/code/backup/SFTP.php new file mode 100644 index 0000000..e27401d --- /dev/null +++ b/app/code/backup/SFTP.php @@ -0,0 +1,112 @@ +files_to_backup = array_combine($filesToBackup, $names); + } + + parent::__construct($config); + } + + /** + * @inheritDoc + */ + protected function preBuild() + { + // TODO: Implement preBuild() method. + } + + /** + * @inheritDoc + */ + protected function postBuild() + { + // TODO: Implement postBuild() method. + } + + /** + * @inheritDoc + */ + protected function build() + { + // TODO: Implement build() method. + } + + /** + * @inheritDoc + * @throws \Exception + */ + public function isValid() + { + if (empty($this->password) && empty($this->key)) { + return false; + } + define('NET_SSH2_LOGGING', SSH2::LOG_COMPLEX); + $this->connection = new LibSFTP($this->host, $this->port); + if (!empty($this->key)) { + $this->password = new RSA(); + $this->password->loadKey(file_get_contents($this->key)); + } + if (!$this->connection->login($this->login, $this->password)) { + throw new \Exception(sprintf('I can\'t connect to the SFTP %s', $this->host)); + } + + $this->connection->enableQuietMode(); + $this->connection->exec('whoami'); + + return $this->connection->getStdError() === '' && $this->connection->read() !== ''; + } + + /** + * @return Files + */ + public function retrieve() + { + $tmp_files = []; + foreach ($this->files_to_backup as $path => $name) { + $tmp_file = TEMP_DIR.$name; + $tmp_files[] = $tmp_file; + $this->connection->get($path, $tmp_file); + } + unset($tmp_file); + try { + $tmp_backup = new Files(['files' => $tmp_files]); + unset($tmp_files); + } catch (\Exception $e) { + echo $e->getMessage(); + } + + return $tmp_backup; + } +} diff --git a/app/scenario/scenarii.json b/app/scenario/scenarii.json index 9fa6932..76d19a1 100644 --- a/app/scenario/scenarii.json +++ b/app/scenario/scenarii.json @@ -14,6 +14,13 @@ "tables": [ "" ] + }, + "SFTP": { + "host" : "sftp.domain.com", + "port" : "22", + "login" : "login", + "password" : "password", + "folder" : "/folder" } }, "transport": { @@ -38,7 +45,7 @@ "folder" : "/folder" }, "Folder": { - + "folder" : "/folder" }, "Dropbox": { "token": "123456789123456789"