null, 1 => 'FTP', 2 => 'SFTP', 3 => 'Rede', 4 => 'Local', 5 => 'USB'); /** * Variável que armazena se é necessário forçar o backup mesmo que o arquivo temporário já exista * * @var bool */ private $forceBackup = false; /** * Variável que armazena se é um backup local a ser executado * @var bool */ private $isLocal = false; /** * BackupConfig constructor. * * 1 2 3 4 5 * manual 4 0 2017-08-24 admin * * @param array $argv * * @throws Exception */ private function __construct(array $argv) { if (!isset($argv)) { throw new Exception("É obrigatório informar \$argv no construtor"); } self::$argv = $argv; $this->script = $this->getArg(0, __FILE__); if (isset($argv[1])) { $arg1 = strtoupper(trim($argv[1])); $this->isManual = ($arg1 == 'MANUAL'); } $this->isDebug = !($this->isManual); $this->tipo = $this->getArg(2, BKP_TIPO_DADOS); $this->isDiferencial = ($this->getArg(3, 0) === 1); $this->data = $this->getArg(4, date('Y-m-d')); $this->usuario = $this->getArg(5, 'admin'); $this->forceBackup = $this->getArg(6, false); self::$tmpFile = sprintf(self::$tmpFile, date("Ymd")); self::log("Arquivo temporário: " . self::$tmpFile); try { return $this->prepare(); } catch (Exception $e) { self::log($e->getMessage()); exit; } } private function getArg($argIndex, $default = null) { return ($this->isManual && isset($argv[$argIndex]) ? self::$argv[$argIndex] : $default); } public static function log($msg, $logDate = true) { $log = (($logDate) ? date('d/m/Y H:i:s') . ': ' : '') . $msg . "\n"; GravaLog($log, BackupConfig::$logFile); } /** * verifica se o script já esta em execução. * Outra instancia não deve iniciar até que seja concluída a operação. * * @return BackupConfig * @throws Exception */ private function prepare() { $fileExec = self::$tmpFile; if (file_exists($fileExec)) { $msg = "{$fileExec} já existe, abortando operação de backup"; if (!$this->forceBackup) { throw new Exception($msg); } else { self::log("Arquivo de backup já existe porém o script foi executando com a opção de forçar o backup"); $this->removeTmp(); } } $arq = fopen($fileExec, 'w'); fclose($arq); return $this; } /** * @param $argv * * @return BackupConfig */ public static function getInstance($argv) { self::log("", false); self::log("Iniciando processo de backup"); if (!isset(BackupConfig::$instance)) { BackupConfig::$instance = new BackupConfig($argv); } return BackupConfig::$instance; } /** * */ public function __destruct() { if ($this->isRunning()) { $this->removeTmp(); } self::log("Processo de backup finalizado"); } /** * Checa se existe um backup em andamento * * @return bool */ public function isRunning() { return file_exists(self::$tmpFile); } /** * Remove o arquivo temporário e libera o script para uma nova operação de backup */ private function removeTmp() { self::log("Removendo arquivo temporário..."); $remove = unlink(self::$tmpFile); if ($remove) { self::log("Arquivo removido com sucesso."); } else { self::log("Não foi possível excluir o arquivo temporário."); } } /** * @param array $config * * @return $this */ public function setBackupConfig(array $config) { $this->config = $config; if (empty($config)) { self::log("Nenhum backup está configurado para executar agora"); } else { $this->countBackups = count($config); self::log("{$this->countBackups} backups estão prontos para iniciarem"); } return $this; } /** * @return mixed */ public function getCountBackups() { return $this->countBackups; } /** * @param null|int $index * * @return array */ public function getConfig($index = null) { return (isset($index) && !is_null($index) ? $this->config[$index] : $this->config); } /** * identifica e instancia o gerenciador de backup de acordo com o tipo de backup a ser feito * * @param int $protocolo * * @return AbstractBackup * @throws Exception */ public function determineHandler($protocolo = null) { if (is_null($protocolo) || $protocolo <= 0 || !isset($this->protocolos[$protocolo])) { throw new Exception("Protocolo inválido"); } $backupObjectType = $this->protocolos[$protocolo]; $this->isLocal = ((int) $protocolo === 4); $backupObjectClassName = "Backup{$backupObjectType}"; require_once 'AbstractBackup.php'; require_once 'AbstractLocalLan.php'; /** @noinspection PhpIncludeInspection */ require_once "{$backupObjectClassName}.php"; return new $backupObjectClassName(); } /** * @return bool */ public function isLocal() { return $this->isLocal; } }