Last active
June 27, 2024 13:09
-
-
Save alinefr/f301d1a6352ed856d5bf87ec38db733e to your computer and use it in GitHub Desktop.
Generate mysqldump from mysql for wordpress running in a container.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ------------------------------------------------------------------------- | |
# Script to generate mysqldump from wordpress running inside a container | |
# | |
# Copyright (C) 2016 Aline Freitas <[email protected]> | |
# | |
# It has two command line arguments: | |
# -c: the docker container name | |
# -d: the html volume in your host pointing to the root of your wordpress installation. | |
# It needs to be where exist the wp-config.php file. | |
# | |
# Usage: | |
# wp-mysqldump.sh -c <container> -d <docker html path> | |
# YOu may need also to set the following variables | |
# Where you want to save your backups. They will be created in a subdir with the same name | |
# as the container. | |
BACKUP_DIR="/root/mysqlbackups" | |
# The generated extension for the backup file. | |
BACKUP_EXTENSION="*.mysqldump" | |
# The maximum amount of files to keep. If you dont want to delete any of then just comment | |
# out this line. | |
BACKUP_MAX_FILES=30 | |
usage() { | |
echo "Usage: $0 -c <container> -d <docker html path>" | |
echo "Example: $0 -c www-mariadb -d /data/www.lambda3.com.br/html" | |
} | |
while getopts ":c:d:" opt; do | |
case "$opt" in | |
c) | |
CONTAINER=$OPTARG | |
;; | |
d) | |
DOCKER_HTML_PATH=$OPTARG | |
;; | |
*) | |
echo 'Invalid argument.' | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
if [ $# -eq 0 ]; then | |
usage | |
exit 1 | |
fi | |
if [ -z ${CONTAINER} ] || [ -z ${DOCKER_HTML_PATH} ]; then | |
echo "Missing parameters." | |
usage | |
exit 1 | |
fi | |
if ! docker ps -q -f name=$CONTAINER &>/dev/null | |
then | |
echo "Docker container not running..." | |
exit 1 | |
fi | |
wp_get_value() { | |
local wp_config_path="$DOCKER_HTML_PATH/wp-config.php" | |
if [ -r $wp_config_path ]; then | |
echo $(sed -n "/^ *define( *'$1', *'\([^']*\)'.*/ {s//\1/p;q;}" $wp_config_path) | |
else | |
echo "Missing wp-config.php." | |
return 1 | |
fi | |
} | |
backup_generate() { | |
test -d ${BACKUP_DIR}/${CONTAINER} || mkdir ${BACKUP_DIR}/${CONTAINER} | |
docker exec $CONTAINER /usr/bin/mysqldump --user=$(wp_get_value DB_USER) --password=$(wp_get_value DB_PASSWORD) $(wp_get_value DB_NAME) > ${BACKUP_DIR}/${CONTAINER}/$CONTAINER-$(date +"%Y%m%d-%H%M").sqldump | |
} | |
remove_older() { | |
filenum=$(ls -1 $BACKUP_DIR/$CONTAINER | grep $BACKUP_EXTENSION | wc -l) | |
if [ $filenum -ge $BACKUP_MAX_FILES ]; then | |
rm $(ls -1t $BACKUP_DIR/$CONTAINER | tail -n+$(($BACKUP_MAX_FILES++))) | |
fi | |
} | |
backup_generate | |
if [ $? -ne 0 ]; then | |
echo "Error generating backup." | |
exit 1 | |
fi | |
if [ -z $BACKUP_MAX_FILES ]; then | |
remove_older | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment