Python实现向s3共享存储上传和下载文件

本文介绍了一种使用Python脚本自动备份MySQL数据库的方法,并将其上传到Amazon S3存储桶。通过调用boto库,实现与S3的连接,利用mysqldump工具进行数据库备份。
#!/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.youkuaiyun.com/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!'

 

转载于:https://www.cnblogs.com/liang545621/p/10298617.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值