Skip to content

Instantly share code, notes, and snippets.

@sebastian13
Last active July 28, 2023 20:42
Show Gist options
  • Select an option

  • Save sebastian13/cc58304f7b177ac1d16d7671dc67efb9 to your computer and use it in GitHub Desktop.

Select an option

Save sebastian13/cc58304f7b177ac1d16d7671dc67efb9 to your computer and use it in GitHub Desktop.

Download

curl -JO https://gist.githubusercontent.com/sebastian13/cc58304f7b177ac1d16d7671dc67efb9/raw/docker-mysql-dump.sh
curl -JO https://gist.githubusercontent.com/sebastian13/cc58304f7b177ac1d16d7671dc67efb9/raw/docker-mysql-restore.sh
chmod +x docker-mysql-(dump|restore).sh

Example: .env

MYSQL_DATABASE=example
MYSQL_ROOT_PASSWORD=123456

Example: docker-compose.yml

services:
  mysql:
    image: mariadb
    ...
    env_file: .env
    
  other:
    ...
    environment:
      - WORDPRESS_DB_NAME=${MYSQL_DATABASE}
      - WORDPRESS_DB_PASSWORD=${MYSQL_ROOT_PASSWORD}      
#!/bin/bash
# Change to the script's directory & create directory
cd $(dirname "$(readlink -f "$0")")
mkdir -p ./dbdumps
# Start mysql service
docker-compose --log-level ERROR up -d mysql
# Wait
i=20
while (( i >= 1 )); do
sleep 1
echo -ne
echo -ne "Wait for DB to initialize. Creating Dump in $(( i-- )) seconds ... "'\r'
done
# Load database name + root password
source .env
# Delete old Backups
find ./dbdumps/* -atime +7 -exec rm {} \;
docker-compose exec -T mysql /usr/bin/mysqldump -u root \
--password=$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE \
| gzip --rsyncable > ./dbdumps/`date +\%Y\%m\%d`-$MYSQL_DATABASE.sql.gz
#!/bin/bash
# Start mysql service
docker-compose --log-level ERROR up -d mysql
# Load database name + root password
source .env
# Find latest dumps
latest1=$(ls -1t ./dbdumps/*.sql.gz | head -1)
latest2=$(ls -1t ./dbdumps/*.sql.gz | head -n2 | tail -n1)
latest3=$(ls -1t ./dbdumps/*.sql.gz | head -n3 | tail -n1)
# Select dump
echo "Which dump should be used for restoring?"
select result in $latest1 $latest2 $latest3
do
[ $result ] && break
done
# Wait
i=15
while (( i >= 1 )); do
sleep 1
echo -ne
echo -ne "Wait for DB to initialize. Starting Restore of $result in $(( i-- )) seconds ... "'\r'
done
# Restore
gunzip --keep --stdout $result | docker-compose exec -T mysql \
/usr/bin/mysql -u root --password=${MYSQL_ROOT_PASSWORD} ${MYSQL_DATABASE}
#!/bin/bash
# Change to the script's directory & create directory
cd $(dirname "$(readlink -f "$0")")
mkdir -p ./dbdumps
# Start mysql service
docker-compose --log-level ERROR up -d mysql
# Wait
i=20
while (( i >= 1 )); do
sleep 1
echo -ne
echo -ne "Wait for DB to initialize. Creating Dump in $(( i-- )) seconds ... "'\r'
done
# Load database name + root password
source .env
# Delete old Backups
find ./dbdumps/* -atime +7 -exec rm {} \;
docker-compose exec -T mysql /usr/bin/mysqldump -u root \
--password=$MYSQL_ROOT_PASSWORD \
--ignore-table=zabbix.acknowledges \
--ignore-table=zabbix.alerts \
--ignore-table=zabbix.auditlog \
--ignore-table=zabbix.auditlog_details \
--ignore-table=zabbix.escalations \
--ignore-table=zabbix.events \
--ignore-table=zabbix.history \
--ignore-table=zabbix.history_log \
--ignore-table=zabbix.history_str \
--ignore-table=zabbix.history_str_sync \
--ignore-table=zabbix.history_sync \
--ignore-table=zabbix.history_text \
--ignore-table=zabbix.history_uint \
--ignore-table=zabbix.history_uint_sync \
--ignore-table=zabbix.trends \
--ignore-table=zabbix.trends_uint \
$MYSQL_DATABASE \
| gzip --rsyncable > ./dbdumps/`date +\%Y\%m\%d`-$MYSQL_DATABASE.sql.gz
@sagaban
Copy link

sagaban commented May 20, 2021

I just had a problem with source .env , but replacing this line with . ./.env worked. Thanks for these scripts, amazing work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment