Skip to content

Commit

Permalink
Start repo
Browse files Browse the repository at this point in the history
  • Loading branch information
aknuds1 committed Jan 3, 2016
1 parent c56bda5 commit 7b03ae6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.*.sw*
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM debian
MAINTAINER "MuzHack" <[email protected]>

RUN apt-get update && apt-get install -y cron python3 rethinkdb \
&& pip3 install virtualenv
RUN apt-get clean -y && apt-get autoclean -y && apt-get autoremove -y && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /app
COPY ./crontab /etc/cron.d/
COPY ./requirements.txt /app
COPY ./rethinkdb/backup.py /app/rethinkdb/

RUN virtualenv /env && /env/bin/pip3 install -r requirements.txt
RUN rm -rf requirements.txt

CMD /env/bin/python3 cron.py
15 changes: 15 additions & 0 deletions cron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import subprocess
import os


crontab_filename = '/etc/cron.d/cron-python'
with open(crontab_filename, 'rb') as f:
crontab = f.readlines()
for k in [k for k in os.environ if k.startswith('CRONVAR_')]:
cron_vars.append((k, os.environ[k],))

crontab = cron_vars + ['',] + crontab
with open(crontab_filename, 'wb') as f:
f.write('\n'.join(crontab))
subprocess.check_call(['crond', '-L', '15',])

1 change: 1 addition & 0 deletions crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
30 18 * * * /env/bin/python3 /app/rethinkdb/backup.py --s3-bucket $S3_BUCKET --remove
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rethinkdb
boto3
56 changes: 56 additions & 0 deletions rethinkdb/backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
import subprocess
import argparse
import os.path
import sys
from datetime import datetime
import boto3


def _info(msg):
sys.stdout.write('* {}\n'.format(msg))
sys.stdout.flush()


def _error(msg):
sys.stderr.write('* {}\n'.format(msg))
sys.exit(1)


def _get_environment_value(key):
value = (os.environ.get(key) or '').strip()
if not value:
_error('You must define environment value \'{}\''.format(key))
return value


parser = argparse.ArgumentParser(description='Back up local RethinkDB instance')
parser.add_argument('--s3-bucket', default=None, help='Specify S3 bucket')
parser.add_argument('--remove', action='store_true', default=False,
help='Remove backup archive when done?')
args = parser.parse_args()

date_time_str = datetime.utcnow().strftime('%Y-%m-%dT%H:%M')
filename = 'rethinkdb-dump-{}.tar.gz'.format(date_time_str)
if os.path.exists(filename):
os.remove(filename)
command = ['rethinkdb', 'dump', '-f', filename]
auth_key = os.environ.get('RETHINKDB_AUTH_KEY')
if auth_key:
command.extend(['-a', auth_key,])
_info('Backing up database to {}...'.format(filename))
subprocess.check_call(command, stdout=subprocess.PIPE)

if args.s3_bucket:
_info('Uploading \'{}\' to S3 bucket \'{}\'...'.format(filename, args.s3_bucket))
access_key_id = _get_environment_value('AWS_ACCESS_KEY_ID')
secret = _get_environment_value('AWS_SECRET_ACCESS_KEY')
s3_client = boto3.client('s3', region_name='eu-central-1', aws_access_key_id=access_key_id,
aws_secret_access_key=secret)
s3_client.upload_file(filename, args.s3_bucket, filename)
# TODO: Implement deleting backups that are older than 100 days

if args.remove:
os.remove(filename)

_info('Success!')

0 comments on commit 7b03ae6

Please sign in to comment.