Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit a64fff7

Browse files
authored
Merge pull request #181 from Human-Connection/backup_script
Add backup script
2 parents 4437261 + 31dbd12 commit a64fff7

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
npm-debug.log
3+
scripts/
34
Dockerfile

scripts/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*
2+
!remote-dump.sh
3+
!README.md
4+
!.gitignore

scripts/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# How to use the backup script
2+
3+
The backup script is intended to be used as a cron job or as a single command from your laptop.
4+
It uses SSH tunneling to a remote host and dumps the mongo database on your machine.
5+
Therefore, a public SSH key needs to be copied to the remote machine.
6+
7+
# Usage
8+
9+
All parameters must be supplied as environment variables:
10+
11+
| Name | required |
12+
|-----------------------|-----------|
13+
| SSH\_USERNAME | yes |
14+
| SSH\_HOST | yes |
15+
| MONGODB\_USERNAME | yes |
16+
| MONGODB\_PASSWORD | yes |
17+
| MONGODB\_DATABASE | yes |
18+
| OUTPUT | |
19+
| GPG\_PASSWORD | |
20+
21+
If you set `GPG_PASSWORD`, the resulting archive will be encrypted (symmetrically, with the given passphrase).
22+
This is recommended if you dump the database on your personal laptop because of data security.
23+
24+
After exporting these environment variables to your bash, run:
25+
26+
```bash
27+
./remote-dump.sh
28+
```
29+
30+
31+
# Import into your local mongo db (optional)
32+
33+
Run (but change the file name accordingly):
34+
```bash
35+
mongorestore --gzip --archive=human-connection-dump_2018-11-21.archive
36+
```
37+
38+
If you previously encrypted your dump, run:
39+
```bash
40+
gpg --decrypt human-connection-dump_2018-11-21.archive.gpg | mongorestore --gzip --archive
41+
```
42+

scripts/remote-dump.sh

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
for var in "SSH_USERNAME" "SSH_HOST" "MONGODB_USERNAME" "MONGODB_PASSWORD" "MONGODB_DATABASE"
4+
do
5+
if [[ -z "${!var}" ]]; then
6+
echo "${var} is undefined"
7+
exit -1
8+
fi
9+
done
10+
11+
OUTPUT_FILE_NAME=${OUTPUT:-human-connection-dump}_$(date -I).archive
12+
13+
echo "SSH_USERNAME ${SSH_USERNAME}"
14+
echo "SSH_HOST ${SSH_HOST}"
15+
echo "MONGODB_USERNAME ${MONGODB_USERNAME}"
16+
echo "MONGODB_PASSWORD ${MONGODB_PASSWORD}"
17+
echo "MONGODB_DATABASE ${MONGODB_DATABASE}"
18+
echo "OUTPUT_FILE_NAME ${OUTPUT_FILE_NAME}"
19+
echo "GPG_PASSWORD ${GPG_PASSWORD:-<none>}"
20+
echo "-------------------------------------------------"
21+
22+
ssh -M -S my-ctrl-socket -fnNT -L 27018:localhost:27017 -l ${SSH_USERNAME} ${SSH_HOST}
23+
24+
if [[ -z "${!GPG_PASSWORD}" ]]; then
25+
mongodump --host localhost -d ${MONGODB_DATABASE} --port 27018 --username ${MONGODB_USERNAME} --password ${MONGODB_PASSWORD} --authenticationDatabase admin --gzip --archive | gpg -c --batch --passphrase ${GPG_PASSWORD} --output ${OUTPUT_FILE_NAME}.gpg
26+
else
27+
mongodump --host localhost -d ${MONGODB_DATABASE} --port 27018 --username ${MONGODB_USERNAME} --password ${MONGODB_PASSWORD} --authenticationDatabase admin --gzip --archive=${OUTPUT_FILE_NAME}
28+
fi
29+
30+
31+
ssh -S my-ctrl-socket -O check -l ${SSH_USERNAME} ${SSH_HOST}
32+
ssh -S my-ctrl-socket -O exit -l ${SSH_USERNAME} ${SSH_HOST}

0 commit comments

Comments
 (0)