Skip to content

Commit

Permalink
Merge pull request #30 from weseek/feat/add-uri-option
Browse files Browse the repository at this point in the history
Feature: add support for "MONGODB_URI" environment variable
  • Loading branch information
skomma committed Jul 9, 2020
2 parents d78a248 + ee83967 commit a6c8726
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 66 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM alpine:3.7
FROM alpine:3.12

LABEL maintainer="WESEEK <info@weseek.co.jp>"

RUN apk add --no-cache \
coreutils \
bash \
tzdata \
py2-pip \
py3-pip \
mongodb-tools \
curl

Expand All @@ -15,7 +15,7 @@ RUN pip install awscli

# install gcloud (also gsutil)
# ref: https://cloud.google.com/sdk/docs?hl=en#install_the_latest_cloud_tools_version_cloudsdk_current_version
ARG CLOUD_SDK_VERSION=281.0.0
ARG CLOUD_SDK_VERSION=299.0.0
ARG CLOUD_SDK_URL="https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz"
RUN curl $CLOUD_SDK_URL | tar xz -C $HOME
RUN $HOME/google-cloud-sdk/install.sh -q --path-update true
Expand Down
68 changes: 39 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ docker run --rm \
[ -e GCP_PROJECT_ID=<Your GCP Project ID> \ ]
-e TARGET_BUCKET_URL=<Target Bucket URL ([s3://...|gs://...])> \
[ -e BACKUPFILE_PREFIX=<Prefix of Backup Filename (default: "backup") \ ]
[ -e MONGODB_URI=<Target MongoDB URI> \ ]
[ -e MONGODB_HOST=<Target MongoDB Host (default: "mongo")> \ ]
[ -e MONGODB_DBNAME=<Target DB name> \ ]
[ -e MONGODB_USERNAME=<DB login username> \ ]
Expand Down Expand Up @@ -63,6 +64,7 @@ docker run --rm \
-e CRONMODE=true \
-e CRON_EXPRESSION=<Cron expression (ex. "CRON_EXPRESSION='0 4 * * *'" if you want to run at 4:00 every day)> \
[ -e BACKUPFILE_PREFIX=<Prefix of Backup Filename (default: "backup") \ ]
[ -e MONGODB_URI=<Target MongoDB URI> \ ]
[ -e MONGODB_HOST=<Target MongoDB Host (default: "mongo")> \ ]
[ -e MONGODB_DBNAME=<Target DB name> \ ]
[ -e MONGODB_USERNAME=<DB login username> \ ]
Expand All @@ -87,11 +89,12 @@ docker run --rm \
[ -e GCP_PROJECT_ID=<Your GCP Project ID> \ ]
-e TARGET_BUCKET_URL=<Target Bucket URL ([s3://...|gs://...])> \
-e TARGET_FILE=<Target S3 or GS file name to restore> \
[ -e MONGODB_URI=<Target MongoDB URI> \ ]
[ -e MONGODB_HOST=<Target MongoDB Host (default: "mongo")> \ ]
[ -e MONGODB_DBNAME=<Target DB name> \ ]
[ -e MONGODB_USERNAME=<DB login username> \ ]
[ -e MONGODB_PASSWORD=<DB login password> \ ]
[ -e MONGODB_AUTHDB=<Authentication DB name> \ ]
[ -e MONGODB_AUTHDB=<Authentication DB name> \ ]
[ -e MONGORESTORE_OPTS=<Options list of mongorestore> \ ]
[ -e AWSCLI_ENDPOINT_OPT=<S3 endpoint URL (ex. https://fra1.digitaloceanspaces.com)> \ ]
[ -v ~:/mab \ ]
Expand All @@ -114,22 +117,26 @@ Environment variables
#### Optional
| Variable | Description | Default |
| --------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| GCP_SERVICE_ACCOUNT_KEY_JSON_PATH | JSON file path to your GCP Service Account Key | - |
| GCP_ACCESS_KEY_ID | Your GCP Access Key | - |
| GCP_SECRET_ACCESS_KEY | Your GCP Secret | - |
| GCP_PROJECT_ID | Your GCP Project ID | - |
| BACKUPFILE_PREFIX | Prefix of Backup Filename | "backup" |
| MONGODB_HOST | Target MongoDB Host | "mongo" |
| MONGODB_DBNAME | Target DB name | - |
| MONGODB_USERNAME | DB login username | - |
| MONGODB_PASSWORD | DB login password | - |
| MONGODB_AUTHDB | Authentication DB name | - |
| CRONMODE | If set "true", this container is executed in cron mode. In cron mode, the script will be executed with the specified arguments and at the time specified by CRON_EXPRESSION. | "false" |
| CRON_EXPRESSION | Cron expression (ex. "CRON_EXPRESSION=0 4 * * *" if you want to run at 4:00 every day) | - |
| Variable | Description | Default |
| --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| GCP_SERVICE_ACCOUNT_KEY_JSON_PATH | JSON file path to your GCP Service Account Key | - |
| GCP_ACCESS_KEY_ID | Your GCP Access Key | - |
| GCP_SECRET_ACCESS_KEY | Your GCP Secret | - |
| GCP_PROJECT_ID | Your GCP Project ID | - |
| BACKUPFILE_PREFIX | Prefix of Backup Filename | "backup" |
| MONGODB_URI | Target MongoDB URI (ex. `mongodb://mongodb?replicaSet=rs0`). If set, the other `MONGODB_*` variables will be ignored. | - |
| MONGODB_HOST | Target MongoDB Host | "mongo" |
| MONGODB_DBNAME | Target DB name | - |
| MONGODB_USERNAME | DB login username | - |
| MONGODB_PASSWORD | DB login password | - |
| MONGODB_AUTHDB | Authentication DB name | - |
| CRONMODE | If set "true", this container is executed in cron mode. In cron mode, the script will be executed with the specified arguments and at the time specified by CRON_EXPRESSION. | "false" |
| CRON_EXPRESSION | Cron expression (ex. "CRON_EXPRESSION=0 4 * * *" if you want to run at 4:00 every day) | - |
| AWSCLI_ENDPOINT_OPT | Set a custom S3 endpoint if you use a S3 based service like DigitalOcean Spaces. (ex. AWSCLI_ENDPOINT_OPT="https://fra1.digitaloceanspaces.com") If not set the Amazon S3 standard endpoint will be used. | - |
| HEALTHCHECKS_URL | URL that gets called after a successful backup (eg. https://healthchecks.io) | - |
| AWSCLIOPT | Other options you want to pass to `aws` command | - |
| GCSCLIOPT | Other options you want to pass to `gsutil` command | - |
| HEALTHCHECKS_URL | URL that gets called after a successful backup (eg. https://healthchecks.io) | - |
### For `restore`
Expand All @@ -144,16 +151,19 @@ Environment variables
#### Optional
| Variable | Description | Default |
| --------------------------------- | ---------------------------------------------- | ------- |
| GCP_SERVICE_ACCOUNT_KEY_JSON_PATH | JSON file path to your GCP Service Account Key | - |
| GCP_ACCESS_KEY_ID | Your GCP Access Key | - |
| GCP_SECRET_ACCESS_KEY | Your GCP Secret | - |
| GCP_PROJECT_ID | Your GCP Project ID | - |
| MONGODB_HOST | Target MongoDB Host | "mongo" |
| MONGODB_DBNAME | Target DB name | - |
| MONGODB_USERNAME | DB login username | - |
| MONGODB_PASSWORD | DB login password | - |
| MONGODB_AUTHDB | Authentication DB name | - |
| MONGORESTORE_OPTS | Options list of mongorestore. (ex --drop) | - |
| AWSCLI_ENDPOINT_OPT | Set a custom S3 endpoint if you use a S3 based service like DigitalOcean Spaces. (ex. AWSCLI_ENDPOINT_OPT="https://fra1.digitaloceanspaces.com") If not set the Amazon S3 standard endpoint will be used. | - |
| Variable | Description | Default |
| --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| GCP_SERVICE_ACCOUNT_KEY_JSON_PATH | JSON file path to your GCP Service Account Key | - |
| GCP_ACCESS_KEY_ID | Your GCP Access Key | - |
| GCP_SECRET_ACCESS_KEY | Your GCP Secret | - |
| GCP_PROJECT_ID | Your GCP Project ID | - |
| MONGODB_URI | Target MongoDB URI (ex. `mongodb://mongodb?replicaSet=rs0`). If set, the other `MONGODB_*` variables will be ignored. | - |
| MONGODB_HOST | Target MongoDB Host | "mongo" |
| MONGODB_DBNAME | DB name to be restored from backup | - |
| MONGODB_USERNAME | DB login username | - |
| MONGODB_PASSWORD | DB login password | - |
| MONGODB_AUTHDB | Authentication DB name | - |
| MONGORESTORE_OPTS | Options list of mongorestore. (ex --drop) | - |
| AWSCLI_ENDPOINT_OPT | Set a custom S3 endpoint if you use a S3 based service like DigitalOcean Spaces. (ex. AWSCLI_ENDPOINT_OPT="https://fra1.digitaloceanspaces.com") If not set the Amazon S3 standard endpoint will be used. | - |
| AWSCLIOPT | Other options you want to pass to `aws` command | - |
| GCSCLIOPT | Other options you want to pass to `gsutil` command | - |
25 changes: 15 additions & 10 deletions bin/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
BACKUPFILE_PREFIX=${BACKUPFILE_PREFIX:-backup}
MONGODB_HOST=${MONGODB_HOST:-mongo}
CRONMODE=${CRONMODE:-false}
#MONGODB_URI=
#MONGODB_HOST=
#MONGODB_DBNAME=
#MONGODB_USERNAME=
Expand Down Expand Up @@ -44,19 +45,23 @@ if [ "x${TARGET_BUCKET_URL}" == "x" ]; then
exit 1
fi


# dump database
if [ "x${MONGODB_DBNAME}" != "x" ]; then
MONGODUMP_OPTS="${MONGODUMP_OPTS} -d ${MONGODB_DBNAME}"
fi
if [ "x${MONGODB_USERNAME}" != "x" ]; then
MONGODUMP_OPTS="${MONGODUMP_OPTS} -u ${MONGODB_USERNAME} -p ${MONGODB_PASSWORD}"
fi
if [ "x${MONGODB_AUTHDB}" != "x" ]; then
MONGODUMP_OPTS="${MONGODUMP_OPTS} --authenticationDatabase ${MONGODB_AUTHDB}"
if [ "x${MONGODB_URI}" != "x" ]; then
MONGODUMP_OPTS="--uri=${MONGODB_URI} ${MONGODUMP_OPTS}"
else
if [ "x${MONGODB_DBNAME}" != "x" ]; then
MONGODUMP_OPTS="${MONGODUMP_OPTS} -d ${MONGODB_DBNAME}"
fi
if [ "x${MONGODB_USERNAME}" != "x" ]; then
MONGODUMP_OPTS="${MONGODUMP_OPTS} -u ${MONGODB_USERNAME} -p ${MONGODB_PASSWORD}"
fi
if [ "x${MONGODB_AUTHDB}" != "x" ]; then
MONGODUMP_OPTS="${MONGODUMP_OPTS} --authenticationDatabase ${MONGODB_AUTHDB}"
fi
MONGODUMP_OPTS="-h ${MONGODB_HOST} ${MONGODUMP_OPTS}"
fi
echo "dump MongoDB..."
mongodump -h ${MONGODB_HOST} -o ${TARGET} ${MONGODUMP_OPTS}
mongodump -o ${TARGET} ${MONGODUMP_OPTS}

# run tar command
echo "backup ${TARGET}..."
Expand Down
22 changes: 13 additions & 9 deletions bin/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,21 @@ fi
echo "expands ${TARGET}..."
time ${TAR_CMD} ${TAR_OPTS} ${TARBALL_FULLPATH} -C ${DIRNAME} ${BASENAME}


# restore database
if [ "x${MONGODB_DBNAME}" != "x" ]; then
MONGORESTORE_OPTS="${MONGORESTORE_OPTS} -d ${MONGODB_DBNAME}"
TARGET=${TARGET}/${MONGODB_DBNAME}
fi
if [ "x${MONGODB_USERNAME}" != "x" ]; then
MONGORESTORE_OPTS="${MONGORESTORE_OPTS} -u ${MONGODB_USERNAME} -p ${MONGODB_PASSWORD}"
MONGORESTORE_OPTS="--nsInclude=${MONGODB_DBNAME}.* ${MONGORESTORE_OPTS}"
fi
if [ "x${MONGODB_AUTHDB}" != "x" ]; then
MONGORESTORE_OPTS="${MONGORESTORE_OPTS} --authenticationDatabase ${MONGODB_AUTHDB}"

if [ "x${MONGODB_URI}" != "x" ]; then
MONGORESTORE_OPTS="--uri=${MONGODB_URI} ${MONGORESTORE_OPTS}"
else
if [ "x${MONGODB_USERNAME}" != "x" ]; then
MONGORESTORE_OPTS="${MONGORESTORE_OPTS} -u ${MONGODB_USERNAME} -p ${MONGODB_PASSWORD}"
fi
if [ "x${MONGODB_AUTHDB}" != "x" ]; then
MONGORESTORE_OPTS="${MONGORESTORE_OPTS} --authenticationDatabase ${MONGODB_AUTHDB}"
fi
MONGORESTORE_OPTS="-h ${MONGODB_HOST} ${MONGORESTORE_OPTS}"
fi
echo "restore MongoDB..."
mongorestore -h ${MONGODB_HOST} -v ${TARGET} ${MONGORESTORE_OPTS}
mongorestore -v ${TARGET} ${MONGORESTORE_OPTS}
11 changes: 11 additions & 0 deletions skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# "skaffold run" auto-builds docker image, when modifying Dockerfile
apiVersion: skaffold/v1beta15
kind: Config
metadata:
name: mongodb-awesome-backup
build:
artifacts:
- image: weseek/mongodb-awesome-backup
local:
push: false
deploy: {}
Loading

0 comments on commit a6c8726

Please sign in to comment.