You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

88 lines
2.6 KiB

<?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ã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ão foi possível inicializar o SFTP!");
return false;
}
$this->backup->dirAtual = $this->backup->VerBarraPath($this->backup->dir);
$this->Debug("Navegando até diretório padrã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ão foi possível gravar o arquivo remoto!");
return false;
}
$this->Debug("Upload concluí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ão foi possível acesar o diretó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ão foi possível criar o diretório destino![SFTP]");
return false;
}
return true;
}
}