9
9
from datetime import datetime
10
10
from io import StringIO
11
11
from pathlib import Path
12
- from typing import IO , Callable , Dict , NamedTuple , Optional , Sequence
12
+ from typing import IO , Callable , Dict , Iterable , NamedTuple , Optional
13
13
14
14
import docker
15
15
import pycron
@@ -150,7 +150,7 @@ def backup_redis(container: Container) -> str:
150
150
INCLUDE_LOGS = bool (os .environ .get ("INCLUDE_LOGS" ))
151
151
152
152
153
- def get_backup_provider (container_names : Sequence [str ]) -> Optional [BackupProvider ]:
153
+ def get_backup_provider (container_names : Iterable [str ]) -> Optional [BackupProvider ]:
154
154
for name in container_names :
155
155
for provider in BACKUP_PROVIDERS :
156
156
if any (fnmatch .fnmatch (name , pattern ) for pattern in provider .patterns ):
@@ -159,14 +159,23 @@ def get_backup_provider(container_names: Sequence[str]) -> Optional[BackupProvid
159
159
return None
160
160
161
161
162
+ def get_container_names (container : Container ) -> Iterable [str ]:
163
+ names = set ()
164
+ for tag in container .image .tags :
165
+ registry , image = docker .auth .resolve_repository_name (tag )
166
+ image , tag_name = image .split (":" , 1 )
167
+ names .add (image )
168
+ return names
169
+
170
+
162
171
@pycron .cron (SCHEDULE )
163
172
def backup (now : datetime ) -> None :
164
173
docker_client = docker .from_env ()
165
174
166
175
backed_up_containers = []
167
176
168
177
for container in docker_client .containers .list ():
169
- container_names = [ tag . rsplit ( ":" , 1 )[ 0 ] for tag in container . image . tags ]
178
+ container_names = get_container_names ( container )
170
179
backup_provider = get_backup_provider (container_names )
171
180
if backup_provider is None :
172
181
continue
0 commit comments