예제 #1
0
        public async Task <ActionResult> GetLogs()
        {
            var files = _backupService.LogFiles(_config.GetMaxRollingFiles(), _config.GetLoggingFileName());

            var tempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp");
            var dateString    = DateTime.Now.ToShortDateString().Replace("/", "_");

            var tempLocation = Path.Join(tempDirectory, "logs_" + dateString);

            DirectoryService.ExistOrCreate(tempLocation);
            if (!_directoryService.CopyFilesToDirectory(files, tempLocation))
            {
                return(BadRequest("Unable to copy files to temp directory for log download."));
            }

            var zipPath = Path.Join(tempDirectory, $"kavita_logs_{dateString}.zip");

            try
            {
                ZipFile.CreateFromDirectory(tempLocation, zipPath);
            }
            catch (AggregateException ex)
            {
                _logger.LogError(ex, "There was an issue when archiving library backup");
                return(BadRequest("There was an issue when archiving library backup"));
            }
            var fileBytes = await _directoryService.ReadFileAsync(zipPath);

            DirectoryService.ClearAndDeleteDirectory(tempLocation);
            (new FileInfo(zipPath)).Delete();

            return(File(fileBytes, "application/zip", Path.GetFileName(zipPath)));
        }
예제 #2
0
        public void BackupDatabase()
        {
            _logger.LogInformation("Beginning backup of Database at {BackupTime}", DateTime.Now);
            var backupDirectory = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BackupDirectory)).Result.Value;

            _logger.LogDebug("Backing up to {BackupDirectory}", backupDirectory);
            if (!DirectoryService.ExistOrCreate(backupDirectory))
            {
                _logger.LogError("Could not write to {BackupDirectory}; aborting backup", backupDirectory);
                return;
            }

            var dateString = DateTime.Now.ToShortDateString().Replace("/", "_");
            var zipPath    = Path.Join(backupDirectory, $"kavita_backup_{dateString}.zip");

            if (File.Exists(zipPath))
            {
                _logger.LogInformation("{ZipFile} already exists, aborting", zipPath);
                return;
            }

            var tempDirectory = Path.Join(_tempDirectory, dateString);

            DirectoryService.ExistOrCreate(tempDirectory);
            DirectoryService.ClearDirectory(tempDirectory);

            _directoryService.CopyFilesToDirectory(
                _backupFiles.Select(file => Path.Join(Directory.GetCurrentDirectory(), file)).ToList(), tempDirectory);
            try
            {
                ZipFile.CreateFromDirectory(tempDirectory, zipPath);
            }
            catch (AggregateException ex)
            {
                _logger.LogError(ex, "There was an issue when archiving library backup");
            }

            DirectoryService.ClearAndDeleteDirectory(tempDirectory);
            _logger.LogInformation("Database backup completed");
        }