|
|
|
|
<?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 BackupRede
|
|
|
|
|
*
|
|
|
|
|
* @author Victor
|
|
|
|
|
*
|
|
|
|
|
* @property BackupInfo $backup
|
|
|
|
|
*/
|
|
|
|
|
final class BackupRede extends AbstractLocalLan {
|
|
|
|
|
|
|
|
|
|
protected $_diretorioRedeLocal = "/mnt/bk_simples/";
|
|
|
|
|
private $_networkMounted = false;
|
|
|
|
|
|
|
|
|
|
public function __destruct() {
|
|
|
|
|
if ($this->_networkMounted) {
|
|
|
|
|
$this->Debug("Desconectando do host '{$this->GetInfo('host')}'");
|
|
|
|
|
$this->sys(sprintf("umount -f %s", $this->_diretorioRedeLocal));
|
|
|
|
|
$this->Debug("Desconectado do host '{$this->GetInfo('host')}'");
|
|
|
|
|
} else {
|
|
|
|
|
$this->Debug("Nenhum mapeameto de rede ativo para desmontar");
|
|
|
|
|
}
|
|
|
|
|
$this->Debug(self::$BACKUP_FINALIZADO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Connect(BackupInfo $backup) {
|
|
|
|
|
$this->backup = $backup;
|
|
|
|
|
$this->Debug("Conectando ao host '{$this->backup->host}' na rede...");
|
|
|
|
|
$host = str_replace("\\", "/", $this->backup->host . $this->backup->dir);
|
|
|
|
|
$aut = $this->backup->anon ? "guest" : sprintf("username=%s,pass=%s", $this->backup->user, $this->backup->pass);
|
|
|
|
|
if (!file_exists($this->_diretorioRedeLocal)) {
|
|
|
|
|
$this->Debug("'{$this->_diretorioRedeLocal}' n<EFBFBD>o existe. Criando diret<EFBFBD>rio...");
|
|
|
|
|
$this->sys(sprintf("mkdir -o %s", $this->_diretorioRedeLocal));
|
|
|
|
|
if (!file_exists($this->_diretorioRedeLocal)) {
|
|
|
|
|
$this->Error("N<EFBFBD>o foi poss<EFBFBD>vel criar o diret<EFBFBD>rio '{$this->_diretorioRedeLocal}' para mapeamento da rede");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->Debug("Diret<EFBFBD>rio criado");
|
|
|
|
|
}
|
|
|
|
|
if (!is_readable($this->_diretorioRedeLocal)) {
|
|
|
|
|
$this->Error("Sem permiss<EFBFBD>o de leitura para o diret<EFBFBD>rio {$this->_diretorioRedeLocal}");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
if (!is_writable($this->_diretorioRedeLocal)) {
|
|
|
|
|
$this->Error("Sem permiss<EFBFBD>o de escrita para o diret<EFBFBD>rio {$this->_diretorioRedeLocal}");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mount -t cifs -o username=user,pass=SimpleS_root //192.168.115.139/Downloads/ /mnt/bk_simples/
|
|
|
|
|
$cmd = sprintf("mount -t cifs -o %s %s %s", $aut, $host, $this->_diretorioRedeLocal);
|
|
|
|
|
$this->Debug("Montando mapeamento de unidade do host na rede...");
|
|
|
|
|
|
|
|
|
|
$output = $this->sys($cmd);
|
|
|
|
|
if (is_null($output)) {
|
|
|
|
|
$this->Debug("N<EFBFBD>o foi poss<EFBFBD>vel conectar ao host");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (StringClass::ContemTexto($output, "exclusiva de root")) {
|
|
|
|
|
$this->Error($output);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->Debug("Mapeamento conclu<EFBFBD>do com sucesso");
|
|
|
|
|
$this->backup->dirAtual = $host;
|
|
|
|
|
$this->_networkMounted = true;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Upload(array $dadosArquivo, $remoteFile) {
|
|
|
|
|
$localFile = $dadosArquivo['file'];
|
|
|
|
|
$hash = $dadosArquivo['hash'];
|
|
|
|
|
$arquivoCopiado = sprintf("%s%s", $this->_diretorioRedeLocal, $remoteFile);
|
|
|
|
|
|
|
|
|
|
$cmd = sprintf("cp -u %s %s", $localFile, $arquivoCopiado);
|
|
|
|
|
$this->Debug("Copiando arquivos de backup para o host na rede...");
|
|
|
|
|
$result = $this->sys($cmd);
|
|
|
|
|
|
|
|
|
|
if (!file_exists($arquivoCopiado)) {
|
|
|
|
|
$this->Error("N<EFBFBD>o foi poss<EFBFBD>vel gravar o arquivo remoto!\n" . $cmd);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$remoto = str_replace("\\", "/", $this->backup->host . $this->backup->dir . $remoteFile);
|
|
|
|
|
$this->Debug("Backup copiado para [$remoto]");
|
|
|
|
|
|
|
|
|
|
//apaga arquivo local
|
|
|
|
|
$this->unlink($localFile);
|
|
|
|
|
|
|
|
|
|
return $this->CheckHash($hash, $arquivoCopiado);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|