tl;dr you want in container DB dumps for consistent backups via an external backup method grabbing your volume or bind mount directory.
Enable the LinuxServer.io docker mod for cron by adding the env var to your compose / run script:
DOCKER_MODS=linuxserver/mods:universal-cron
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
Use CRON to set a regular SQL dump e.g. add the following to /config/crontabs/root
0 */4 * * * /config/backups/periodic-backup.sh
Verify that your CRON jobs are running successfully by validating one has dumped correctly.
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