tiredofit / docker-db-backup

Backup multiple database types on a scheduled basis with many customizable options
MIT License
861 stars 125 forks source link

Allow multiple backup targets #21

Closed jwillmer closed 2 years ago

jwillmer commented 4 years ago

You can define lists in environment variables. This way we could us the image to backup multiple targets without the need of hosting the image multiple times.

"MessageBusList:0:Name": "Test"
"MessageBusList:0:UserName": demo
tiredofit commented 4 years ago

Do you mean something like DB2_HOST,DB2_NAME ? I think that would be possible, but multiple time expressions may get a bit messy.

jwillmer commented 4 years ago

I use this in an internal project to define multiple targets, example for this project:

  example-db-backup:
    container_name: example-db-backup
    image: tiredofit/db-backup
    links:
     - example-db
    volumes:
      - ./backups:/backup
    environment:
      - DbTarget:0:DB_TYPE=mariadb
      - DbTarget:0:DB_HOST=maria-host
      - DbTarget:0:DB_NAME=maria-db-name
      - DbTarget:0:DB_USER=maria-user
      - DbTarget:0:DB_PASS=maria-pw
      - DbTarget:0:DB_DUMP_FREQ=1440

      - DbTarget:1:DB_TYPE=mysql
      - DbTarget:1:DB_HOST=mysql-host
      - DbTarget:1:DB_NAME=mysql-db-name
      - DbTarget:1:DB_USER=mysql-user
      - DbTarget:1:DB_PASS=mysql-pw
      - DbTarget:1:DB_DUMP_FREQ=1440
    restart: always

The json equivalent that you can get from this is:

DbTarget: [
  {
    DB_TYPE=mariadb
    DB_HOST=maria-host
    DB_NAME=maria-db-name
    DB_USER=maria-user
    DB_PASS=maria-pw
    DB_DUMP_FREQ=1440
  },
  {
    DB_TYPE=mysql
    DB_HOST=mysql-host
    DB_NAME=mysql-db-name
    DB_USER=mysql-user
    DB_PASS=mysql-pw
    DB_DUMP_FREQ=1440
  }
]
mtucker502 commented 4 years ago

You should run multiple instances of this container. One for each target.

ithelpme commented 4 years ago

I use this in an internal project to define multiple targets, example for this project:

  example-db-backup:
    container_name: example-db-backup
    image: tiredofit/db-backup
    links:
     - example-db
    volumes:
      - ./backups:/backup
    environment:
      - DbTarget:0:DB_TYPE=mariadb
      - DbTarget:0:DB_HOST=maria-host
      - DbTarget:0:DB_NAME=maria-db-name
      - DbTarget:0:DB_USER=maria-user
      - DbTarget:0:DB_PASS=maria-pw
      - DbTarget:0:DB_DUMP_FREQ=1440

      - DbTarget:1:DB_TYPE=mysql
      - DbTarget:1:DB_HOST=mysql-host
      - DbTarget:1:DB_NAME=mysql-db-name
      - DbTarget:1:DB_USER=mysql-user
      - DbTarget:1:DB_PASS=mysql-pw
      - DbTarget:1:DB_DUMP_FREQ=1440
    restart: always

The json equivalent that you can get from this is:

DbTarget: [
  {
    DB_TYPE=mariadb
    DB_HOST=maria-host
    DB_NAME=maria-db-name
    DB_USER=maria-user
    DB_PASS=maria-pw
    DB_DUMP_FREQ=1440
  },
  {
    DB_TYPE=mysql
    DB_HOST=mysql-host
    DB_NAME=mysql-db-name
    DB_USER=mysql-user
    DB_PASS=mysql-pw
    DB_DUMP_FREQ=1440
  }
]

I tried this using docker-compose and it's not working. Getting a No Database Type entered. I would love to get this method working rather than creating each container for each database that needs to be backed up. Thank you.

TucTitan commented 4 years ago

Same here. I would love to have support for backuping multiple databases in a single container

justinforlenza commented 4 years ago

+1 For this

RookieKiwi commented 4 years ago

+1 for this came here just to ask this, perhaps a read file or config file so u can add multiple DBs to so you can have them all backed up at once be great!

tiredofit commented 4 years ago

Other than being able to backup the MariaDB databases by specifying root the image would require a complete overhaul to support this.

I'm calling on the community to fork and submit a PR if you are up to it, otherwise I'm up for accepting bounties. This goes well beyond my intended usage case in my production environments.

BobWs commented 4 years ago

+1

BobWs commented 4 years ago
#!/bin/bash
#
DIR=/volume1/backup/backup-db/baikal/docker-version/
DATESTAMP=$(date +%Y-%m-%d-%H-%M)
DB_USER=backup-user
DB_PASS=Password

# create backup dir if it does not exist
mkdir -p ${DIR}

# remove all backups except the $KEEP latests
KEEP=5
BACKUPS=`find ${DIR} -name "mysqldump-*.gz" | wc -l | sed 's/\ //g'`
while [ $BACKUPS -ge $KEEP ]
do
  ls -tr1 ${DIR}mysqldump-*.gz | head -n 1 | xargs rm -f 
  BACKUPS=`expr $BACKUPS - 1` 
done

# list MySQL databases and dump each
FILENAME=${DIR}mysqldump-${DATESTAMP}.gz
docker exec mariadb /usr/bin/mysqldump --user=$DB_USER --password=$DB_PASS --opt baikal --flush-logs | gzip > $FILENAME

Using this for multiple databases to backup in combination with cron

stefanh12 commented 3 years ago

+1 , currently running 7 instances of this, would really like to get it down to 1 :)

Cartache commented 2 years ago

Or maybe, simply cycling through all the DBs on the host by specifying a specific user which has access to all of it.

tiredofit commented 2 years ago

Version 3.x and later supports multiple databases, I will not be adding multiple host capabilities due to the complexity of scheduling, hosts, and credentials.