forked from SimplesIP/pabx-app
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.
193 lines
6.7 KiB
193 lines
6.7 KiB
<?php |
|
|
|
/** |
|
* Classe BackupFile |
|
* |
|
* A classe realiza a copia dos arquivos selecionados para uma pasta descrita |
|
* e monta uma compactacao. |
|
* |
|
* @author Lucas Awade |
|
* @version 1.0.0 |
|
* @date 28/01/2020 |
|
*/ |
|
class BackupFile extends Backup { |
|
|
|
/** @directory */ |
|
private $dir; |
|
private $folder; |
|
|
|
/** @folderBackup */ |
|
private $name; |
|
|
|
/** @files */ |
|
private $files = array(); |
|
private $compact; |
|
private $formats = array(); |
|
|
|
const CONF_DIR_TEMP_FOLDER = "/tmp"; |
|
|
|
/** |
|
* $dir = Diretorio onde armazena os arquivos para serem feito o backup; |
|
* $name = Nome do backup para ser gerado; |
|
* |
|
* @param string $dir |
|
* @param string $name |
|
*/ |
|
function __construct($dir, $name) { |
|
$this->dir = $this->strslash($dir); |
|
$this->name = $name; |
|
$this->setFolder($name); |
|
} |
|
|
|
/** |
|
* Gera os arquivos na pasta de backup |
|
*/ |
|
public function generate() { |
|
$this->log()->debug("Criando arquivo de Backup!", debug_backtrace()); |
|
try { |
|
$this->create_folder(); |
|
$this->copy_to_folder(); |
|
foreach ($this->formats as $format) { |
|
$this->$format(); |
|
} |
|
return true; |
|
} catch (Exception $ex) { |
|
$this->log()->error("Erro ao criar os arquivos de backup! " . $ex->getMessage(), debug_backtrace()); |
|
return false; |
|
} |
|
} |
|
|
|
######################################################################## |
|
######## FORMAT COMPACT ######## |
|
######################################################################## |
|
|
|
/** |
|
* Formato de compactacao em tar.gz |
|
*/ |
|
public function targz() { |
|
$this->formats["targz"] = 'compact_targz'; |
|
} |
|
|
|
######################################################################## |
|
######## IMPORTANT UTIL ######## |
|
######################################################################## |
|
|
|
/** |
|
* Lista dos arquivos que serao feitos o backup |
|
* @param string $file |
|
* @param string $rename |
|
*/ |
|
public function setFiles($file, $rename = null) { |
|
if ($rename) { |
|
$this->files[$rename] = $file; |
|
} else { |
|
$this->files[] = $file; |
|
} |
|
} |
|
|
|
/** |
|
* Completa o nome da pasta de backup |
|
* @param string $name |
|
*/ |
|
private function setFolder($name) { |
|
$this->folder = $this->strslash(self::CONF_DIR_TEMP_FOLDER) . $this->strslash($name); |
|
} |
|
|
|
/** |
|
* Nome do arquivo a ser compactado e seu formato |
|
* @param string $file |
|
* @return string |
|
*/ |
|
private function compact($file) { |
|
$this->compact = $file; |
|
return $this->compact; |
|
} |
|
|
|
######################################################################## |
|
######## FUNCTIONS CLASS ######## |
|
######################################################################## |
|
|
|
/** |
|
* Cria a pasta para adicionar os arquivos de backup |
|
* @throws Exception |
|
*/ |
|
private function create_folder() { |
|
$this->log()->debug("### Criando Pasta ###", debug_backtrace()); |
|
if (mkdir($this->folder)) { |
|
$this->log()->debug("Diretorio criado com sucesso!"); |
|
} else { |
|
$this->log()->error("Nao foi possivel criar diretorio!"); |
|
throw new Exception($this->log()->getText()); |
|
} |
|
} |
|
|
|
/** |
|
* Copia os arquivos de backup para a pasta |
|
* @throws Exception |
|
*/ |
|
private function copy_to_folder() { |
|
$this->log()->debug("### Copiando Arquivos Pasta ###", debug_backtrace()); |
|
if (!file_exists($this->dir)) { |
|
$this->log()->error("Nome da pasta informda nao encontrada!"); |
|
throw new Exception($this->log()->getText()); |
|
} |
|
|
|
if (!$this->files) { |
|
$this->log()->error("Nenhum arquivo selecionado para realizar o backup!"); |
|
throw new Exception($this->log()->getText()); |
|
} |
|
|
|
$this->log()->debug("Pasta de Backup " . $this->folder); |
|
|
|
foreach ($this->files as $rename => $file) { |
|
$this->log()->debug("Pasta: " . $this->folder); |
|
if (!file_exists($this->dir . $file)) { |
|
$this->log()->error($file . " nao encontrado!"); |
|
} else { |
|
shell_exec("/bin/cp -Rap {$this->dir}{$file} {$this->folder}" . (!is_numeric($rename) ? $rename : '')); |
|
} |
|
} |
|
|
|
if (filesize($this->folder) < 5) { |
|
unlink($this->folder); |
|
$this->log()->error("Arquivo de backup foi criado vazio! Excluido.."); |
|
throw new Exception($this->log()->getText()); |
|
} |
|
$this->log()->debug("Finalizado a copia dos arquivos!"); |
|
} |
|
|
|
/** |
|
* Compacta a pasta em formato tar.gz |
|
* @throws Exception |
|
*/ |
|
private function compact_targz() { |
|
$this->log()->debug("### Compactando Pasta ###", debug_backtrace()); |
|
if (file_exists($this->folder)) { |
|
$targz = $this->strslash(self::CONF_DIR_TEMP_FOLDER) . "{$this->name}.tar.gz"; |
|
shell_exec("tar -czf " . $this->compact($targz) . " -C " . $this->strslash(self::CONF_DIR_TEMP_FOLDER) . " " . $this->name); |
|
shell_exec("/bin/rm -Rf {$this->folder}"); |
|
} else { |
|
$this->log()->error("Pasta nao encontrada para a compactacao!"); |
|
throw new Exception($this->log()->getText()); |
|
} |
|
$this->log()->debug("Finalizacao da compactacao do arquivo!"); |
|
} |
|
|
|
/** |
|
* Remove o arquivo compactado do servidor |
|
* @return boolean |
|
*/ |
|
public function remove_compact() { |
|
$this->log()->debug("Remove Arquivo Compactado", debug_backtrace()); |
|
shell_exec("/bin/rm -Rf {$this->compact}"); |
|
if (!file_exists($this->compact)) { |
|
$this->log()->success("Arquivo compactado removido!"); |
|
return true; |
|
} else { |
|
$this->log()->error("Nao foi possivel remover arquivo compactado!"); |
|
return false; |
|
} |
|
} |
|
|
|
} |
|
|