88dba54087
Introduced backup/restore via external pg_dump/pg_restore tools, configurable in database.xml. Added DatabaseBackupOptions, PostgresBackupService, and example/config docs. Backup features are enabled only for local databases; remote DBs require manual management. Logging and documentation clarify external tool usage, error handling, and security. Updated provider logic and assembly files accordingly.
6.2 KiB
6.2 KiB
PostgreSQL Backup Configuration - Implementation Summary
What Was Created
This implementation adds configurable PostgreSQL backup options to Jellyfin that can be configured through the database.xml file.
New Files Created:
-
DatabaseBackupOptions.cs- Location:
src/Jellyfin.Database/Jellyfin.Database.Implementations/DbConfiguration/ - Contains all configurable backup settings
- Properties: PgDumpPath, PgRestorePath, BackupFormat, CompressionLevel, TimeoutSeconds, etc.
- Location:
-
PostgresBackupService.cs- Location:
src/Jellyfin.Database/Jellyfin.Database.Providers.Postgres/ - Service class that performs backup and restore operations
- Reads configuration from
database.xml - Executes
pg_dumpandpg_restorewith configured options
- Location:
-
database.xml.example- Location:
src/Jellyfin.Database/Jellyfin.Database.Implementations/DbConfiguration/ - Example configuration file showing all available options
- Can be copied to Jellyfin's config directory
- Location:
-
BACKUP_CONFIGURATION.md- Location:
src/Jellyfin.Database/Jellyfin.Database.Providers.Postgres/ - Complete documentation for end users
- Configuration reference and troubleshooting guide
- Location:
Modified Files:
DatabaseConfigurationOptions.cs- Added
BackupOptionsproperty to hold backup configuration
- Added
How It Works
Configuration Flow:
database.xml
↓
DatabaseConfigurationOptions.BackupOptions
↓
PostgresBackupService reads options via IConfigurationManager
↓
pg_dump/pg_restore executed with configured parameters
Example database.xml:
<DatabaseConfigurationOptions>
<DatabaseType>Jellyfin-PostgreSQL</DatabaseType>
<CustomProviderOptions>
<PluginName>Jellyfin-PostgreSQL</PluginName>
<PluginAssembly>Jellyfin.Database.Providers.Postgres</PluginAssembly>
<ConnectionString>Host=localhost;Database=jellyfin;Username=jellyfin;Password=pwd</ConnectionString>
</CustomProviderOptions>
<BackupOptions>
<PgDumpPath>pg_dump</PgDumpPath>
<BackupFormat>custom</BackupFormat>
<CompressionLevel>6</CompressionLevel>
<IncludeBlobs>true</IncludeBlobs>
<TimeoutSeconds>1800</TimeoutSeconds>
<VerboseOutput>true</VerboseOutput>
</BackupOptions>
</DatabaseConfigurationOptions>
Using the Service:
// In your DI setup (Startup.cs or similar)
services.AddSingleton<PostgresBackupService>();
// In your code
public class BackupManager
{
private readonly PostgresBackupService _backupService;
public BackupManager(PostgresBackupService backupService)
{
_backupService = backupService;
}
public async Task PerformBackup()
{
// All settings are read from database.xml automatically
var backupPath = await _backupService.CreateBackupAsync("/backup/directory");
Console.WriteLine($"Backup created: {backupPath}");
}
public async Task RestoreFromBackup(string backupFile)
{
await _backupService.RestoreBackupAsync(backupFile);
}
}
Configuration Options
All options are optional with sensible defaults:
| Option | Type | Default | Description |
|---|---|---|---|
PgDumpPath |
string | Searches PATH | Path to pg_dump executable |
PgRestorePath |
string | Searches PATH | Path to pg_restore executable |
BackupFormat |
string | "custom" | Backup format: custom, plain, directory, tar |
IncludeBlobs |
bool | true | Include large objects in backup |
CompressionLevel |
int | 6 | Compression level (0-9) |
TimeoutSeconds |
int | 1800 | Max backup/restore time (30 min) |
VerboseOutput |
bool | true | Enable verbose logging |
ParallelJobs |
int? | null | Number of parallel jobs (directory format) |
AdditionalArguments |
string? | null | Extra pg_dump arguments |
Benefits
- User-Configurable: Users can customize backup behavior without code changes
- Flexible: Supports all pg_dump formats and options
- Secure: Uses environment variables for passwords
- Robust: Includes timeout handling and error reporting
- Well-Documented: Complete documentation for end users
- Backward Compatible: All options have defaults, no configuration required for basic use
Next Steps
To integrate this into Jellyfin:
-
Register the Service in your DI container:
services.AddSingleton<PostgresBackupService>(); -
Wire into existing backup system (if Jellyfin has one):
// In your backup manager/controller if (dbType == "PostgreSQL") { var postgresBackup = serviceProvider.GetRequiredService<PostgresBackupService>(); await postgresBackup.CreateBackupAsync(backupDirectory); } -
Add UI configuration (optional):
- Create admin UI to edit backup options
- Save to database.xml through configuration manager
-
Testing:
- Test with different backup formats
- Test timeout handling
- Test restore operations
- Test path auto-detection on Windows/Linux
Example Usage Scenarios
Scenario 1: Basic Backup (default settings)
<BackupOptions>
<PgDumpPath>pg_dump</PgDumpPath>
</BackupOptions>
Scenario 2: High Compression for Storage
<BackupOptions>
<BackupFormat>custom</BackupFormat>
<CompressionLevel>9</CompressionLevel>
<TimeoutSeconds>3600</TimeoutSeconds>
</BackupOptions>
Scenario 3: Fast Parallel Backup
<BackupOptions>
<BackupFormat>directory</BackupFormat>
<ParallelJobs>4</ParallelJobs>
<CompressionLevel>3</CompressionLevel>
</BackupOptions>
Scenario 4: Plain SQL Script
<BackupOptions>
<BackupFormat>plain</BackupFormat>
<AdditionalArguments>--exclude-schema=test</AdditionalArguments>
</BackupOptions>
Security Considerations
- File Permissions: Ensure
database.xmlis readable only by the Jellyfin user - Backup Storage: Store backups in a secure location with appropriate permissions
- Password Handling: Uses
PGPASSWORDenvironment variable (more secure than CLI args) - Audit Logging: All operations are logged with timestamps
Troubleshooting
Common issues and solutions are documented in BACKUP_CONFIGURATION.md.