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.
 
 
 
 
 
 

157 lines
4.9 KiB

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of BackupUSB
*
* @author Victor
*
* -> findmnt | grep -i '/dev/sd'
* -> vim /etc/fstab
* -> /dev/sdb1 /mnt/USB vfat user,noauto,rw 0 0
*
*/
final class BackupUSB extends AbstractLocalLan {
protected $_diretorioUsbLocal = "/mnt/USB/";
protected $_usbMounted = false;
public function __destruct() {
if ($this->_usbMounted) {
$this->Debug("Desmontando unidade USB em {$this->_diretorioUsbLocal}...");
$cmd = sprintf("umount -f %s", $this->_diretorioUsbLocal);
$umount = ssh2_exec($this->_handler, $cmd);
if (!$umount) {
$this->Error("Impossível desmontar a unidade USB em {$this->_diretorioUsbLocal}.");
} else {
$this->Debug("Unidade USB em {$this->_diretorioUsbLocal} desmontada com sucesso.");
}
ssh2_exec($this->_handler, "exit");
$this->Debug(self::$BACKUP_FINALIZADO);
} else {
$this->Debug("Nenhuma unidade USB foi montada");
}
$this->Debug(self::$BACKUP_FINALIZADO);
}
/**
* Conecta ao servidor de destino do backup
*
* @param BackupInfo $backup
*
* @return bool
*/
function Connect(BackupInfo $backup) {
$this->backup = $backup;
$sshConectado = $this->ConectaSSH();
if (!$sshConectado) {
$this->backup->hasError = true;
return false;
}
if (!file_exists($this->_diretorioUsbLocal)) {
$cmd = "mkdir -p {$this->_diretorioUsbLocal}";
$result = null;
$cmdStatus = ssh2_exec($this->_handler, $cmd);
if (!$cmdStatus) {
$this->Error("Não foi possível criar o diretório {$this->_diretorioUsbLocal}!");
return false;
}
} else {
if (!is_readable($this->_diretorioUsbLocal)) {
$this->Error("Sem permissão de leitura para o diretório {$this->_diretorioUsbLocal}");
return false;
} else {
if (!is_writable($this->_diretorioUsbLocal)) {
$this->Error("Sem permissão de escrita para o diretório {$this->_diretorioUsbLocal}");
return false;
}
}
}
// monta a unidade usb
$mountCmd = "mount /dev/sdb1 2>&1";
$stream = ssh2_exec($this->_handler, $mountCmd);
if (!$stream) {
$this->backup->hasError = true;
$this->Error("Não foi possível montar a unidade usb {$this->_diretorioUsbLocal}!");
return false;
}
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($stream, true);
$output = utf8_decode(stream_get_contents($stream));
$error = stream_get_contents($errorStream);
if (!empty($output)) {
if (StringClass::ContemTexto($output, "não existe") ||
StringClass::ContemTexto($output, "mount: can't find") ||
StringClass::ContemTexto($output, "already mounted")) {
$error = str_replace("\n", "", $output);
} else {
$this->Debug($output);
}
}
if (!empty($error)) {
$this->Error($error);
fclose($stream);
return false;
}
$this->_usbMounted = !$this->backup->hasError;
fclose($stream);
return !$this->backup->hasError;
}
public function Upload(array $dadosArquivo, $remoteFile) {
$localFile = $dadosArquivo['file'];
$hash = $dadosArquivo['hash'];
$remoteFilePath = sprintf("%s%s", $this->_diretorioUsbLocal, $remoteFile);
$cmd = sprintf("cp -u %s %s", $localFile, $remoteFilePath);
$stream = ssh2_exec($this->_handler, $cmd);
if (!$stream) {
$this->Error("Não foi possível executar o comando [{$cmd}]");
}
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
if (!empty($output)) {
$this->Debug($output);
}
$error = stream_get_contents($errorStream);
if (!empty($error)) {
$this->Error($error);
fclose($stream);
return false;
}
fclose($stream);
if (!file_exists($remoteFilePath)) {
$this->Error("Não foi possível gravar o arquivo na unidade USB!");
return false;
}
$this->Debug("Backup copiado para '{$remoteFilePath}'");
$this->unlink($localFile);
return $this->CheckHash($hash, $remoteFilePath);
}
}