-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from SrZorro/master
- Loading branch information
Showing
15 changed files
with
193 additions
and
64 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM blacklabelops/volumerize | ||
|
||
RUN apk add --no-cache \ | ||
mongodb-tools | ||
|
||
COPY postexecute /postexecute | ||
COPY preexecute /preexecute |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Using a prepost strategy to create MongoDB backups | ||
|
||
Volumerize can execute scripts before and after the backup process. | ||
|
||
With this prepost strategy you can create dump of your MongoDB containers and save it with Volumerize. | ||
|
||
## Environment Variables | ||
|
||
Aside of the required environment variables by Volumerize, this prepost strategy will require a couple of extra variables. | ||
MONGO_USERNAME MONGO_PASSWORD MONGO_HOST MONGO_PORT | ||
| Name | Description | | ||
| -------------- | ---------------------------------------------------------- | | ||
| MONGO_USERNAME | Username of the user who will perform the restore or dump. | | ||
| MONGO_PASSWORD | Password of the user who will perform the restore or dump. | | ||
| MONGO_HOST | MongoDB IP or domain. | | ||
| MONGO_PORT | MongoDB port. | | ||
|
||
## Example with Docker Compose | ||
|
||
```YAML | ||
version: "3" | ||
|
||
services: | ||
mongodb: | ||
image: mongo | ||
ports: | ||
- 27017:27017 | ||
environment: | ||
- MONGO_INITDB_ROOT_USERNAME=root | ||
- MONGO_INITDB_ROOT_PASSWORD=1234 | ||
volumes: | ||
- mongodb:/data/db | ||
|
||
volumerize: | ||
build: ./prepost_strategies/mongodb/ | ||
environment: | ||
- VOLUMERIZE_SOURCE=/source | ||
- VOLUMERIZE_TARGET=file:///backup | ||
- MONGO_USERNAME=root | ||
- MONGO_PASSWORD=1234 | ||
- MONGO_PORT=27017 | ||
- MONGO_HOST=mongodb | ||
volumes: | ||
- volumerize-cache:/volumerize-cache | ||
- backup:/backup | ||
depends_on: | ||
- mongodb | ||
|
||
volumes: | ||
volumerize-cache: | ||
mongodb: | ||
backup: | ||
``` | ||
Then execute `docker-compose exec volumerize backup` to create a backup of your database and `docker-compose exec volumerize restore` to restore it from your backup. |
7 changes: 7 additions & 0 deletions
7
prepost_strategies/mongodb/postexecute/restore/mongorestore.sh
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source /preexecute/utils/check-env.sh | ||
|
||
check_env "mongorestore" "MONGO_USERNAME" "MONGO_PASSWORD" "MONGO_HOST" "MONGO_PORT" | ||
|
||
echo "mongorestore starts" | ||
mongorestore --host ${MONGO_HOST} --port ${MONGO_PORT} --username ${MONGO_USERNAME} --password "${MONGO_PASSWORD}" ${VOLUMERIZE_SOURCE} | ||
echo "Import done" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
source /preexecute/utils/check-env.sh | ||
|
||
check_env "mongodump" "MONGO_USERNAME" "MONGO_PASSWORD" "MONGO_HOST" "MONGO_PORT" | ||
|
||
echo "Creating $VOLUMERIZE_SOURCE folder if not exists" | ||
mkdir -p $VOLUMERIZE_SOURCE | ||
|
||
echo "mongodump starts" | ||
mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} --username ${MONGO_USERNAME} --password "${MONGO_PASSWORD}" --out ${VOLUMERIZE_SOURCE} |
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source /preexecute/utils/check-env.sh | ||
|
||
check_env "mysqlimport" "MYSQL_PASSWORD" "MYSQL_USERNAME" "MYSQL_HOST" "MYSQL_DATABASE" | ||
|
||
echo "mysql import starts" | ||
pv ${VOLUMERIZE_SOURCE}/dump-${MYSQL_DATABASE}.sql | mysql -u ${MYSQL_USERNAME} -p${MYSQL_PASSWORD} $MYSQL_DATABASE | ||
echo "Import done" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
source /preexecute/utils/check-env.sh | ||
|
||
check_env "Mysqldump" "MYSQL_PASSWORD" "MYSQL_USERNAME" "MYSQL_HOST" "MYSQL_DATABASE" | ||
|
||
echo "Creating $VOLUMERIZE_SOURCE folder if not exists" | ||
mkdir -p $VOLUMERIZE_SOURCE | ||
|
||
# Based on this answer https://stackoverflow.com/a/32361604 | ||
SIZE_BYTES=$(mysql --skip-column-names -u ${MYSQL_USERNAME} -p${MYSQL_PASSWORD} $MYSQL_DATABASE -e "SELECT ROUND(SUM(data_length * 0.8), 0) FROM information_schema.TABLES WHERE table_schema='${MYSQL_DATABASE}';") | ||
|
||
echo "mysqldump starts (Progress is aproximated)" | ||
mysqldump --databases "${MYSQL_DATABASE}" --single-transaction --add-drop-database --user="${MYSQL_USERNAME}" --password="${MYSQL_PASSWORD}" --host="${MYSQL_HOST}" | pv --progress --size "$SIZE_BYTES" > ${VOLUMERIZE_SOURCE}/dump-${MYSQL_DATABASE}.sql |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/bin/sh | ||
|
||
## | ||
# @private | ||
# Executed if a variable is not set or is empty string | ||
# @param1 - Name of the failed environment variable | ||
## | ||
function _check_env_failed { | ||
echo "Environment variable $1 is not set." | ||
echo "Environment variables failed, exit 1" | ||
exit 1 | ||
} | ||
|
||
## | ||
# @private | ||
# Executed if a variable is setted | ||
# @param1 - Name of the environment variable | ||
## | ||
function _check_env_ok { | ||
echo "Env var $1 ok." | ||
} | ||
|
||
## | ||
# Use it to check if environment variables are set | ||
# @param1 - Name of the context | ||
# @param2 to ∞ - Environment variables to check | ||
## | ||
function check_env { | ||
echo "Checking environment variables for $1." | ||
|
||
for e_var in "$@"; do | ||
if [ $e_var = $1 ]; then continue; fi # Jump first arg | ||
|
||
# Check if env var is setted, if not raise error | ||
if [ "${!e_var}" = "" ]; then | ||
_check_env_failed $e_var; | ||
else | ||
_check_env_ok $e_var; | ||
fi | ||
|
||
done | ||
echo "Environment variables ok." | ||
} |
Oops, something went wrong.