This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from Human-Connection/backup_script
Add backup script
- Loading branch information
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules/ | ||
npm-debug.log | ||
scripts/ | ||
Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
* | ||
!remote-dump.sh | ||
!README.md | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# How to use the backup script | ||
|
||
The backup script is intended to be used as a cron job or as a single command from your laptop. | ||
It uses SSH tunneling to a remote host and dumps the mongo database on your machine. | ||
Therefore, a public SSH key needs to be copied to the remote machine. | ||
|
||
# Usage | ||
|
||
All parameters must be supplied as environment variables: | ||
|
||
| Name | required | | ||
|-----------------------|-----------| | ||
| SSH\_USERNAME | yes | | ||
| SSH\_HOST | yes | | ||
| MONGODB\_USERNAME | yes | | ||
| MONGODB\_PASSWORD | yes | | ||
| MONGODB\_DATABASE | yes | | ||
| OUTPUT | | | ||
| GPG\_PASSWORD | | | ||
|
||
If you set `GPG_PASSWORD`, the resulting archive will be encrypted (symmetrically, with the given passphrase). | ||
This is recommended if you dump the database on your personal laptop because of data security. | ||
|
||
After exporting these environment variables to your bash, run: | ||
|
||
```bash | ||
./remote-dump.sh | ||
``` | ||
|
||
|
||
# Import into your local mongo db (optional) | ||
|
||
Run (but change the file name accordingly): | ||
```bash | ||
mongorestore --gzip --archive=human-connection-dump_2018-11-21.archive | ||
``` | ||
|
||
If you previously encrypted your dump, run: | ||
```bash | ||
gpg --decrypt human-connection-dump_2018-11-21.archive.gpg | mongorestore --gzip --archive | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/bin/bash | ||
|
||
for var in "SSH_USERNAME" "SSH_HOST" "MONGODB_USERNAME" "MONGODB_PASSWORD" "MONGODB_DATABASE" | ||
do | ||
if [[ -z "${!var}" ]]; then | ||
echo "${var} is undefined" | ||
exit -1 | ||
fi | ||
done | ||
|
||
OUTPUT_FILE_NAME=${OUTPUT:-human-connection-dump}_$(date -I).archive | ||
|
||
echo "SSH_USERNAME ${SSH_USERNAME}" | ||
echo "SSH_HOST ${SSH_HOST}" | ||
echo "MONGODB_USERNAME ${MONGODB_USERNAME}" | ||
echo "MONGODB_PASSWORD ${MONGODB_PASSWORD}" | ||
echo "MONGODB_DATABASE ${MONGODB_DATABASE}" | ||
echo "OUTPUT_FILE_NAME ${OUTPUT_FILE_NAME}" | ||
echo "GPG_PASSWORD ${GPG_PASSWORD:-<none>}" | ||
echo "-------------------------------------------------" | ||
|
||
ssh -M -S my-ctrl-socket -fnNT -L 27018:localhost:27017 -l ${SSH_USERNAME} ${SSH_HOST} | ||
|
||
if [[ -z "${!GPG_PASSWORD}" ]]; then | ||
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 | ||
else | ||
mongodump --host localhost -d ${MONGODB_DATABASE} --port 27018 --username ${MONGODB_USERNAME} --password ${MONGODB_PASSWORD} --authenticationDatabase admin --gzip --archive=${OUTPUT_FILE_NAME} | ||
fi | ||
|
||
|
||
ssh -S my-ctrl-socket -O check -l ${SSH_USERNAME} ${SSH_HOST} | ||
ssh -S my-ctrl-socket -O exit -l ${SSH_USERNAME} ${SSH_HOST} |