Skip to content

Instantly share code, notes, and snippets.

@stigok
Created December 13, 2019 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stigok/0f5f4139fafb674e6a9e805500cf5bdd to your computer and use it in GitHub Desktop.
Save stigok/0f5f4139fafb674e6a9e805500cf5bdd to your computer and use it in GitHub Desktop.
MongoDB database backup to S3
#!/bin/bash
# Backs up a complete mongodb database instance running on localhost and
# uploads a gzipped dump to an S3 bucket (in this case Scaleway Object Storage)
#
# Configure s3cmd with `s3cmd --configure`
set -eu
MONGODB_USER=${MONGODB_USER}
MONGODB_PASSWORD=${MONGODB_PASSWORD}
S3_BUCKET=mongodumps
dir=$(mktemp -d)
filename="mongodump-$(date '+%F-%T' | tr '/:' -).gz"
mongodump --gzip --archive=${dir}/${filename} -u ${MONGODB_USER} -p ${MONGODB_PASSWORD}
s3cmd put ${dir}/${filename} s3://${S3_BUCKET}/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment