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))); }
public void CanExtractArchive(string archivePath, int expectedFileCount) { var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives"); var extractDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives/Extraction"); DirectoryService.ClearAndDeleteDirectory(extractDirectory); Stopwatch sw = Stopwatch.StartNew(); _archiveService.ExtractArchive(Path.Join(testDirectory, archivePath), extractDirectory); var di1 = new DirectoryInfo(extractDirectory); Assert.Equal(expectedFileCount, di1.Exists ? di1.GetFiles().Length : 0); _testOutputHelper.WriteLine($"Processed in {sw.ElapsedMilliseconds} ms"); DirectoryService.ClearAndDeleteDirectory(extractDirectory); }
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"); }