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

Commit

Permalink
Merge pull request #181 from Human-Connection/backup_script
Browse files Browse the repository at this point in the history
Add backup script
  • Loading branch information
roschaefer authored Nov 26, 2018
2 parents 4437261 + 31dbd12 commit a64fff7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
npm-debug.log
scripts/
Dockerfile
4 changes: 4 additions & 0 deletions scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!remote-dump.sh
!README.md
!.gitignore
42 changes: 42 additions & 0 deletions scripts/README.md
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
```

32 changes: 32 additions & 0 deletions scripts/remote-dump.sh
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}

0 comments on commit a64fff7

Please sign in to comment.