SFTP transport added

This commit is contained in:
Shikiryu 2015-08-17 13:16:28 +02:00
parent 085f9ae152
commit 9c74a9601b
4 changed files with 91 additions and 4 deletions

View File

@ -16,10 +16,11 @@ class Scenario {
public static function register() 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) if (strpos($class, __NAMESPACE__.'\\') === 0)
{ {
@ -27,7 +28,7 @@ class Scenario {
$translated_path = str_replace('\\', '/', $relative_NS); $translated_path = str_replace('\\', '/', $relative_NS);
require __DIR__ . '/' . $translated_path . '.php'; require __DIR__ . '/' . $translated_path . '.php';
} }
} }*/
/** /**
* @param array $scenario * @param array $scenario

View File

@ -0,0 +1,67 @@
<?php
namespace Shikiryu\Backup\Transport;
use phpseclib\Net\SFTP as LibSFTP;
class Sftp extends TransportAbstract
{
protected $host;
protected $port = 22;
protected $login;
protected $password;
protected $folder;
private $connection;
/**
* @param \Shikiryu\Backup\Backup\BackupAbstract $backup
* @param array $config
* @throws \Exception
*/
public function __construct($backup, $config)
{
parent::__construct($backup, $config);
$this->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;
}
}

View File

@ -2,7 +2,7 @@
namespace Shikiryu\Backup\Backup; namespace Shikiryu\Backup\Backup;
class BackupAbstract abstract class BackupAbstract
{ {
protected $_filesToBackup; protected $_filesToBackup;

19
composer.json Normal file
View File

@ -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/"]
}
}