forked from experimental-berlin/muzhack-backup-rethinkdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
.*.sw* |
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,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 |
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,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',]) | ||
|
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 @@ | ||
30 18 * * * /env/bin/python3 /app/rethinkdb/backup.py --s3-bucket $S3_BUCKET --remove |
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,2 @@ | ||
rethinkdb | ||
boto3 |
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,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!') |