Skip to content

Instantly share code, notes, and snippets.

@Kline-
Last active September 5, 2023 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kline-/859cc351bf251598a8902146af12278c to your computer and use it in GitHub Desktop.
Save Kline-/859cc351bf251598a8902146af12278c to your computer and use it in GitHub Desktop.
Backup MariaDB data running in a Docker container
#!/bin/bash
BASE=`date -I`-mariabackup
CONT=mariadb
DEST=/home/matt/_gdrive/mariabackup/
MAGE=365
OWNER=matt:matt
TGZ=$DEST$BASE.tgz
# check if the container is running
if [ "$( docker container inspect -f '{{.State.Running}}' $CONT )" == "true" ]; then
docker exec $CONT mariabackup --backup --target-dir=/var/lib/mysql/$BASE --user=root
cd $DEST
tar -c -C /var/lib/mysql/$BASE/ . | gzip --rsyncable > $TGZ
chown $OWNER $TGZ
rm -rf /var/lib/mysql/$BASE
#cleanup older than max age
if [ "$(find $DEST -mtime +$MAGE | wc -l)" -ge 1 ]; then
echo "Cleaning up backups older than $MAGE days..."
find $DEST -mtime +$MAGE -delete -print
fi
else
echo "$CONT is not running. Exiting."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment