1- import os
21import logging
32from pathlib import Path
3+ import socket
44from typing import List
55
66from restic_compose_backup import enums , utils
@@ -35,6 +35,10 @@ def __init__(self, data: dict):
3535 self ._include = self ._parse_pattern (self .get_label (enums .LABEL_VOLUMES_INCLUDE ))
3636 self ._exclude = self ._parse_pattern (self .get_label (enums .LABEL_VOLUMES_EXCLUDE ))
3737
38+ network_settings : dict = data .get ("NetworkSettings" , {})
39+ networks : dict = network_settings .get ("Networks" , {})
40+ self ._network_details : dict = list (networks .values ())[0 ]
41+
3842 @property
3943 def instance (self ) -> "Container" :
4044 """Container: Get a service specific subclass instance"""
@@ -57,9 +61,14 @@ def id(self) -> str:
5761 return self ._data .get ("Id" )
5862
5963 @property
60- def hostname (self ) -> str :
61- """Hostname of the container"""
62- return self .get_config ("Hostname" , default = self .id [0 :12 ])
64+ def network_name (self ) -> str :
65+ """str: Name of the first network the container is connected to"""
66+ return self ._network_details .get ("NetworkID" , "" )
67+
68+ @property
69+ def ip_address (self ) -> str :
70+ """str: IP address the container has on its first network"""
71+ return self ._network_details .get ("IPAddress" , "" )
6372
6473 @property
6574 def image (self ) -> str :
@@ -407,13 +416,13 @@ def __init__(self):
407416 # Find the container we are running in.
408417 # If we don't have this information we cannot continue
409418 for container_data in all_containers :
410- if container_data .get ("Id" ).startswith (os . environ [ "HOSTNAME" ] ):
419+ if container_data .get ("Id" ).startswith (socket . gethostname () ):
411420 self .this_container = Container (container_data )
412421
413422 if not self .this_container :
414423 raise ValueError ("Cannot find metadata for backup container" )
415424
416- # Gather all running containers in the current compose setup
425+ # Gather relevant containers
417426 for container_data in all_containers :
418427 container = Container (container_data )
419428
@@ -429,25 +438,22 @@ def __init__(self):
429438 if not container .is_running :
430439 continue
431440
441+ # If not swarm mode we need to filter in compose project
442+ if (
443+ not config .swarm_mode
444+ and not config .include_all_compose_projects
445+ and container .project_name != self .this_container .project_name
446+ ):
447+ continue
448+
432449 # Gather stop during backup containers
433450 if container .stop_during_backup :
434- if config .swarm_mode :
435- self .stop_during_backup_containers .append (container )
436- else :
437- if container .project_name == self .this_container .project_name :
438- self .stop_during_backup_containers .append (container )
451+ self .stop_during_backup_containers .append (container )
439452
440453 # Detect running backup process container
441454 if container .is_backup_process_container :
442455 self .backup_process_container = container
443456
444- # --- Determine what containers should be evaluated
445-
446- # If not swarm mode we need to filter in compose project
447- if not config .swarm_mode :
448- if container .project_name != self .this_container .project_name :
449- continue
450-
451457 # Containers started manually are not included
452458 if container .is_oneoff :
453459 continue
@@ -473,10 +479,14 @@ def backup_process_running(self) -> bool:
473479 """Is the backup process container running?"""
474480 return self .backup_process_container is not None
475481
476- def containers_for_backup (self ):
482+ def containers_for_backup (self ) -> list [ Container ] :
477483 """Obtain all containers with backup enabled"""
478484 return [container for container in self .containers if container .backup_enabled ]
479485
486+ def networks_for_backup (self ) -> set [str ]:
487+ """Obtain all networks needed for backup"""
488+ return {container .network_name for container in self .containers_for_backup ()}
489+
480490 def generate_backup_mounts (self , dest_prefix = "/volumes" ) -> dict :
481491 """Generate mounts for backup for the entire compose setup"""
482492 mounts = {}
0 commit comments