Skip to content

Commit 296c67c

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

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,39 @@ 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 = (
163+
env_var.replace("CUSTOM_BACKUP_PROVIDER_", "")
164+
.replace("_PATTERNS", "")
165+
.lower()
166+
)
167+
custom_patterns = [
168+
pattern.strip() for pattern in value.split(",") if pattern.strip()
169+
]
170+
171+
# Find the provider and extend its patterns
172+
for provider in BACKUP_PROVIDERS:
173+
if provider.name.lower() == provider_name:
174+
# Create a new BackupProvider with extended patterns
175+
index = BACKUP_PROVIDERS.index(provider)
176+
BACKUP_PROVIDERS[index] = BackupProvider(
177+
name=provider.name,
178+
patterns=provider.patterns + custom_patterns,
179+
backup_method=provider.backup_method,
180+
file_extension=provider.file_extension,
181+
)
182+
print(
183+
f"Extended {provider.name} backup provider with patterns: {', '.join(custom_patterns)}"
184+
)
185+
break
186+
else:
187+
print(
188+
f"Warning: Custom backup provider {provider_name} not found, skipping patterns: {', '.join(custom_patterns)}"
189+
)
190+
158191

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

tests/tests.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,60 @@ 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(
142+
"CUSTOM_BACKUP_PROVIDER_POSTGRES_PATTERNS",
143+
"immich-app/postgres,custom-postgres",
144+
)
145+
146+
# Create a copy of the original providers
147+
test_providers = original_providers.copy()
148+
db_auto_backup.BACKUP_PROVIDERS = test_providers
149+
150+
# Run the code that processes environment variables
151+
for env_var, value in {
152+
"CUSTOM_BACKUP_PROVIDER_POSTGRES_PATTERNS": "immich-app/postgres,custom-postgres"
153+
}.items():
154+
if env_var.startswith("CUSTOM_BACKUP_PROVIDER_") and env_var.endswith(
155+
"_PATTERNS"
156+
):
157+
provider_name = (
158+
env_var.replace("CUSTOM_BACKUP_PROVIDER_", "")
159+
.replace("_PATTERNS", "")
160+
.lower()
161+
)
162+
custom_patterns = [
163+
pattern.strip() for pattern in value.split(",") if pattern.strip()
164+
]
165+
166+
for provider in db_auto_backup.BACKUP_PROVIDERS:
167+
if provider.name.lower() == provider_name:
168+
index = db_auto_backup.BACKUP_PROVIDERS.index(provider)
169+
db_auto_backup.BACKUP_PROVIDERS[
170+
index
171+
] = db_auto_backup.BackupProvider(
172+
name=provider.name,
173+
patterns=provider.patterns + custom_patterns,
174+
backup_method=provider.backup_method,
175+
file_extension=provider.file_extension,
176+
)
177+
break
178+
179+
# Test with the new custom pattern
180+
provider = db_auto_backup.get_backup_provider(["immich-app/postgres"])
181+
assert provider is not None
182+
assert provider.name == "postgres"
183+
184+
provider = db_auto_backup.get_backup_provider(["custom-postgres"])
185+
assert provider is not None
186+
assert provider.name == "postgres"
187+
finally:
188+
# Restore original backup providers
189+
db_auto_backup.BACKUP_PROVIDERS = original_providers

0 commit comments

Comments
 (0)