databacker / mysql-backup

image to enable automated backups of mysql databases in containers
636 stars 178 forks source link

RUN_ONCE only works with setting DB_DUMP{FREQ,BEGIN} to null/nothing #316

Open BlackQube opened 2 months ago

BlackQube commented 2 months ago

I had an issue with the following scenario:

I want to take dumps of my MariaDB 10.3 on the host from mysql-backup in docker. My docker compose file looks like this:

---
services:
  mysql-backup:
    image: databack/mysql-backup:master
    container_name: mysql-dump-baremetal-test
    restart: 'no'
    environment:
      - RUN_ONCE=true
      - DEBUG=true
      - RETENTION=2w
      - DB_DUMP_FREQ=1440
      - DB_DUMP_BEGIN=2345
      - DB_DUMP_TARGET=/db
      - DB_DUMP_SAFECHARS=true
      - DB_SERVER=/var/run/mysqld/mysqld.sock
      - DB_USER=${DB_USER}
      - DB_PASS=${DB_PW}
      - TZ=Europe/Berlin
    volumes:
      - ./data/dumps:/db
      - /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
    user: "0"
    command: dump -v 1

But, when I'm running the container, it just runs forever and doesn't produce any output. The only output I got was mysql-dump-baremetal-test | time="2024-06-25T14:05:05Z" level=debug msg="starting dump"

From inside the Container:

efe176c09c75:/# ps
PID   USER     TIME  COMMAND
    1 root      0:00 {entrypoint} /bin/sh /entrypoint dump -v 1
    7 root      0:00 /mysql-backup dump -v 1
   12 root      0:00 bash
   18 root      0:00 ps

When I try to start the dump from inside the container: efe176c09c75:/# ./entrypoint dump -v 1 --server $DB_SERVER --user $DB_USER --pass $DB_PASS --once I got the following Error: FATA[0000] if any flags in the group [once begin] are set none of the others can be; [begin once] were all set


The Problem was, that the "RUN_ONCE=true" only works, when you set the

explicitly to nothing:

since the default will set some values for them.

efe176c09c75:/# echo $DB_DUMP_FREQ
1440
efe176c09c75:/# echo $DB_DUMP_BEGIN
2345
efe176c09c75:/# DB_DUMP_FREQ=
efe176c09c75:/# DB_DUMP_BEGIN=
efe176c09c75:/# echo $DB_DUMP_FREQ

efe176c09c75:/#
efe176c09c75:/# echo $DB_DUMP_BEGIN

efe176c09c75:/#

After that:

efe176c09c75:/# ./entrypoint dump -v 1 --server $DB_SERVER --user $DB_USER --pass $DB_PASS --once
DEBU[0000] starting dump
INFO[0000] beginning dump 2024-06-25T14:21:13Z           run=62b3ff95-f174-4a28-9165-5057eb3f294d
DEBU[0003] uploading via protocol file from db_backup_2024-06-25T14-21-13Z.tgz  run=62b3ff95-f174-4a28-9165-5057eb3f294d
DEBU[0003] completed copying 10285312 bytes              run=62b3ff95-f174-4a28-9165-5057eb3f294d
INFO[0003] Backup complete
efe176c09c75:/# 

So the running docker compose config looks like this:

---
services:
  mysql-backup:
    image: databack/mysql-backup:master
    container_name: mysql-dump-baremetal-test
    restart: 'no'
    environment:
      - RUN_ONCE=true
      - DEBUG=true
      - RETENTION=2w
      - DB_DUMP_FREQ=
      - DB_DUMP_BEGIN=
      - DB_DUMP_TARGET=/db
      - DB_DUMP_SAFECHARS=true
      - DB_SERVER=/var/run/mysqld/mysqld.sock
      - DB_USER=${DB_USER}
      - DB_PASS=${DB_PW}
      - TZ=Europe/Berlin
    volumes:
      - ./data/dumps:/db
      - /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
    user: "0"
    command: dump -v 1

If you need any further/additional infos, please ask.

deitch commented 2 months ago

I see the issue you are describing.

  1. If it has an error, it should not hang, but should exit with the error.
  2. If the env vars are not set, it should not set defaults for them, as that messes up run-once

We can try to fix those.

Separately, your compose file looks like:

services:
  mysql-backup:
    image: databack/mysql-backup:master
    container_name: mysql-dump-baremetal-test
    restart: 'no'
    environment:
      - RUN_ONCE=true
      - DEBUG=true
      - RETENTION=2w
      - DB_DUMP_FREQ=1440
      - DB_DUMP_BEGIN=2345
      - DB_DUMP_TARGET=/db
      - DB_DUMP_SAFECHARS=true
      - DB_SERVER=/var/run/mysqld/mysqld.sock
      - DB_USER=${DB_USER}
      - DB_PASS=${DB_PW}
      - TZ=Europe/Berlin
    volumes:
      - ./data/dumps:/db
      - /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
    user: "0"
    command: dump -v 1

So it has both RUN_ONCE=true and DB_DUMP_BEGIN=2345. Isn't the error the correct behaviour here? Or are you raising both of these issues independently, that it should have errored out in this case rather than hang, and that it should handle the "I didn't set those at all" case as well?

deitch commented 2 months ago

I see why it didn't catch it. All of the other dump-time configuration flags are DB_DUMP_*, and we use that with the library to catch it; they also all follow DB_DUMP_ with the exact same name as the CLI flag. RUN_ONCE is the only one that:

The solution is simple, since we have not yet GAed 1.0.0; change the env var. Then it works correctly. PR coming shortly on that.

deitch commented 2 months ago

As for the second issue, I do not see it. If I do not set the env vars at all for DB_DUMP_BEGIN, it does the right thing. Sure, there is a default, but it recognizes that the default was used, not explicitly set, so it allows once to override it.