#!/usr/bin/env python
#-*- encoding: utf8 -*-
import boto
import boto.s3.connection
from boto.s3.key import Key
from datetime import datetime
import os
# Reference: https://blog.csdn.net/github_25679381/article/details/52943665
conn = boto.connect_s3(
            aws_access_key_id = 'xxxxx',
            aws_secret_access_key = 'xxxxx',
            host = 's3.xxxxx', is_secure = False,
            calling_format = boto.s3.connection.OrdinaryCallingFormat())
mytime = datetime.now().strftime('%Y%m%d_%H')
bucket = conn.get_bucket('xxxxx')    
k = Key(bucket)
filename = 'xxxxx'+ mytime +'.sql'
k.key = filename
os.system("mysqldump -h xxxxx -u xxxxx -pxxxxx --single-transaction xxxxx > "+filename)
print 'backup mysql dumpfile finished!'
# Download
#filename2 = 'xxxxx'
#k.get_contents_to_filename(filename2)
# Upload
k.set_contents_from_filename(filename)
print 'upload to S3 finished!'