Shikiryu_Backup/app/code/Transport/SFTP.php

72 lines
1.9 KiB
PHP
Raw Normal View History

2015-08-17 13:16:28 +02:00
<?php
namespace Shikiryu\Backup\Transport;
use phpseclib\Net\SFTP as LibSFTP;
class Sftp extends TransportAbstract
{
2016-07-01 21:59:39 +02:00
/** @var string */
2015-08-17 13:16:28 +02:00
protected $host;
2016-07-01 21:59:39 +02:00
/** @var int */
2015-08-17 13:16:28 +02:00
protected $port = 22;
2016-07-01 21:59:39 +02:00
/** @var string */
2015-08-17 13:16:28 +02:00
protected $login;
2016-07-01 21:59:39 +02:00
/** @var string */
2015-08-17 13:16:28 +02:00
protected $password;
2016-07-01 21:59:39 +02:00
/** @var string */
2015-08-17 13:16:28 +02:00
protected $folder;
2016-07-01 21:59:39 +02:00
/** @var LibSFTP */
2015-08-17 13:16:28 +02:00
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
2016-07-01 21:59:39 +02:00
*
2015-08-17 13:16:28 +02:00
* @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<71>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<71>e de '.$name.' vers '.$this->folder.$name;
$sent = false;
}
}
}
if (!$sent) {
throw new \Exception('At least an upload didnt work.');
}
return $sent;
}
}