From 9c74a9601b975d1e4d90c4688fb1482beb25b735 Mon Sep 17 00:00:00 2001 From: Shikiryu Date: Mon, 17 Aug 2015 13:16:28 +0200 Subject: [PATCH] SFTP transport added --- app/code/Scenario.php | 7 ++-- app/code/Transport/SFTP.php | 67 ++++++++++++++++++++++++++++++ app/code/backup/BackupAbstract.php | 2 +- composer.json | 19 +++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 app/code/Transport/SFTP.php create mode 100644 composer.json diff --git a/app/code/Scenario.php b/app/code/Scenario.php index 55fbd8a..abd7f64 100644 --- a/app/code/Scenario.php +++ b/app/code/Scenario.php @@ -16,10 +16,11 @@ class Scenario { public static function register() { - spl_autoload_register(array(__CLASS__, 'autoload')); + include __DIR__.'/../../vendor/autoload.php'; + //spl_autoload_register(array(__CLASS__, 'autoload')); } - public static function autoload($class) + /*public static function autoload($class) { if (strpos($class, __NAMESPACE__.'\\') === 0) { @@ -27,7 +28,7 @@ class Scenario { $translated_path = str_replace('\\', '/', $relative_NS); require __DIR__ . '/' . $translated_path . '.php'; } - } + }*/ /** * @param array $scenario diff --git a/app/code/Transport/SFTP.php b/app/code/Transport/SFTP.php new file mode 100644 index 0000000..0a444a0 --- /dev/null +++ b/app/code/Transport/SFTP.php @@ -0,0 +1,67 @@ +connection = new LibSFTP($this->host, $this->port); + if (!$this->connection->login($this->login, $this->password)) { + throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host)); + } + } + + /** + * @return bool + * @throws \Exception + */ + public function send() + { + $sent = true; + $files = $this->backup->getFilesToBackup(); + if (!empty($files)){ + foreach ($files as $file => $name) { + $upload = $this->connection->put($this->folder.'/'.$name, $file, LibSFTP::SOURCE_LOCAL_FILE); + if (!$upload) { + $sent = false; + echo 'SFTP upload manquée de '.$file.' vers '.$this->folder.$name; + } + } + } + + $streams = $this->backup->getStreamsToBackup(); + if (!empty($streams)){ + foreach ($streams as $name => $stream) { + $upload = $this->connection->put($this->folder.'/'.$name, $stream); + if (!$upload) { + echo 'SFTP upload manquée de '.$name.' vers '.$this->folder.$name; + $sent = false; + } + } + } + + if (!$sent) { + throw new \Exception('At least an upload didnt work.'); + } + return $sent; + } +} \ No newline at end of file diff --git a/app/code/backup/BackupAbstract.php b/app/code/backup/BackupAbstract.php index 2d5884a..cd9b1f9 100644 --- a/app/code/backup/BackupAbstract.php +++ b/app/code/backup/BackupAbstract.php @@ -2,7 +2,7 @@ namespace Shikiryu\Backup\Backup; -class BackupAbstract +abstract class BackupAbstract { protected $_filesToBackup; diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c85c666 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "shikiryu/backup", + "description": "Backup script for limited shared hosting", + "version": "0.0.1", + "authors": [ + { + "name": "Shikiryu", + "email": "backup@desmidt.fr", + "homepage": "http://shikiryu.com", + "role": "Developer" + } + ], + "require": { + "phpseclib/phpseclib": ">=1.0.0" + }, + "autoload": { + "classmap": ["app/code/", "vendor/"] + } +}