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

Commit a401751

Browse files
committed
Add first version of backup script
1 parent 4437261 commit a401751

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

scripts/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
20+
After exporting these environment variables to your bash, run:
21+
```
22+
./remote-dump.sh
23+
```
24+

scripts/remote-dump.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 "-------------------------------------------------"
20+
21+
ssh -M -S my-ctrl-socket -fnNT -L 27018:localhost:27017 -l ${SSH_USERNAME} ${SSH_HOST}
22+
mongodump --host localhost -d ${MONGODB_DATABASE} --port 27018 --username ${MONGODB_USERNAME} --password ${MONGODB_PASSWORD} --authenticationDatabase admin --gzip --archive=${OUTPUT_FILE_NAME}
23+
ssh -S my-ctrl-socket -O check -l ${SSH_USERNAME} ${SSH_HOST}
24+
ssh -S my-ctrl-socket -O exit -l ${SSH_USERNAME} ${SSH_HOST}

0 commit comments

Comments
 (0)