/**
* (non-PHPDoc)
*
* @see \phpbu\App\Backup\Sync::sync()
* @param \phpbu\App\Backup\Target $target
* @param \phpbu\App\Result $result
* @throws \phpbu\App\Backup\Sync\Exception
*/
public function sync(Target $target, Result $result)
{
// silence phpseclib
$old = error_reporting(0);
$sftp = new phpseclib\Net\SFTP($this->host);
if (!$sftp->login($this->user, $this->password)) {
error_reporting($old);
throw new Exception(sprintf('authentication failed for %s@%s%s', $this->user, $this->host, empty($this->password) ? '' : ' with password ****'));
}
error_reporting($old);
$remoteFilename = $target->getFilename();
$localFile = $target->getPathname();
if ('' !== $this->remotePath) {
$remoteDirs = explode('/', $this->remotePath);
foreach ($remoteDirs as $dir) {
if (!$sftp->is_dir($dir)) {
$result->debug(sprintf('creating remote dir \'%s\'', $dir));
$sftp->mkdir($dir);
}
$result->debug(sprintf('change to remote dir \'%s\'', $dir));
$sftp->chdir($dir);
}
}
$result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename));
$result->debug(sprintf('last error \'%s\'', $sftp->getLastSFTPError()));
/** @noinspection PhpInternalEntityUsedInspection */
if (!$sftp->put($remoteFilename, $localFile, phpseclib\Net\SFTP::SOURCE_LOCAL_FILE)) {
throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $sftp->getLastSFTPError()));
}
}