1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpseclib; |
5
|
|
|
use phpbu\App\Result; |
6
|
|
|
use phpbu\App\Backup\Sync; |
7
|
|
|
use phpbu\App\Backup\Target; |
8
|
|
|
use phpbu\App\Util\Arr; |
9
|
|
|
use phpbu\App\Util\Str; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Sftp sync |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
19
|
|
|
* @link http://phpbu.de/ |
20
|
|
|
* @since Class available since Release 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class Sftp extends Xtp implements Simulator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Check for required loaded libraries or extensions. |
26
|
|
|
* |
27
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
28
|
|
|
*/ |
29
|
|
|
protected function checkRequirements() |
30
|
|
|
{ |
31
|
|
|
if (!class_exists('\\phpseclib\\Net\\SFTP')) { |
32
|
|
|
throw new Exception('phpseclib not installed - use composer to install "phpseclib/phpseclib" version 2.x'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return implemented (*)TP protocol name. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
protected function getProtocolName() |
42
|
|
|
{ |
43
|
|
|
return 'SFTP'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* (non-PHPDoc) |
48
|
|
|
* |
49
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
50
|
|
|
* @param \phpbu\App\Backup\Target $target |
51
|
|
|
* @param \phpbu\App\Result $result |
52
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
53
|
|
|
*/ |
54
|
|
|
public function sync(Target $target, Result $result) |
55
|
|
|
{ |
56
|
|
|
// silence phpseclib |
57
|
|
|
$old = error_reporting(0); |
58
|
|
|
$sftp = new phpseclib\Net\SFTP($this->host); |
59
|
5 |
View Code Duplication |
if (!$sftp->login($this->user, $this->password)) { |
60
|
|
|
error_reporting($old); |
61
|
5 |
|
throw new Exception( |
62
|
|
|
sprintf( |
63
|
|
|
'authentication failed for %s@%s%s', |
64
|
5 |
|
$this->user, |
65
|
1 |
|
$this->host, |
66
|
|
|
empty($this->password) ? '' : ' with password ****' |
67
|
4 |
|
) |
68
|
1 |
|
); |
69
|
|
|
} |
70
|
3 |
|
error_reporting($old); |
71
|
2 |
|
|
72
|
|
|
$remoteFilename = $target->getFilename(); |
73
|
1 |
|
$localFile = $target->getPathname(); |
74
|
1 |
|
|
75
|
|
|
if ('' !== $this->remotePath) { |
76
|
|
|
$remoteDirs = explode('/', $this->remotePath); |
77
|
1 |
|
foreach ($remoteDirs as $dir) { |
78
|
1 |
|
if (!$sftp->is_dir($dir)) { |
79
|
1 |
|
$result->debug(sprintf('creating remote dir \'%s\'', $dir)); |
80
|
1 |
|
$sftp->mkdir($dir); |
81
|
1 |
|
} |
82
|
|
|
$result->debug(sprintf('change to remote dir \'%s\'', $dir)); |
83
|
|
|
$sftp->chdir($dir); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
$result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename)); |
87
|
|
|
$result->debug(sprintf('last error \'%s\'', $sftp->getLastSFTPError())); |
88
|
|
|
|
89
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
90
|
|
|
if (!$sftp->put($remoteFilename, $localFile, phpseclib\Net\SFTP::SOURCE_LOCAL_FILE)) { |
91
|
|
|
throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $sftp->getLastSFTPError())); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|