Skip to content

Commit

Permalink
Merge pull request #10 from znowfox/patch-3
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmoysrt authored Jun 4, 2024
2 parents f2e24ac + 7e1408f commit b727898
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 0 deletions.
Binary file added assets/logos/mongodb-backup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions build/mongodb-backup/6.0/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM mongo:6.0

ENV CRON_TIME="0 0 * * *" \
TZ=UTC \
CRON_TZ=UTC

# Install Python and Cron
RUN \
apt-get update && \
apt-get --assume-yes --no-install-recommends install \
awscli \
cron && \
rm -rf \
/var/lib/apt/lists/* \
/tmp/* \
/var/tmp/*

COPY run.sh /run.sh
RUN chmod +x /run.sh
CMD /run.sh
75 changes: 75 additions & 0 deletions build/mongodb-backup/6.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# mongodb-backup-s3

This is a simplified version of the mongodb-backup-s3 fork https://github.com/kevoj/mongodb-backup-s3.

Current image supports only `backup` of database.

---

## Example AWS IAM policy

This policy contains the required permissions for this container to operate. Replace the
`YOUR-BUCKET-HERE` placeholder with your bucket name.
```json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucket",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::YOUR-BUCKET-HERE",
"arn:aws:s3:::YOUR-BUCKET-HERE/*"
]
}
]
}
```

## Parameters

`AWS_ACCESS_KEY_ID` - your aws access key id (for your s3 bucket)

`AWS_SECRET_ACCESS_KEY`: - your aws secret access key (for your s3 bucket)

`BUCKET`: - your s3 bucket

`BUCKET_REGION`: - your s3 bucket' region (eg `us-east-2` for Ohio). Optional. Add if you get an error `A client error (PermanentRedirect)`

`ENDPOINT_URL`: - your custom S3 endpoint (eg `https://radosgw.example.com` )

`BUCKET_PREFIX`: - name of folder or path to put backups (eg `myapp/db_backups/`). defaults to root of bucket.

`MONGODB_HOST` - the host/ip of your mongodb database

`MONGODB_PORT` - the port number of your mongodb database

`MONGODB_USER` - the username of your mongodb database. I

`MONGODB_PASS` - the password of your mongodb database

`MONGODB_DB` - the database name to dump. If not specified, it will dump all the databases

`CRON_TIME` - the interval of cron job to run mongodump. `0 3 * * *` by default, which is every day at 03:00hrs.

`INIT_BACKUP` - if set, create a backup when the container launched



## Acknowledgements

Fork tree
```
https://github.com/halvves/mongodb-backup-s3
└─ https://github.com/deenoize/mongodb-backup-s3
└─ https://github.com/chobostar/mongodb-backup-s3
└─ https://github.com/zhonghuiwen/mongodb-backup-s3
└─ https://github.com/kevoj/mongodb-backup-s3
└─ this fork
```
46 changes: 46 additions & 0 deletions build/mongodb-backup/6.0/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

S3PATH="s3://$BUCKET/$BUCKET_PREFIX"

[[ ( -n "${ENDPOINT_URL}" ) ]] && ENDPOINT_STR=" --endpoint-url ${ENDPOINT_URL}"
[[ ( -n "${BUCKET_REGION}" ) ]] && REGION_STR=" --region ${BUCKET_REGION}"
[[ ( -n "${MONGODB_USER}" ) ]] && USER_STR=" --username ${MONGODB_USER}"
[[ ( -n "${MONGODB_PASS}" ) ]] && PASS_STR=" --password ${MONGODB_PASS}"
[[ ( -n "${MONGODB_DB}" ) ]] && DB_STR=" --db ${MONGODB_DB}"
[[ ( -n "${MONGODB_PORT}" ) ]] && PORT_STR=" --port ${MONGODB_PORT}"
[[ ( -n "${MONGODB_HOST}" ) ]] && HOST_STR=" --host ${MONGODB_HOST}"

# Export AWS Credentials into env file for cron job
printenv | sed 's/^\([a-zA-Z0-9_]*\)=\(.*\)$/export \1="\2"/g' | grep -E "^export AWS" > /root/project_env.sh
chmod +x /root/project_env.sh

echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
#!/bin/bash
TIMESTAMP=\`/bin/date +"%Y%m%dT%H%M%S"\`
BACKUP_NAME=\${TIMESTAMP}.dump.gz
S3BACKUP=${S3PATH}\${BACKUP_NAME}
aws configure set default.s3.signature_version s3v4
echo "=> Backup started"
if mongodump ${HOST_STR}${PORT_STR}${USER_STR}${PASS_STR}${DB_STR} --archive=\${BACKUP_NAME} --gzip && aws s3 cp \${BACKUP_NAME} \${S3BACKUP} ${REGION_STR} ${ENDPOINT_STR} && rm \${BACKUP_NAME} ;then
echo " > Backup succeeded"
else
echo " > Backup failed"
fi
echo "=> Done"
EOF
chmod +x /backup.sh
echo "=> Backup script created"

ln -s /backup.sh /usr/bin/backup

echo "=> Creating a backup on startup"
/backup.sh


echo "${CRON_TIME} . /root/project_env.sh; /backup.sh" > /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
cron
20 changes: 20 additions & 0 deletions build/mongodb-backup/7.0/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM mongo:7.0

ENV CRON_TIME="0 0 * * *" \
TZ=UTC \
CRON_TZ=UTC

# Install Python and Cron
RUN \
apt-get update && \
apt-get --assume-yes --no-install-recommends install \
awscli \
cron && \
rm -rf \
/var/lib/apt/lists/* \
/tmp/* \
/var/tmp/*

COPY run.sh /run.sh
RUN chmod +x /run.sh
CMD /run.sh
75 changes: 75 additions & 0 deletions build/mongodb-backup/7.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# mongodb-backup-s3

This is a simplified version of the mongodb-backup-s3 fork https://github.com/kevoj/mongodb-backup-s3.

Current image supports only `backup` of database.

---

## Example AWS IAM policy

This policy contains the required permissions for this container to operate. Replace the
`YOUR-BUCKET-HERE` placeholder with your bucket name.
```json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucket",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::YOUR-BUCKET-HERE",
"arn:aws:s3:::YOUR-BUCKET-HERE/*"
]
}
]
}
```

## Parameters

`AWS_ACCESS_KEY_ID` - your aws access key id (for your s3 bucket)

`AWS_SECRET_ACCESS_KEY`: - your aws secret access key (for your s3 bucket)

`BUCKET`: - your s3 bucket

`BUCKET_REGION`: - your s3 bucket' region (eg `us-east-2` for Ohio). Optional. Add if you get an error `A client error (PermanentRedirect)`

`ENDPOINT_URL`: - your custom S3 endpoint (eg `https://radosgw.example.com` )

`BUCKET_PREFIX`: - name of folder or path to put backups (eg `myapp/db_backups/`). defaults to root of bucket.

`MONGODB_HOST` - the host/ip of your mongodb database

`MONGODB_PORT` - the port number of your mongodb database

`MONGODB_USER` - the username of your mongodb database. I

`MONGODB_PASS` - the password of your mongodb database

`MONGODB_DB` - the database name to dump. If not specified, it will dump all the databases

`CRON_TIME` - the interval of cron job to run mongodump. `0 3 * * *` by default, which is every day at 03:00hrs.

`INIT_BACKUP` - if set, create a backup when the container launched



## Acknowledgements

Fork tree
```
https://github.com/halvves/mongodb-backup-s3
└─ https://github.com/deenoize/mongodb-backup-s3
└─ https://github.com/chobostar/mongodb-backup-s3
└─ https://github.com/zhonghuiwen/mongodb-backup-s3
└─ https://github.com/kevoj/mongodb-backup-s3
└─ this fork
```
46 changes: 46 additions & 0 deletions build/mongodb-backup/7.0/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

S3PATH="s3://$BUCKET/$BUCKET_PREFIX"

[[ ( -n "${ENDPOINT_URL}" ) ]] && ENDPOINT_STR=" --endpoint-url ${ENDPOINT_URL}"
[[ ( -n "${BUCKET_REGION}" ) ]] && REGION_STR=" --region ${BUCKET_REGION}"
[[ ( -n "${MONGODB_USER}" ) ]] && USER_STR=" --username ${MONGODB_USER}"
[[ ( -n "${MONGODB_PASS}" ) ]] && PASS_STR=" --password ${MONGODB_PASS}"
[[ ( -n "${MONGODB_DB}" ) ]] && DB_STR=" --db ${MONGODB_DB}"
[[ ( -n "${MONGODB_PORT}" ) ]] && PORT_STR=" --port ${MONGODB_PORT}"
[[ ( -n "${MONGODB_HOST}" ) ]] && HOST_STR=" --host ${MONGODB_HOST}"

# Export AWS Credentials into env file for cron job
printenv | sed 's/^\([a-zA-Z0-9_]*\)=\(.*\)$/export \1="\2"/g' | grep -E "^export AWS" > /root/project_env.sh
chmod +x /root/project_env.sh

echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
#!/bin/bash
TIMESTAMP=\`/bin/date +"%Y%m%dT%H%M%S"\`
BACKUP_NAME=\${TIMESTAMP}.dump.gz
S3BACKUP=${S3PATH}\${BACKUP_NAME}
aws configure set default.s3.signature_version s3v4
echo "=> Backup started"
if mongodump ${HOST_STR}${PORT_STR}${USER_STR}${PASS_STR}${DB_STR} --archive=\${BACKUP_NAME} --gzip && aws s3 cp \${BACKUP_NAME} \${S3BACKUP} ${REGION_STR} ${ENDPOINT_STR} && rm \${BACKUP_NAME} ;then
echo " > Backup succeeded"
else
echo " > Backup failed"
fi
echo "=> Done"
EOF
chmod +x /backup.sh
echo "=> Backup script created"

ln -s /backup.sh /usr/bin/backup

echo "=> Creating a backup on startup"
/backup.sh


echo "${CRON_TIME} . /root/project_env.sh; /backup.sh" > /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
cron
Loading

0 comments on commit b727898

Please sign in to comment.