Skip to content

Commit

Permalink
Prepost strategy for MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
SrZorro committed Jun 14, 2018
1 parent 5e3caa1 commit e8cfe65
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
7 changes: 7 additions & 0 deletions prepost_strategies/mongodb/Dockerfile
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
55 changes: 55 additions & 0 deletions prepost_strategies/mongodb/README.md
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.
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"
9 changes: 9 additions & 0 deletions prepost_strategies/mongodb/preexecute/backup/mongodump.sh
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}
43 changes: 43 additions & 0 deletions prepost_strategies/mongodb/preexecute/utils/check-env.sh
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."
}

0 comments on commit e8cfe65

Please sign in to comment.