forked from coop-cloud/backup-bot-two
Compare commits
11 Commits
main
...
feature/do
Author | SHA1 | Date | |
---|---|---|---|
2a7e564a24 | |||
5f381f395d | |||
e0ee16426b | |||
92845c4142 | |||
d0d0f29c79 | |||
88168de90e | |||
71c88d0428 | |||
15b2d656bb | |||
46522a2e9a | |||
30e88a972a | |||
64e09a6472 |
@ -4,8 +4,6 @@ SECRET_RESTIC_PASSWORD_VERSION=v1
|
|||||||
|
|
||||||
COMPOSE_FILE=compose.yml
|
COMPOSE_FILE=compose.yml
|
||||||
|
|
||||||
DOMAIN=backup-bot-two.example.com
|
|
||||||
|
|
||||||
RESTIC_REPOSITORY=/backups/restic
|
RESTIC_REPOSITORY=/backups/restic
|
||||||
|
|
||||||
CRON_SCHEDULE='30 3 * * *'
|
CRON_SCHEDULE='30 3 * * *'
|
||||||
|
@ -10,7 +10,7 @@ Automatically take backups from all volumes of running Docker Swarm services and
|
|||||||
|
|
||||||
* **Category**: Utilities
|
* **Category**: Utilities
|
||||||
* **Status**: 0, work-in-progress
|
* **Status**: 0, work-in-progress
|
||||||
* **Image**: [`git.coopcloud.tech/coop-cloud/backup-bot-two`](https://git.coopcloud.tech/coop-cloud/-/packages/container/backup-bot-two), 4, upstream
|
* **Image**: [`thecoopcloud/backup-bot-two`](https://hub.docker.com/r/thecoopcloud/backup-bot-two), 4, upstream
|
||||||
* **Healthcheck**: No
|
* **Healthcheck**: No
|
||||||
* **Backups**: N/A
|
* **Backups**: N/A
|
||||||
* **Email**: N/A
|
* **Email**: N/A
|
||||||
|
19
backupbot.py
19
backupbot.py
@ -42,7 +42,7 @@ sys.excepthook = handle_exception
|
|||||||
@click.option('-l', '--log', 'loglevel')
|
@click.option('-l', '--log', 'loglevel')
|
||||||
@click.option('-m', '--machine-logs', 'machine_logs', is_flag=True)
|
@click.option('-m', '--machine-logs', 'machine_logs', is_flag=True)
|
||||||
@click.option('service', '--host', '-h', envvar='SERVICE')
|
@click.option('service', '--host', '-h', envvar='SERVICE')
|
||||||
@click.option('repository', '--repo', '-r', envvar='RESTIC_REPOSITORY')
|
@click.option('repository', '--repo', '-r', envvar='RESTIC_REPOSITORY', required=True)
|
||||||
def cli(loglevel, service, repository, machine_logs):
|
def cli(loglevel, service, repository, machine_logs):
|
||||||
global SERVICE
|
global SERVICE
|
||||||
if service:
|
if service:
|
||||||
@ -54,8 +54,8 @@ def cli(loglevel, service, repository, machine_logs):
|
|||||||
if not isinstance(numeric_level, int):
|
if not isinstance(numeric_level, int):
|
||||||
raise ValueError('Invalid log level: %s' % loglevel)
|
raise ValueError('Invalid log level: %s' % loglevel)
|
||||||
logger.setLevel(numeric_level)
|
logger.setLevel(numeric_level)
|
||||||
logHandler = logging.StreamHandler()
|
|
||||||
if machine_logs:
|
if machine_logs:
|
||||||
|
logHandler = logging.StreamHandler()
|
||||||
formatter = jsonlogger.JsonFormatter(
|
formatter = jsonlogger.JsonFormatter(
|
||||||
"%(levelname)s %(filename)s %(lineno)s %(process)d %(message)s", rename_fields={"levelname": "message_type"})
|
"%(levelname)s %(filename)s %(lineno)s %(process)d %(message)s", rename_fields={"levelname": "message_type"})
|
||||||
logHandler.setFormatter(formatter)
|
logHandler.setFormatter(formatter)
|
||||||
@ -66,13 +66,9 @@ def cli(loglevel, service, repository, machine_logs):
|
|||||||
|
|
||||||
|
|
||||||
def init_repo():
|
def init_repo():
|
||||||
if repo:= os.environ.get('RESTIC_REPOSITORY_FILE'):
|
|
||||||
# RESTIC_REPOSITORY_FILE and RESTIC_REPOSITORY are mutually exclusive
|
|
||||||
del os.environ['RESTIC_REPOSITORY']
|
|
||||||
else:
|
|
||||||
repo = os.environ['RESTIC_REPOSITORY']
|
repo = os.environ['RESTIC_REPOSITORY']
|
||||||
restic.repository = repo
|
|
||||||
logger.debug(f"set restic repository location: {repo}")
|
logger.debug(f"set restic repository location: {repo}")
|
||||||
|
restic.repository = repo
|
||||||
restic.password_file = '/var/run/secrets/restic_password'
|
restic.password_file = '/var/run/secrets/restic_password'
|
||||||
try:
|
try:
|
||||||
restic.cat.config()
|
restic.cat.config()
|
||||||
@ -108,7 +104,7 @@ def create(retries):
|
|||||||
def get_backup_cmds():
|
def get_backup_cmds():
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
container_by_service = {
|
container_by_service = {
|
||||||
c.labels.get('com.docker.swarm.service.name'): c for c in client.containers.list()}
|
c.labels['com.docker.swarm.service.name']: c for c in client.containers.list()}
|
||||||
backup_paths = set()
|
backup_paths = set()
|
||||||
backup_apps = set()
|
backup_apps = set()
|
||||||
pre_commands = {}
|
pre_commands = {}
|
||||||
@ -143,14 +139,14 @@ def copy_secrets(apps):
|
|||||||
os.mkdir(SECRET_PATH)
|
os.mkdir(SECRET_PATH)
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
container_by_service = {
|
container_by_service = {
|
||||||
c.labels.get('com.docker.swarm.service.name'): c for c in client.containers.list()}
|
c.labels['com.docker.swarm.service.name']: c for c in client.containers.list()}
|
||||||
services = client.services.list()
|
services = client.services.list()
|
||||||
for s in services:
|
for s in services:
|
||||||
app_name = s.attrs['Spec']['Labels']['com.docker.stack.namespace']
|
app_name = s.attrs['Spec']['Labels']['com.docker.stack.namespace']
|
||||||
if (app_name in apps and
|
if (app_name in apps and
|
||||||
(app_secs := s.attrs['Spec']['TaskTemplate']['ContainerSpec'].get('Secrets'))):
|
(app_secs := s.attrs['Spec']['TaskTemplate']['ContainerSpec'].get('Secrets'))):
|
||||||
if not container_by_service.get(s.name):
|
if not container_by_service.get(s.name):
|
||||||
logger.warning(
|
logger.error(
|
||||||
f"Container {s.name} is not running, secrets can not be copied.")
|
f"Container {s.name} is not running, secrets can not be copied.")
|
||||||
continue
|
continue
|
||||||
container_id = container_by_service[s.name].id
|
container_id = container_by_service[s.name].id
|
||||||
@ -161,7 +157,6 @@ def copy_secrets(apps):
|
|||||||
f"For the secret {sec['SecretName']} the file {src} does not exist for {s.name}")
|
f"For the secret {sec['SecretName']} the file {src} does not exist for {s.name}")
|
||||||
continue
|
continue
|
||||||
dst = SECRET_PATH + sec['SecretName']
|
dst = SECRET_PATH + sec['SecretName']
|
||||||
logger.debug("Copy Secret {sec['SecretName']}")
|
|
||||||
copyfile(src, dst)
|
copyfile(src, dst)
|
||||||
|
|
||||||
|
|
||||||
@ -189,8 +184,6 @@ def run_commands(commands):
|
|||||||
def backup_volumes(backup_paths, apps, retries, dry_run=False):
|
def backup_volumes(backup_paths, apps, retries, dry_run=False):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
logger.info("Start volume backup")
|
|
||||||
logger.debug(backup_paths)
|
|
||||||
result = restic.backup(backup_paths, dry_run=dry_run, tags=apps)
|
result = restic.backup(backup_paths, dry_run=dry_run, tags=apps)
|
||||||
logger.summary("backup finished", extra=result)
|
logger.summary("backup finished", extra=result)
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user