Skip to content
Open
11 changes: 9 additions & 2 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ rcb dump-env > /.env
# Write crontab
rcb crontab > crontab

# start cron in the foreground
# Start cron in the background and capture its PID
crontab crontab
crond -f
crond -f &
CRON_PID=$!

# Trap termination signals and kill the cron process
trap 'kill $CRON_PID; exit 0' TERM INT

# Wait for cron and handle signals
wait $CRON_PID
3 changes: 0 additions & 3 deletions src/restic_compose_backup/backup_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def run(
volumes: dict = None,
environment: dict = None,
labels: dict = None,
source_container_id: str = None,
):
logger.info("Starting backup container")
client = utils.docker_client()
Expand All @@ -21,11 +20,9 @@ def run(
image,
command,
labels=labels,
# auto_remove=True, # We remove the container further down
detach=True,
environment=environment + ["BACKUP_PROCESS_CONTAINER=true"],
volumes=volumes,
network_mode=f"container:{source_container_id}", # Reuse original container's network stack.
working_dir=os.getcwd(),
tty=True,
)
Expand Down
1 change: 0 additions & 1 deletion src/restic_compose_backup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ def backup(config, containers):
command="rcb start-backup-process",
volumes=volumes,
environment=containers.this_container.environment,
source_container_id=containers.this_container.id,
labels={
containers.backup_process_label: "True",
"com.docker.compose.project": containers.project_name,
Expand Down
1 change: 1 addition & 0 deletions src/restic_compose_backup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, check=True):
self.swarm_mode = os.environ.get("SWARM_MODE") or False
self.include_project_name = os.environ.get("INCLUDE_PROJECT_NAME") or False
self.exclude_bind_mounts = os.environ.get("EXCLUDE_BIND_MOUNTS") or False
self.include_all_compose_projects = os.environ.get("INCLUDE_ALL_COMPOSE_PROJECTS") or False
self.include_all_volumes = os.environ.get("INCLUDE_ALL_VOLUMES") or False
if self.include_all_volumes:
logger.warning(
Expand Down
21 changes: 8 additions & 13 deletions src/restic_compose_backup/containers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import logging
from pathlib import Path
import socket
from typing import List

from restic_compose_backup import enums, utils
Expand Down Expand Up @@ -407,13 +407,13 @@ def __init__(self):
# Find the container we are running in.
# If we don't have this information we cannot continue
for container_data in all_containers:
if container_data.get("Id").startswith(os.environ["HOSTNAME"]):
if container_data.get("Id").startswith(socket.gethostname()):
self.this_container = Container(container_data)

if not self.this_container:
raise ValueError("Cannot find metadata for backup container")

# Gather all running containers in the current compose setup
# Gather relevant containers
for container_data in all_containers:
container = Container(container_data)

Expand All @@ -429,24 +429,19 @@ def __init__(self):
if not container.is_running:
continue

# If not swarm mode we need to filter in compose project
if not config.swarm_mode and not config.include_all_compose_projects and container.project_name != self.this_container.project_name:
continue

# Gather stop during backup containers
if container.stop_during_backup:
if config.swarm_mode:
self.stop_during_backup_containers.append(container)
else:
if container.project_name == self.this_container.project_name:
self.stop_during_backup_containers.append(container)
self.stop_during_backup_containers.append(container)

# Detect running backup process container
if container.is_backup_process_container:
self.backup_process_container = container

# --- Determine what containers should be evaluated

# If not swarm mode we need to filter in compose project
if not config.swarm_mode:
if container.project_name != self.this_container.project_name:
continue

# Containers started manually are not included
if container.is_oneoff:
Expand Down
4 changes: 0 additions & 4 deletions src/restic_compose_backup/log.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import logging
import os
import sys

logger = logging.getLogger("restic_compose_backup")
HOSTNAME = os.environ["HOSTNAME"]

DEFAULT_LOG_LEVEL = logging.INFO
LOG_LEVELS = {
Expand All @@ -22,7 +20,5 @@ def setup(level: str = "warning"):

ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(level)
# ch.setFormatter(logging.Formatter('%(asctime)s - {HOSTNAME} - %(name)s - %(levelname)s - %(message)s'))
# ch.setFormatter(logging.Formatter('%(asctime)s - {HOSTNAME} - %(levelname)s - %(message)s'))
ch.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s: %(message)s"))
logger.addHandler(ch)
Loading