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
2
7
3
8
The backup script is intended to be used as a cron job or as a single command from your laptop.
4
9
It uses SSH tunneling to a remote host and dumps the mongo database on your machine.
5
10
Therefore, a public SSH key needs to be copied to the remote machine.
6
11
7
- # Usage
12
+ ### Usage
8
13
9
14
All parameters must be supplied as environment variables:
10
15
@@ -28,7 +33,7 @@ After exporting these environment variables to your bash, run:
28
33
```
29
34
30
35
31
- # Import into your local mongo db (optional)
36
+ ### Import into your local mongo db (optional)
32
37
33
38
Run (but change the file name accordingly):
34
39
``` bash
@@ -40,3 +45,36 @@ If you previously encrypted your dump, run:
40
45
gpg --decrypt human-connection-dump_2018-11-21.archive.gpg | mongorestore --gzip --archive
41
46
```
42
47
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