Created
November 9, 2017 10:59
-
-
Save maracuja/4152bf199235152e9073ecda30062334 to your computer and use it in GitHub Desktop.
Backup database to S3
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
import datetime | |
from fabric.api import * | |
from fabric.colors import cyan, green | |
import boto | |
import boto.s3 | |
from boto.s3.key import Key | |
AWS_REGION = 'eu-west-2' | |
AWS_ACCESS_KEY_ID = '' | |
AWS_SECRET_ACCESS_KEY = '' | |
BUCKET_NAME = '' | |
DB_FILENAME = 'db.sqlite3' | |
def backup_db(): | |
print (cyan('>>>> Starting backup.')) | |
print (cyan('>>>> Connecting to AWS.')) | |
conn = boto.s3.connect_to_region( | |
AWS_REGION, | |
aws_access_key_id=AWS_ACCESS_KEY_ID, | |
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, | |
is_secure=True) | |
bucket = conn.get_bucket(BUCKET_NAME) | |
print (cyan('>>>> Uploading {} to Amazon S3 bucket {}'.format(DB_FILENAME, BUCKET_NAME))) | |
k = Key(bucket) | |
k.key = '{}/{}'.format( | |
datetime.datetime.now().strftime('%Y/%m/%d'), DB_FILENAME) | |
k.set_contents_from_filename('./{}'.format(DB_FILENAME)) | |
print (green('>>>> Done.')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment