Skip to content

Instantly share code, notes, and snippets.

@omartrigui
Last active January 9, 2023 13:29
Show Gist options
  • Save omartrigui/c956c28327137c181147a9bf5be970f2 to your computer and use it in GitHub Desktop.
Save omartrigui/c956c28327137c181147a9bf5be970f2 to your computer and use it in GitHub Desktop.
Mongodump shell script - Backup/Restore
#!/bin/bash
MONGO_DATABASE="db_name"
INSTANCE_NAME="instance_name"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="./path/$INSTANCE_NAME"
BACKUP_NAME="$INSTANCE_NAME-$TIMESTAMP"
$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME
# mongodump --db db --username username --password password -o output
# mongorestore --db database -u username -p password dump/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment