Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyaw/229f4db0ab239eca21220c6e10f102a7 to your computer and use it in GitHub Desktop.
Save jyaw/229f4db0ab239eca21220c6e10f102a7 to your computer and use it in GitHub Desktop.
Nightscout mLab Maintenance, Backup and Reduce

#Nightscout mLab Maintenance ###How to backup and reduce the size of your mLab sandbox

This gist documents the method I use to maintain the size of my MongoDB collections used with my Nightscout instance. I've noticed that I occasionally run out of space on my sandbox mLab account (the devicestatus collection is typically the culprit). This may be due to the use of multiple rigs at once... not sure exactly. If you have a sandbox mLab account you're capped at 496 MB, preallocated to contain the aggregate of storageSize and indexSize and some magical margin of MBs πŸ˜„. You can find those sizes on the mLab site under Stats. When my devicestatus collection gets too large, the smaller collections tend to keep functioning but the devicestatus quits updating. Currently, with heroku mLab, I don't get any notification that I'm running out of space, so I have to keep checking in on it.

These instructions were completed with a Windows 10 laptop. Much of it will be similar for other systems (including approach and even most or all of the MongoDB commands), however you may need to adapt for the particular system you're using (Mac, Linux, etc).

Anyway, here are some pieces of info you'll need for your specific use. You'll need...

  • <host>: your mlab host URL e.g. something.mlab.com
  • <port>: the port often referenced on your mlab instance page immediately following the host URL
  • <db_name>: the database name located on your mlab page. It's in bigger font following "Database:"
  • <db_user>: the databse user. This is at the top of your mlab page in the curly brackets following "user:"
  • <db_password>: the database password πŸ”πŸ”₯
  1. 🍻 Download the appropriate version of MongoDB

    MongoDB Downloads

    Note, this gist was originally put together while using a Windows 10 Laptop (Downloaded Nov 2016) MongoDB 3.2 Download for Windows 10

  2. 🍬 Setup an area on your machine specifically to archive your MongoDB database files

    I used <home_directory>\dev\mongodb\<db_name>_YYYYMMDD

  3. 🍩 Open terminal and navigate to the MongoDB application bin folder (steps below are windows specific)

    windows-R

    cmd, press enter

    cd "C:\Program Files\MongoDB\Server\3.2\bin"

  4. 🍜 Dump MongoDB database to the area you just setup

mongodump -h \<host>:<port> -d <db_name> -u <db_user> -p <db_password> -o <full_path_from_step_2>

  1. πŸ• Reduce the MongoDB database storageSize
  • To connect to MongoDB using mongo tool (execute in same terminal and location)

    mongo --host <host>:<port> -u <db_user> -p <db_password> <db_name>

  • (useful command freebie πŸ˜„) Useful command to check on database stats (what's shown on the mlab stats page)

    db.stats

  • To delete MongoDB documents in the devicestatus collection before a certain date in mongo shell (pick a date with the YYYY-MM-DD format as prescribed in the command. I usually pick a date a month or 2 previous):

    db.devicestatus.bulkWrite([{deleteMany:{"filter":{"created_at":{"$lte":"<YYYY-MM-DD>T01:01:01.000Z"}}}}])

    If other collections in your MongoDB instance need to be cleared, you can use the previous command but replace devicestatus with the appropriate collection name (e.g. entries or treatments)

Note that you can restore databases from various machines to a single MongoDB using the commands below.

For example, you may want to use these if you were using an Azure instance and want to move to Heroku.

  • To restore databases to MongoDB from MongoDB dump

    mongorestore -h <host>:<port> -d <db_name> -u <db_user> -p <db_password> <local_directory_where_MongoDB_dumps_reside>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment