Skip to content

Commit 1e1f932

Browse files
committed
Add support for custom backup provider patterns
1 parent 6b0b23e commit 1e1f932

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ A script to automatically back up all databases running under docker on a host,
1717
- [pgautoupgrade](https://github.com/pgautoupgrade/docker-pgautoupgrade))
1818
- Redis
1919

20+
### Custom Backup Providers
21+
22+
You can extend the existing backup providers with additional container patterns by setting environment variables:
23+
24+
```
25+
CUSTOM_BACKUP_PROVIDER_POSTGRES_PATTERNS=immich-app/postgres,my-custom-postgres
26+
CUSTOM_BACKUP_PROVIDER_MYSQL_PATTERNS=my-custom-mysql,another-mysql-container
27+
CUSTOM_BACKUP_PROVIDER_REDIS_PATTERNS=my-custom-redis
28+
```
29+
30+
Each variable should contain a comma-separated list of container patterns to match.
31+
2032
## Installation
2133

2234
This container requires access to the docker socket. This can be done either by mounting `/var/lib/docker.sock`, or using a HTTP proxy to provide it through `$DOCKER_HOST`.
@@ -55,6 +67,9 @@ services:
5567
environment:
5668
- SUCCESS_HOOK_URL=https://hc-ping.com/1234
5769
- INCLUDE_LOGS=true
70+
# Custom backup provider patterns
71+
- CUSTOM_BACKUP_PROVIDER_POSTGRES_PATTERNS=immich-app/postgres,custom-postgres
72+
- CUSTOM_BACKUP_PROVIDER_MYSQL_PATTERNS=my-custom-mysql
5873
```
5974
6075
### Oneshot

db-auto-backup.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,29 @@ def backup_redis(container: Container) -> str:
155155
),
156156
]
157157

158+
# Extend backup providers with custom patterns from environment variables
159+
# Format: CUSTOM_BACKUP_PROVIDER_<provider_name>_PATTERNS=pattern1,pattern2,...
160+
for env_var, value in os.environ.items():
161+
if env_var.startswith("CUSTOM_BACKUP_PROVIDER_") and env_var.endswith("_PATTERNS"):
162+
provider_name = env_var.replace("CUSTOM_BACKUP_PROVIDER_", "").replace("_PATTERNS", "").lower()
163+
custom_patterns = [pattern.strip() for pattern in value.split(",") if pattern.strip()]
164+
165+
# Find the provider and extend its patterns
166+
for provider in BACKUP_PROVIDERS:
167+
if provider.name.lower() == provider_name:
168+
# Create a new BackupProvider with extended patterns
169+
index = BACKUP_PROVIDERS.index(provider)
170+
BACKUP_PROVIDERS[index] = BackupProvider(
171+
name=provider.name,
172+
patterns=provider.patterns + custom_patterns,
173+
backup_method=provider.backup_method,
174+
file_extension=provider.file_extension,
175+
)
176+
print(f"Extended {provider.name} backup provider with patterns: {', '.join(custom_patterns)}")
177+
break
178+
else:
179+
print(f"Warning: Custom backup provider {provider_name} not found, skipping patterns: {', '.join(custom_patterns)}")
180+
158181

159182
BACKUP_DIR = Path(os.environ.get("BACKUP_DIR", "/var/backups"))
160183
SCHEDULE = os.environ.get("SCHEDULE", "0 0 * * *")

tests/tests.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,45 @@ def test_get_backup_provider(container_name: str, name: str) -> None:
130130

131131
assert provider is not None
132132
assert provider.name == name
133+
134+
135+
def test_custom_backup_provider_patterns(monkeypatch: Any) -> None:
136+
# Save original backup providers
137+
original_providers = db_auto_backup.BACKUP_PROVIDERS.copy()
138+
139+
try:
140+
# Set custom patterns environment variable
141+
monkeypatch.setenv("CUSTOM_BACKUP_PROVIDER_POSTGRES_PATTERNS", "immich-app/postgres,custom-postgres")
142+
143+
# Create a copy of the original providers
144+
test_providers = original_providers.copy()
145+
db_auto_backup.BACKUP_PROVIDERS = test_providers
146+
147+
# Run the code that processes environment variables
148+
for env_var, value in {"CUSTOM_BACKUP_PROVIDER_POSTGRES_PATTERNS": "immich-app/postgres,custom-postgres"}.items():
149+
if env_var.startswith("CUSTOM_BACKUP_PROVIDER_") and env_var.endswith("_PATTERNS"):
150+
provider_name = env_var.replace("CUSTOM_BACKUP_PROVIDER_", "").replace("_PATTERNS", "").lower()
151+
custom_patterns = [pattern.strip() for pattern in value.split(",") if pattern.strip()]
152+
153+
for provider in db_auto_backup.BACKUP_PROVIDERS:
154+
if provider.name.lower() == provider_name:
155+
index = db_auto_backup.BACKUP_PROVIDERS.index(provider)
156+
db_auto_backup.BACKUP_PROVIDERS[index] = db_auto_backup.BackupProvider(
157+
name=provider.name,
158+
patterns=provider.patterns + custom_patterns,
159+
backup_method=provider.backup_method,
160+
file_extension=provider.file_extension,
161+
)
162+
break
163+
164+
# Test with the new custom pattern
165+
provider = db_auto_backup.get_backup_provider(["immich-app/postgres"])
166+
assert provider is not None
167+
assert provider.name == "postgres"
168+
169+
provider = db_auto_backup.get_backup_provider(["custom-postgres"])
170+
assert provider is not None
171+
assert provider.name == "postgres"
172+
finally:
173+
# Restore original backup providers
174+
db_auto_backup.BACKUP_PROVIDERS = original_providers

0 commit comments

Comments
 (0)