Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Aterfax/16f7665eb4f5b2d99fa16cb58462a794 to your computer and use it in GitHub Desktop.
Save Aterfax/16f7665eb4f5b2d99fa16cb58462a794 to your computer and use it in GitHub Desktop.
In container MariaDB / MySQL dumps for Linuxserver.io MariaDB/MySQL containers

In container MariaDB / MySQL dumps for Linuxserver.io MariaDB/MySQL containers

Motivation

tl;dr you want in container DB dumps for consistent backups via an external backup method grabbing your volume or bind mount directory.

Step 1

Enable the LinuxServer.io docker mod for cron by adding the env var to your compose / run script:

DOCKER_MODS=linuxserver/mods:universal-cron

Step 2

Make a backup script which uses the existing container variables and paths.

Note that I decided to purge the backups after 7 days since I have a backup process external to the container collecting these dumps.

#!/bin/bash
# shellcheck shell=bash
mkdir -p /config/backups/
mysqldump --all-databases -p"$MYSQL_ROOT_PASSWORD" | gzip > /config/backups/$(date --iso-8601=minutes).sql
find /config/backups -type f \( -name "*.sql" -or -name "*.sql.gz" \) -mtime +7 -delete

Make this script available in the container e.g. mount it on a path or add it to an existing volume / mounted folder.

I chose to add it to /config/backups/periodic-backup.sh

Step 3

Use CRON to set a regular SQL dump e.g. add the following to /config/crontabs/root

0 */4 * * *       /config/backups/periodic-backup.sh

Step 4

Verify that your CRON jobs are running successfully by validating one has dumped correctly.

Step 5 (Optional)

Consider using a tool like Healthchecks.io to ensure regular dumps are happening.

e.g. I would change my script above to be:

#!/bin/bash
# shellcheck shell=bash

# Set the HC_ENDPOINT  and HC_UUID variables to your own choices
HC_UUID="your-uuid-here"
HC_ENDPOINT="https://healthchecks.domain.com/ping/${HC_UUID}"

callHC() {
  local suffix=$1
  curl -fsS -m 10 --retry 5 -o /dev/null "${HC_ENDPOINT}/${suffix}"
}

# Let healthchecks know we're starting the backup process
callHC start

# Initialize a variable to store the sum of exit codes (0 indicates success overall)
exitcodesum=0

mkdir -p "/config/backups/"
exitcodesum=$((exitcodesum+$?))

mysqldump --all-databases -p"$MYSQL_ROOT_PASSWORD" | gzip > "/config/backups/$(date --iso-8601=minutes).sql.gz"
exitcodesum=$((exitcodesum+$?))

find "/config/backups/" -type f \( -name "*.sql" -or -name "*.sql.gz" \) -mtime +7 -delete
exitcodesum=$((exitcodesum+$?))

# Report the final status to healthchecks
callHC $exitcodesum
exit $exitcodesum  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment