Skip to content

Instantly share code, notes, and snippets.

@SarasArya
Created February 11, 2017 12:28
Show Gist options
  • Save SarasArya/38110d6ebc8944aa997336e0e5d374ba to your computer and use it in GitHub Desktop.
Save SarasArya/38110d6ebc8944aa997336e0e5d374ba to your computer and use it in GitHub Desktop.
This script is used to automate the backup process of your mongodb instance. It's a very easy bash script and variables are provided for customization to script. If you don't know how to use mongodump, I suggest you start [here](https://docs.mongodb.com/manual/reference/program/mongodump/)
MONGO_DATABASE="your_db_name"
APP_NAME="your_app_name" #this is optional.Use this if you have more than 1 scripts for more than 1 app
MONGO_HOST="<db_name>/server0.example.com:27011,server1.example.com:27011,server2.example.com:27011" #for replica set config
#MONGO_HOST="127.0.0.1" in case running a standalone server
#MONGO_PORT=27001 in case running a standalone server on a port other than 27017
TIMESTAMP=`date +"%d-%m-%y"`
MONGODUMP_PATH="/usr/bin/mongodump" #got it from whereis mongodump
#BACKUP_DIR="/home/oroborus/work/server-logs" #for my local machine to test it out
BACKUP_DIR="/home/ubuntu/server-logs" #ec2 instance where to put it
BACKUP_NAME="$APP_NAME/$TIMESTAMP" #folder structure optional again
$MONGODUMP_PATH --host $MONGO_HOST --db $MONGO_DATABASE --out $BACKUP_DIR/$BACKUP_NAME
#/usr/bin/mongodump --host 127.0.0.1 --port 27017 /home/oroborus/work/server-logs
#echo this statement to check what is executing.
#use crontab.guru to check what time you want to run the script
#I choose to run every hour. so my crontab line looks like
#0 * * * * sudo /bin/bash /home/ubuntu/server-logs/mongo-backup-automate.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment