Giter Site home page Giter Site logo

ix-ai / mariadb-backup Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 2.0 33 KB

Docker image to backup a MySQL or MariaDB container.

License: MIT License

Dockerfile 13.30% Shell 86.70%
mariadb-backup backup-container mysql-container restore docker cli-client docker-image database

mariadb-backup's Introduction

mariadb-backup

Pipeline Status Docker Stars Docker Pulls Gitlab Project

The mariadb-backup Docker image will provide you a container to backup and restore a MySQL or MariaDB database container.

The backup is made with mydumper, a fast MySQL backup utility.

Usage

To backup a MySQL or MariaDB database, you simply specify the credentials and the host. You can optionally specify the database as well.

Environment variables

Variable FILE Support Default Mandatory Description
DB_HOST DB_HOST__FILE - yes The host to connect to
DB_PASS DB_PASS__FILE - yes The password for the SQL server
DB_NAME DB_NAME__FILE - no If specified, only this database will be backed up
DB_PORT N/A 3306 no The port of the SQL server
DB_USER DB_USER__FILE root no The user to connect to the SQL server
MODE N/A BACKUP no One of BACKUP or RESTORE
BASE_DIR N/A /backup no Path of the base directory (aka working directory)
RESTORE_DIR N/A - no Name of a backup directory to restore
BACKUP_UID N/A 666 no UID of the backup
BACKUP_GID N/A 666 no GID of the backup
UMASK N/A 0022 no Umask which should be used to write the backup files
OPTIONS N/A -c / -o no Options passed to mydumper / myloader

The following environment variables support setting via *__FILE, where

Please note the backup will be written to /backup by default, so you might want to mount that directory from your host.

Example Docker CLI client

To create a backup from a MySQL container via docker CLI client:

docker run --name my-backup -e DB_HOST=mariadb -e DB_PASS=amazing_pass -v /var/mysql_backups:/backup registry.gitlab.com/ix.ai/mariadb-backup:latest

The container will stop automatically as soon as the backup has finished. To create more backups in the future simply start your container again:

docker start my-backup

To restore a backup into a MySQL container via docker CLI client:

docker run --name my-restore -e DB_HOST=mariadb -e DB_PASS=amazing_pass -e MODE=RESTORE -v /var/mysql_backups:/backup registry.gitlab.com/ix.ai/mariadb-backup:latest

Script example

To back up multiple databases, all running in docker, all labeled with mariadb-backup:

#!/usr/bin/env bash
/bin/mkdir -p /mariadb-backup

/usr/bin/docker pull registry.gitlab.com/ix.ai/mariadb-backup:latest

for CONTAINER in $(/usr/bin/docker ps -f label=mariadb-backup --format='{{.Names}}'); do
  DB_PASS=$(/usr/bin/docker inspect ${CONTAINER}|/usr/bin/jq -r '.[0]|.Config.Env[]|select(test("^MARIADB_ROOT_PASSWORD.*"))'|/bin/sed -n 's/^MARIADB_ROOT_PASSWORD=\(.*\)/\1/p')
  DB_NAME=$(/usr/bin/docker inspect ${CONTAINER}|/usr/bin/jq -r '.[0]|.Config.Env[]|select(test("^MARIADB_DATABASE.*"))'|/bin/sed -n 's/^MARIADB_DATABASE=\(.*\)/\1/p')
  DB_NET=$(/usr/bin/docker inspect ${CONTAINER}|/usr/bin/jq -r '.[0]|.NetworkSettings.Networks|to_entries[]|.key')
  if [[ -n "${DB_PASS}" ]]; then
    /usr/bin/docker run --rm --name ${CONTAINER}-backup -e DB_PASS=${DB_PASS} -e DB_HOST=${CONTAINER} -e DB_NAME=${DB_NAME} --network ${DB_NET} -v /mariadb-backup:/backup registry.gitlab.com/ix.ai/mariadb-backup:latest
  fi
done

Configuration

Mode

By default the container backups the database. However, you can change the mode of the container by setting the following environment variable:

  • MODE: Sets the mode of the backup container while [BACKUP|RESTORE]

Base directory

By default the base directory /backup is used. However, you can overwrite that by setting the following environment variable:

  • BASE_DIR: Path of the base directory (aka working directory)

Restore directory

By default the container will automatically restore the latest backup found in BASE_DIR. However, you can manually set the name of a backup directory underneath BASE_DIR:

  • RESTORE_DIR: Name of a backup directory to restore

This option is only required when the container runs in in RESTORE mode.

UID and GID

By default the backup will be written with UID and GID 666. However, you can overwrite that by setting the following environment variables:

  • BACKUP_UID: UID of the backup
  • BACKUP_GID: GID of the backup

umask

By default a umask of 0022 will be used. However, you can overwrite that by setting the following environment variable:

  • UMASK: Umask which should be used to write the backup files

mydumper / myloader CLI options

By default mydumper is invoked with the -c (compress backup) and myloader with the -o (overwrite tables) CLI option. However, you can modify the CLI options by setting the following environment variable:

  • OPTIONS: Options passed to mydumper (when MODE is BACKUP) or myloader (when MODE is RESTORE)

Tags and Arch

Starting with version v0.0.3, the images are multi-arch, with builds for amd64, arm64, armv7. Support for armv6 has been dropped.

  • vN.N.N - for example v0.0.2
  • latest - always pointing to the latest version
  • dev-master - the last build on the master branch

Resources

Credits

Special thanks to confirm/docker-mysql-backup, which this project uses heavily.

mariadb-backup's People

Contributors

mgsousa avatar tlex avatar

Watchers

 avatar  avatar

mariadb-backup's Issues

Probably missing ENV variable in restore example in README.md

There is the MODE variable in Environment Variables list, which controls whether it is a backup or restore operation.
This MODE variable is missing from the restore example command:

To restore a backup into a MySQL container via docker CLI client:  
  
docker run --name my-restore -e DB_HOST=mariadb -e DB_PASS=amazing_pass -v /var/mysql_backups:/backup registry.gitlab.com/ix.ai/mariadb-backup:latest

Fix build

Currently, the build is failing for ARM64 and for armv7.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.