/** * Gets a list of database backups * @return array Objects of backups * @throws InternalErrorException */ public static function index() { if (!is_readable(BACKUPS)) { throw new InternalErrorException(__d('database_backup', 'File or directory {0} not readable', rtr(BACKUPS))); } //Gets all files $files = array_values((new Folder(BACKUPS))->read()[1]); //Parses files $files = array_filter(array_map(function ($file) { preg_match('/(\\d{14})?\\.(sql|sql\\.gz|sql\\.bz2)$/i', $file, $matches); if (empty($matches[2])) { return false; } //If it cannot detect the date from the filename, it tries to find // the last modified date of the file if (!empty($matches[1])) { $datetime = preg_replace('/(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})/', '$1-$2-$3 $4:$5:$6', $matches[1]); } else { $datetime = date('Y-m-d H:i:s', filemtime(BACKUPS . DS . $file)); } return (object) ['filename' => $file, 'extension' => $matches[2], 'size' => filesize(BACKUPS . DS . $file), 'compression' => getCompression($matches[2]), 'datetime' => new FrozenTime($datetime)]; }, $files)); if (empty($files)) { return []; } //Re-orders, using the datetime value usort($files, function ($a, $b) { return $b->datetime >= $a->datetime; }); return $files; }
/** * Sets the filename where to export the database. * * Using this method, the compression type will be automatically detected * by the filename. * @param string $filename Filename path * @return string Filename path * @uses compression() * @uses $connection * @uses $filename * @throws InternalErrorException */ public function filename($filename) { //Replaces patterns $filename = str_replace(['{$DATABASE}', '{$DATETIME}', '{$HOSTNAME}', '{$TIMESTAMP}'], [$this->connection['database'], date('YmdHis'), $this->connection['host'], time()], $filename); $filename = BACKUPS . DS . $filename; if (file_exists($filename)) { throw new InternalErrorException(__d('database_backup', 'File or directory {0} already exists', $filename)); } //Checks if the file has an extension if (!preg_match('/\\.(.+)$/', pathinfo($filename, PATHINFO_BASENAME), $matches)) { throw new InternalErrorException(__d('database_backup', 'Invalid file extension')); } $this->compression(getCompression($matches[1])); return $this->filename = $filename; }
/** * Sets the filename where to export the database * @param string $filename Filename path * @return string Filename path * @throws InternalErrorException * @uses compression() * @uses $filename */ public function filename($filename) { if (!\Cake\Filesystem\Folder::isAbsolute($filename)) { $filename = BACKUPS . DS . $filename; } if (!is_readable($filename)) { throw new InternalErrorException(__d('database_backup', 'File or directory {0} not readable', $filename)); } //Checks if the file has an extension if (!preg_match('/\\.(.+)$/', pathinfo($filename, PATHINFO_BASENAME), $matches)) { throw new InternalErrorException(__d('database_backup', 'Invalid file extension')); } $this->compression(getCompression($matches[1])); return $this->filename = $filename; }