In this article, we will discuss the Docker image databack/mysql-backup, its configuration using Docker Compose, and some of the key features and parameters it offers for efficient MySQL database backups.
Docker Compose Configuration
Below is a sample Docker Compose configuration for the databack/mysql-backup Docker image:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
x-defaults: &defaults image: databack/mysql-backup@sha256:fb35135e01f1746ea46dbabb6ee2f85cc1f0b42cb3c79c8718e8d87288bf288d restart: unless-stopped user: "0:0" extra_hosts: - "host.docker.internal:host-gateway" environment: &default-environment DB_SERVER: host.docker.internal NO_DATABASE_NAME: false DB_USER: backup DB_PASS_FILE: /run/secrets/db_password DB_DUMP_CRON: 0 2 * * * COMPRESSION: bzip2 DB_DUMP_TARGET: /backup DB_DUMP_SAFECHARS: true MYSQLDUMP_OPTS: "--single-transaction --quick --hex-blob" TZ: UTC secrets: - db_password command: dump volumes: - ./backups:/backup services: mysql-backup: <<: *defaults test: <<: *defaults restart: no environment: <<: *default-environment RUN_ONCE: true secrets: db_password: file: ./db_password.txt |
Environment Variables
In this configuration, several environment variables are defined. Let’s review them in detail:
DB_SERVER: Specifies the hostname to connect to the database. Required.DB_PORT: Port to use to connect to the database, defaulting to 3306 if not specified.DB_USER: The username for the database.DB_PASS_FILE: Path to the file containing the database password.NO_DATABASE_NAME: When set tofalse, specifies the backup is not for a specific database.DB_DUMP_CRON: Sets the dump schedule using standard crontab syntax. In this example, it’s set to back up the database at 2 AM every day.COMPRESSION: Specifies the compression to use; in this case, it’sbzip2.DB_DUMP_TARGET: The directory where the backup will be stored; here, it is/backup.DB_DUMP_SAFECHARS: Replaces certain characters in the dump filename to ensure compatibility across various systems.MYSQLDUMP_OPTS: Additionalmysqldumpoptions. In this example, the options--single-transaction,--quick, and--hex-blobare used.TZ: Time zone setting; here, it’s set toUTC.
The Options of mysqldump (MYSQLDUMP_OPTS)
The MYSQLDUMP_OPTS parameter allows additional options to be passed to mysqldump:
--single-transaction: Ensures the dump is a consistent state by isolating the dump in a single transaction, ideal for InnoDB tables.--quick: Retrieves rows from the database table in smaller chunks to preserve memory.--hex-blob: Dumps binary strings in hexadecimal format to ensure data integrity for binary columns.
Using Secrets
The configuration uses Docker secrets to securely pass the database password. The secret db_password is defined under secrets, and the corresponding file is ./db_password.txt. The application can then access the password from /run/secrets/db_password.
Extending YAML Syntax to Avoid Text Duplication
The YAML configuration uses anchors and aliases to extend the syntax and avoid repetitive text. Here’s how it’s done:
- The
x-defaultsanchor stores the common configuration under&defaults, which is then referenced using<<: *defaults. - The shared environment variables are stored under
&default-environmentand referenced using<<: *default-environment.
This technique maintains clarity and reduces redundancy in the configuration.
Creating Backup Accounts in MySQL
Before using the Docker image, a backup account must be created in MySQL with the necessary permissions. Execute the following SQL commands to create a user backup:
|
1 2 3 |
CREATE USER 'backup'@'%' IDENTIFIED BY 'xxxxxx'; GRANT SELECT, SHOW VIEW, PROCESS, LOCK TABLES ON *.* TO 'backup'@'%'; FLUSH PRIVILEGES; |
This grants the backup user the necessary permissions to perform read-only operations required for backup purposes.
To get started with the databack/mysql-backup Docker image, follow these straightforward steps.
Running the mysql-backup Service
To run the mysql-backup service in the background, use the following command:
|
1 |
docker-compose up -d mysql-backup |
This command starts the mysql-backup service as defined in your compose.yml file. The -d flag ensures that the service runs as a daemon in the background, allowing it to perform scheduled backups without user intervention.
Testing the Backup Configuration
To verify that your backup configuration is set up correctly, you can run the test service. This service is configured to execute the backup process once and then exit. Use the following command:
|
1 |
docker-compose up test |
By running this command, the test service will execute and print logs to the console. If the backup configuration is correct, you should see messages indicating that the backup process has successfully completed.
The databack/mysql-backup Docker image is a comprehensive solution for scheduled backups of MySQL databases. By leveraging Docker Compose and the robust configuration options offered, including the use of secrets and customizable mysqldump parameters, users can efficiently manage and secure their database backups. The use of YAML anchors and aliases simplifies the configuration, promoting maintainability and clarity.