#!/usr/bin/env python
import subprocess
import argparse
import os.path
import sys
from datetime import datetime


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


parser = argparse.ArgumentParser(description='Back up local RethinkDB instance')
parser.add_argument('--s3-bucket', default=None, help='Specify S3 bucket')
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))
    # TODO: Implement S3 uploading and deleting backups that are older than 100 days

@@ -105,16 +105,29 @@
 atetime%0A
+import boto3%0A
 %0A%0Adef _i
@@ -204,16 +204,47 @@
 ush()%0A%0A%0A
+s3_client = boto3.client('s3')%0A
 parser =
@@ -540,16 +540,33 @@
 me_str)%0A
+bucket_name = ''%0A
 if os.pa
@@ -887,16 +887,16 @@
 bucket:%0A
-
     _inf
@@ -965,16 +965,103 @@
 ucket))%0A
+    # Upload the file to S3%0A    s3_client.upload_file(filename, bucket_name, filename)%0A
     # TO

add s3 upload to backup.py