What command do I use and run?
Use mongodump
:
$ ./mongodump --host prod.example.com
connected to: prod.example.com
all dbs
DATABASE: log to dump/log
log.errors to dump/log/errors.bson
713 objects
log.analytics to dump/log/analytics.bson
234810 objects
DATABASE: blog to dump/blog
blog.posts to dump/log/blog.posts.bson
59 objects
DATABASE: admin to dump/admin
Source: http://www.mongodb.org/display/DOCS/Import+Export+Tools
Following command connect to the remote server to dump a database:
<> optional params use them if you need them
output to a created folder with a name
mongodump --host --port --username --db --ssl --password --out _date+"%Y-%m-%d"
Mongo dump and restore with uri to local
mongodump --uri "mongodb://USERNAME:PASSWORD@IP_OR_URL:PORT/DB_NAME" --collection COLLECTION_NAME -o LOCAL_URL
Omitting --collection COLLECTION_NAME will dump entire DB.
use "path" for windows. Else it gives the error as: positional arguments not allowed
mongodump -h hostname -u dbusername -p dbpassword --db dbname --port portnumber --out /path/folder
mongodump -h hostname -u dbusername -p dbpassword --db dbname --port portnumber --out /path/folder.gz
Backup/Restore Mongodb with timing.
Backup:
sudo mongodump --db db_name --out /path_of_your_backup/`date +"%m-%d-%y"`
--db
argument for databse name
--out
argument for path of output
Restore:
sudo mongorestore --db db_name --drop /path_of_your_backup/01-01-19/db_name/
--drop
argument for drop databse before restore
Timing:
You can use crontab for timing backup:
sudo crontab -e
It opens with editor(e.g. nano)
3 3 * * * mongodump --out /path_of_your_backup/`date +"%m-%d-%y"`
backup every day at 03:03 AM
Depending on your MongoDB database sizes you may soon run out of disk space with too many backups. That's why it's also recommended to clean the old backups regularly or to compress them. For example, to delete all the backups older than 7 days you can use the following bash command:
3 1 * * * find /path_of_your_backup/ -mtime +7 -exec rm -rf {} \;
delete all the backups older than 7 days
Good Luck.
ref: https://www.digitalocean.com/community/tutorials/how-to-back-up-restore-and-migrate-a-mongodb-database-on-ubuntu-14-04