Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup mechanism based on dockup image (for Amazon S3) #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compose/dockup/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ubuntu:14.04
MAINTAINER Krzysztof Szumny <[email protected]>

RUN apt-get update && apt-get install -y python-pip && pip install awscli

ADD backup.sh /backup.sh
ADD restore.sh /restore.sh
ADD run.sh /run.sh
RUN chmod 755 /*.sh

CMD ["/run.sh"]
80 changes: 80 additions & 0 deletions compose/dockup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

# Dockup

Docker image to backup your Docker container volumes

Why the name? Docker + Backup = Dockup

# Usage

You have a container running with one or more volumes:

```
$ docker run -d --name mysql tutum/mysql
```

From executing a `$ docker inspect mysql` we see that this container has two volumes:

```
"Volumes": {
"/etc/mysql": {},
"/var/lib/mysql": {}
}
```

## Backup
Launch `dockup` container with the following flags:

```
$ docker run --rm \
--env-file env.txt \
--volumes-from mysql \
--name dockup tutum/dockup:latest
```

The contents of `env.txt` being:

```
DOCKUP_AWS_ACCESS_KEY_ID=<key_here>
DOCKUP_AWS_SECRET_ACCESS_KEY=<secret_here>
DOCKUP_AWS_DEFAULT_REGION=us-east-1
DOCKUP_BACKUP_NAME=mysql
DOCKUP_PATHS_TO_BACKUP=/etc/mysql /var/lib/mysql
DOCKUP_S3_BUCKET_NAME=docker-backups.example.com
DOCKUP_RESTORE=false
```

`dockup` will use your AWS credentials to create a new bucket with name as per the environment variable `DOCKUP_S3_BUCKET_NAME`, or if not defined, using the default name `docker-backups.example.com`. The paths in `DOCKUP_PATHS_TO_BACKUP` will be tarballed, gzipped, time-stamped and uploaded to the S3 bucket.


## Restore
To restore your data simply set the `DOCKUP_RESTORE` environment variable to `true` - this will restore the latest backup from S3 to your volume.


## A note on Buckets

> [Bucket naming guidelines](http://docs.aws.amazon.com/cli/latest/userguide/using-s3-commands.html):
> "Bucket names must be unique and should be DNS compliant. Bucket names can contain lowercase letters, numbers, hyphens and periods. Bucket names can only start and end with a letter or number, and cannot contain a period next to a hyphen or another period."

These rules are enforced in some regions.


[AWS S3 Regions](http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region)

| Region name | Region |
| ------------------------- | -------------- |
| US Standard | us-east-1 |
| US West (Oregon) | us-west-2 |
| US West (N. California) | us-west-1 |
| EU (Ireland) | eu-west-1 |
| EU (Frankfurt) | eu-central-1 |
| Asia Pacific (Singapore) | ap-southeast-1 |
| Asia Pacific (Sydney) | ap-southeast-2 |
| Asia Pacific (Tokyo) | ap-northeast-1 |
| South America (Sao Paulo) | sa-east-1 |


To perform a restore launch the container with the `DOCKUP_RESTORE` variable set to `true`


![](http://s.tutum.co.s3.amazonaws.com/support/images/dockup-readme.png)
22 changes: 22 additions & 0 deletions compose/dockup/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
source /envs
export PATH=$PATH:/usr/bin:/usr/local/bin:/bin
# Get timestamp
: ${backup_siffix:=.$(date +"%Y-%m-%d-%H-%M-%S")}
readonly tarball=$DOCKUP_BACKUP_NAME$backup_siffix.tar.gz

# Create a gzip compressed tarball with the volume(s)
tar czf $tarball $DOCKUP_BACKUP_TAR_OPTION $DOCKUP_PATHS_TO_BACKUP

# Create bucket, if it doesn't already exist
bucket_exist=$(aws s3 ls | grep $DOCKUP_S3_BUCKET_NAME | wc -l)
if [ $bucket_exist -eq 0 ];
then
aws s3 mb s3://$DOCKUP_S3_BUCKET_NAME
fi

# Upload the backup to S3 with timestamp
aws s3 --region $DOCKUP_AWS_DEFAULT_REGION cp $tarball s3://$DOCKUP_S3_BUCKET_NAME/$tarball

# Clean up
rm $tarball
11 changes: 11 additions & 0 deletions compose/dockup/restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
source /envs

# Find last backup file
: ${last_backup:=$(aws s3 ls s3://$DOCKUP_S3_BUCKET_NAME | awk -F " " '{print $4}' | grep ^$DOCKUP_BACKUP_NAME | sort -r | head -n1)}

# Download backup from S3
aws s3 cp s3://$DOCKUP_S3_BUCKET_NAME/$last_backup $last_backup

# Extract backup
tar xzf $last_backup $DOCKUP_RESTORE_TAR_OPTION
23 changes: 23 additions & 0 deletions compose/dockup/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

export AWS_ACCESS_KEY_ID=${DOCKUP_AWS_ACCESS_KEY_ID}
export AWS_SECRET_ACCESS_KEY=${DOCKUP_AWS_SECRET_ACCESS_KEY}
export AWS_DEFAULT_REGION=${DOCKUP_AWS_DEFAULT_REGION}

export > /envs

if [[ "$DOCKUP_RESTORE" == "true" ]]; then
./restore.sh
else
./backup.sh
fi

if [ -n "$DOCKUP_CRON_TIME" ]; then
echo "${DOCKUP_CRON_TIME} /backup.sh >> /dockup.log 2>&1" > /crontab.conf
touch /dockup.log
crontab /crontab.conf
echo "=> Running dockup backups as a cronjob for ${DOCKUP_CRON_TIME}"
rsyslogd
cron
tail -f /var/log/syslog /dockup.log
fi
11 changes: 11 additions & 0 deletions compose/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ volumes:
app_data: {}

services:
backup:
build: ./dockup
volumes:
- postgres_data:/postgres_data
- postgres_backup:/postgres_backup
- app_data:/app_data
env_file: ../.env
environment:
- DOCKUP_PATHS_TO_BACKUP=/postgres_data /postgres_backup /app_data
- DOCKUP_S3_BUCKET_NAME=backup.${HOST_ADDRESS}

postgres:
build: ./postgres
volumes:
Expand Down
7 changes: 7 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ DJANGO_SENTRY_DSN=
DJANGO_OPBEAT_ORGANIZATION_ID
DJANGO_OPBEAT_APP_ID
DJANGO_OPBEAT_SECRET_TOKEN

DOCKUP_AWS_ACCESS_KEY_ID=
DOCKUP_AWS_SECRET_ACCESS_KEY=
DOCKUP_AWS_DEFAULT_REGION=
DOCKUP_BACKUP_NAME=
DOCKUP_RESTORE=false
DOCKUP_CRON_TIME=0 0 * * *