Skip to content

Instantly share code, notes, and snippets.

@ratulbasak
Created November 2, 2017 18:49
Show Gist options
  • Save ratulbasak/7763d6f12d9ca61cee6293bbb6f6339a to your computer and use it in GitHub Desktop.
Save ratulbasak/7763d6f12d9ca61cee6293bbb6f6339a to your computer and use it in GitHub Desktop.
mongodb migration bash script
#!/bin/bash
export hosts="localhost"
export new_db="ratul"
export new_user="ratul"
export new_pass="ratul"
export backup_path="/var/backups/mongobackups"
export old_db="ooop" # existing database name which'll be migrated
sudo mkdir $backup_path
sudo sed -i 's/\(authorization:\)\(.*\)/\1 disabled/' /etc/mongod.conf
sudo systemctl restart mongodb
### create backup of database
sudo mongodump -d $old_db --out $backup_path/
### restore the backed up database to new db
sudo mongorestore -d $new_db --drop $backup_path/$old_db
mongo $new_db --eval "db.createUser({ user: '$new_user', pwd: '$new_pass', roles: [ { role: 'root', db: '$new_db' } ] });"
sudo sed -i 's/\(authorization:\)\(.*\)/\1 enabled/' /etc/mongod.conf
sudo systemctl restart mongodb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment