1- # How to use the backup script
1+ # MongoDB scripts
2+
3+ This README explains how to directly access the production or staging database
4+ for backup or query purposes.
5+
6+ ## Backup script
27
38The backup script is intended to be used as a cron job or as a single command from your laptop.
49It uses SSH tunneling to a remote host and dumps the mongo database on your machine.
510Therefore, a public SSH key needs to be copied to the remote machine.
611
7- # Usage
12+ ### Usage
813
914All parameters must be supplied as environment variables:
1015
@@ -28,7 +33,7 @@ After exporting these environment variables to your bash, run:
2833```
2934
3035
31- # Import into your local mongo db (optional)
36+ ### Import into your local mongo db (optional)
3237
3338Run (but change the file name accordingly):
3439``` bash
@@ -40,3 +45,36 @@ If you previously encrypted your dump, run:
4045gpg --decrypt human-connection-dump_2018-11-21.archive.gpg | mongorestore --gzip --archive
4146```
4247
48+
49+ ## Query remote MongoDB
50+
51+ In contrast to the backup script, querying the database is expected to be done
52+ interactively and on demand by the user. Therefore our suggestion is to use a
53+ tool like [ MongoDB compass] ( https://www.mongodb.com/products/compass ) to query
54+ the mongo db through an SSH tunnel. This tool can export a collection as .csv
55+ file and you can further do custom processing with a csv tool like
56+ [ q] ( https://github.com/harelba/q ) .
57+
58+ ### Suggested workflow
59+
60+ Read on the mongodb compass documentation how to connect to the remote mongo
61+ database [ through SSH] ( https://docs.mongodb.com/compass/master/connect/ ) . You
62+ will need all the credentials and a public SSH key on the server as for the
63+ backup script above.
64+
65+ Once you have a connection, use the MongoDB Compass
66+ [ query bar] ( https://docs.mongodb.com/compass/master/query-bar/ ) to query for the
67+ desired data. You can
68+ [ export the result] ( https://docs.mongodb.com/compass/master/import-export/ ) as
69+ .json or .csv.
70+
71+ Once you have the .csv file on your machine, use standard SQL queries through
72+ the command line tool q to further process the data.
73+
74+ For example
75+ ``` sh
76+ q " SELECT email FROM ./invites.csv INTERSECT SELECT email FROM ./emails.csv" -H --delimiter=,
77+ ```
78+
79+ [ Q's website] ( http://harelba.github.io/q/usage.html ) explains the usage fairly
80+ well.
0 commit comments