Skip to content

Instantly share code, notes, and snippets.

@taufiqpsumarna
Last active August 29, 2022 09:24
Show Gist options
  • Save taufiqpsumarna/5939e0f0eb471edcd1eaeed6a0ab4aab to your computer and use it in GitHub Desktop.
Save taufiqpsumarna/5939e0f0eb471edcd1eaeed6a0ab4aab to your computer and use it in GitHub Desktop.
MongoDB Dump
#!/bin/bash
export HOME=/home/ubuntu/
HOST=localhost
# DB name
DBNAME=database
# S3 bucket name
BUCKET=backups
# Linux user account
USER=ubuntu
# Current time
TIME=`/bin/date +%d-%m-%Y-%T`
# Backup directory
DEST=/home/$USER/tmp
# Tar file of backup directory
TAR=$DEST/../$TIME.tar
# Create backup dir (-p to avoid warning if already exists)
/bin/mkdir -p $DEST
# Log
echo "Backing up $HOST/$DBNAME to s3://$BUCKET/ on $TIME";
# Dump from mongodb host into backup directory
/usr/bin/mongodump -h $HOST -d $DBNAME -o $DEST
# Create tar of backup directory
/bin/tar cvf $TAR -C $DEST .
# Upload tar to s3
/usr/bin/aws s3 cp $TAR s3://$BUCKET/ --storage-class STANDARD_IA
# Remove tar file locally
/bin/rm -f $TAR
# Remove backup directory
/bin/rm -rf $DEST
# All done
echo "Backup available at https://s3.amazonaws.com/$BUCKET/$TIME.tar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment