|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Description of BackupSFTP
|
|
|
|
|
*
|
|
|
|
|
* @author Victor
|
|
|
|
|
*
|
|
|
|
|
* pecl install -f ssh2
|
|
|
|
|
*/
|
|
|
|
|
final class BackupSFTP extends AbstractBackup {
|
|
|
|
|
|
|
|
|
|
private $_sftp;
|
|
|
|
|
|
|
|
|
|
public function __destruct() {
|
|
|
|
|
if (is_resource($this->_handler)) {
|
|
|
|
|
$this->Debug("Encerrando conex<EFBFBD>o com o host {$this->GetInfo('host')}");
|
|
|
|
|
ssh2_exec($this->_handler, "exit");
|
|
|
|
|
$this->Debug(self::$BACKUP_FINALIZADO);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Connect(BackupInfo $backup) {
|
|
|
|
|
$this->backup = $backup;
|
|
|
|
|
$sshConectado = $this->ConectaSSH();
|
|
|
|
|
if (!$sshConectado) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->_sftp = ssh2_sftp($this->_handler);
|
|
|
|
|
if (!$this->_sftp) {
|
|
|
|
|
$this->Error("N<EFBFBD>o foi poss<EFBFBD>vel inicializar o SFTP!");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->backup->dirAtual = $this->backup->VerBarraPath($this->backup->dir);
|
|
|
|
|
$this->Debug("Navegando at<EFBFBD> diret<EFBFBD>rio padr<EFBFBD>o {$this->GetInfo('dirAtual')}");
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Upload(array $dadosArquivo, $remoteFile) {
|
|
|
|
|
$localFile = $dadosArquivo['file'];
|
|
|
|
|
$hash = $dadosArquivo['hash'];
|
|
|
|
|
$arquivoCopiado = sprintf("%s%s%s", $this->GetInfo('host'), $this->GetInfo('dirAtual'), $remoteFile);
|
|
|
|
|
|
|
|
|
|
$debugMsg = sprintf("Iniciando upload do arquivo '%s' para '%s'...", $localFile, $arquivoCopiado);
|
|
|
|
|
$this->Debug($debugMsg);
|
|
|
|
|
if (!ssh2_scp_send($this->_handler, $localFile, $this->backup->dirAtual . $remoteFile, 0644)) {
|
|
|
|
|
$this->Error("N<EFBFBD>o foi poss<EFBFBD>vel gravar o arquivo remoto!");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->Debug("Upload conclu<EFBFBD>do com sucesso");
|
|
|
|
|
|
|
|
|
|
$this->unlink($localFile);
|
|
|
|
|
|
|
|
|
|
return $this->CheckHash($hash, $this->GetInfo('dirAtual') . $remoteFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Chdir($dir) {
|
|
|
|
|
$command = sprintf("cd %s", $dir);
|
|
|
|
|
$stream = ssh2_exec($this->_handler, $command);
|
|
|
|
|
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
|
|
|
|
|
stream_set_blocking($errorStream, true);
|
|
|
|
|
$erro = trim(stream_get_contents($errorStream));
|
|
|
|
|
|
|
|
|
|
if (!empty($erro)) {
|
|
|
|
|
$this->Error("N<EFBFBD>o foi poss<EFBFBD>vel acesar o diret<EFBFBD>rio destino! Erro: $erro.");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->backup->dirAtual = $this->backup->VerBarraPath($dir);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Mkdir($dir) {
|
|
|
|
|
$command = sprintf("%s", $dir);
|
|
|
|
|
if (!ssh2_sftp_mkdir($this->_sftp, $command)) {
|
|
|
|
|
$this->Error("N<EFBFBD>o foi poss<EFBFBD>vel criar o diret<EFBFBD>rio destino![SFTP]");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|