Skip to content

Instantly share code, notes, and snippets.

@kernelkaribou
Last active June 19, 2022 20:03
Show Gist options
  • Save kernelkaribou/fce1147f0647d1ee3eed525fa75d0f3f to your computer and use it in GitHub Desktop.
Save kernelkaribou/fce1147f0647d1ee3eed525fa75d0f3f to your computer and use it in GitHub Desktop.
Backup of all postgres and mariadb docker containers using native dump methods
#!/bin/bash
######################################################################
# Bash script for backing up varying docker database containers
#
# This will search for containers using common database image names
# Currently supports postgrest and mariadb
# This uses the common pg_dump and mysqldump methods of backup
# It will also gzip the results if the -c flag is passed
# Note: This is built with linuxserver MariaDB container but should
# work with MariaDB official if using the MYSQL_* env variables.
#
# TODO
# - Check if volume is docker volume or bind
# - Add archiving feature to create unique backups and not clobber (personally not needed)
#
# Last update: 2022-06-19
# Author: kernelkaribou@github
######################################################################
# Leave as local to have the backups at the same directory level of the container volume.
# otherwise change to a path where you want all db dumps to be placed
backup_dir="LOCAL"
Help()
{
# Display Help
echo
echo "Script for backing up Postgres and MariaDB containers."
echo
echo "Syntax: scriptName.sh [-a|p|m|c|h]"
echo
echo "options:"
echo "-a Backup all database containers"
echo "-p Backup only Postgres containers"
echo "-m Backup only MariaDB containers"
echo "-c Compress the backups with gzip (Default no)"
echo "-h Display this help information"
echo
}
dbDump()
{
db_type=$1
# Backup Postgres Containers
for container in $(sudo docker ps --format '{{.Names}} {{.Image}}' | grep "$db_type" | awk '{print $1}') #search for containers using db_type image
do
echo "$(date +"%Y-%m-%d %T") - Starting database dump ($db_type) for $container"
# Get container environment details
container_env=$(sudo docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' $container)
if [ "$backup_dir" = "LOCAL" ]; then
# Get container mount volume for data, should only be one
# TODO (if the container is using a docker volume this could be an issue and needs to be revisited)
container_path=$(sudo docker inspect --format='{{range .Mounts}}{{println .Source}}{{end}}' $container)
db_backup_dir="${container_path%/*}/db_backup"
else
db_backup_dir="$backup_dir/$container"
fi
db_backup_file="$db_backup_dir/$container.sql"
# Create directory for the database backup
mkdir -p $db_backup_dir
# Performing database dump based upon type of db engine
if [ "$db_type" = "postgres" ]; then
db_name=$(echo $container_env | sed 's/.*POSTGRES_DB=//' | cut -d ' ' -f1)
db_user=$(echo $container_env | sed 's/.*POSTGRES_USER=//' | cut -d ' ' -f1)
$(sudo docker exec -i $container /usr/bin/pg_dump -U $db_user $db_name > $db_backup_file)
elif [ "$db_type" = "mariadb" ]; then
db_name=$(echo $container_env | sed 's/.*MYSQL_DATABASE=//' | cut -d ' ' -f1)
db_pass=$(echo $container_env | sed 's/.*MYSQL_ROOT_PASSWORD=//' | cut -d ' ' -f1)
$(sudo docker exec $container /usr/bin/mysqldump -uroot -p${db_pass} $db_name > $db_backup_file)
fi
# Compress the .sql file if -c flag was passed
if [[ $compress ]]; then
echo "$(date +"%Y-%m-%d %T") - Compressing $container.sql"
gzip -f $db_backup_file
db_backup_file+=".gz"
fi
echo "$(date +"%Y-%m-%d %T") - Database dump ($db_type) of $container completed. Backup Size: $(ls -lh $db_backup_file | awk '{print $5}')"
done
}
# Compress flag was set
if grep -q 'c' <<< ${@}; then compress=true; fi
# Get the options
while getopts ":hapmcv" option; do
case $option in
h) # display Help
Help
exit;;
a) # backup all databases
echo "$(date +"%Y-%m-%d %T") - Backing up all database containers"
dbDump "postgres"
dbDump "mariadb"
exit;;
p) # backup postgres containers
echo "$(date +"%Y-%m-%d %T") - Backing up Postgres database containers"
dbDump "postgres"
exit;;
m) # backup mariadb containers
echo "$(date +"%Y-%m-%d %T") - Backing up MariaDB database containers"
dbDump "mariadb"
exit;;
\?) # Invalid option
echo "Error: Invalid option, -h for help"
exit;;
esac
done
echo "Missing option, -h for help"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment