How do I create a MongoDB dump of my database?

前端 未结 19 2386
既然无缘
既然无缘 2020-12-22 15:06

What command do I use and run?

相关标签:
19条回答
  • 2020-12-22 15:48

    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

    0 讨论(0)
  • 2020-12-22 15:48

    Following command connect to the remote server to dump a database:

    <> optional params use them if you need them

    • host - host name port
    • listening port username
    • username of db db
    • db name ssl
    • secure connection out
    • output to a created folder with a name

      mongodump --host --port --username --db --ssl --password --out _date+"%Y-%m-%d"

    0 讨论(0)
  • 2020-12-22 15:49

    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.

    0 讨论(0)
  • 2020-12-22 15:54

    use "path" for windows. Else it gives the error as: positional arguments not allowed

    0 讨论(0)
  • 2020-12-22 15:54

    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

    0 讨论(0)
  • 2020-12-22 15:55

    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

    0 讨论(0)
提交回复
热议问题