Created
September 9, 2016 05:37
-
-
Save sergiks/47766e5f3cb956f522f09857d329428d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# | |
# Backup database and files of a Wordpress blog running in a Docker container | |
# | |
# by Sergei Sokolov [email protected] | |
# Moscow, 2016 September 08 | |
# | |
DC_MYSQL=dockerwp_mysql_1 | |
DC_FILES=dockerwp_wordpress_1 | |
DEST=/path/to/backups/on/host | |
DATETIME=$(date +%Y%m%d-%H%M%S) | |
BKP_MYSQL="${DATETIME}.sql.gz" | |
BKP_FILES="${DATETIME}.tgz" | |
TMP_MYSQL="/tmp/${BKP_MYSQL}" | |
TMP_FILES="/tmp/${BKP_FILES}" | |
# DATABASE | |
# 1. dump inside Docker | |
sudo docker exec $DC_MYSQL bash -c "mysqldump -u root -p\$MYSQL_ROOT_PASSWORD wordpress | gzip > $TMP_MYSQL" | |
# 2. copy to host | |
sudo docker cp "$DC_MYSQL:$TMP_MYSQL" "$DEST/" | |
# 3. remove temp file inside Docker | |
sudo docker exec $DC_MYSQL rm -f "$TMP_MYSQL" | |
# FILES | |
# 1. create a zipped tar ball inside Docker container | |
sudo docker exec $DC_FILES bash -c "tar -czf $TMP_FILES . " | |
# 2. copy the tarball from Docker container to host | |
sudo docker cp "$DC_FILES:$TMP_FILES" "$DEST/" | |
# 3. remove temp file inside Docker | |
sudo docker exec $DC_FILES rm -f "$TMP_FILES" | |
# Verify | |
ls -lA $DEST |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment