public void Cleanup() { _logger.LogInformation("Cleaning temp directory"); var tempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp"); DirectoryService.ClearDirectory(tempDirectory); _logger.LogInformation("Cleaning cache directory"); _cacheService.Cleanup(); _logger.LogInformation("Cleaning old database backups"); _backupService.CleanupBackups(); }
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"); }