#!/usr/bin/env python3
#-*-coding:utf-8-*-
# date: 2018/4/13
__author__ = 'xiaobing'
import time
import os
from qiniu import Auth, put_file, etag, BucketManager
# 上传文件
def uploading(q, bucket_name, key, local_file):
# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
ret, info = put_file(token, key, local_file)
assert ret['key'] == key
assert ret['hash'] == etag(local_file)
# 设置更新文件生存时间和文件存储类型
def turnover_time(q, bucket_name, key):
# 初始化BucketManager
bucket = BucketManager(q)
# 您要更新的生命周期
days = '5'
bucket.delete_after_days(bucket_name, key, days)
# 1表示低频存储,0是标准存储
bucket.change_type(bucket_name, key, 1)
def main(name):
# 七牛云的空间
bucket_name = 'mysqldump'
# 七牛后保存的文件名
key = name
# 要上传文件的本地路径
local_file = "/data/mysqldump/%s" % name
# 填写你的Access Key 和 Secret Key #需要在七牛云获取
access_key = "access key"
secret_key = 'secret key'
# 构建鉴权对象
q = Auth(access_key, secret_key)
uploading(q, bucket_name, key, local_file)
turnover_time(q, bucket_name, key)
def mysql_dump(name):
path = '/data/mysqldump/%s' % name
path_tar = '%s.tar.gz' % path
cmd_mysql = "mysqldump -uroot -p12345.com --all-databases --lock-tables > %s" % path
cmd_tar = "tar zpPcf %s %s && rm -rf %s" % (path_tar, path, path)
cmd_find = "find /data/mysqldump/ -mtime +7 -name *.sql.tar.gz -exec rm -rf {} \;"
if os.path.isdir("/data/mysqldump/"):
if os.path.isdir(path_tar):
pid = os.getpid()
os.system('kill -9 %s' % pid)
else:
os.system(cmd_mysql)
os.system(cmd_tar)
os.system(cmd_find)
else:
os.mkdir("/data/mysqldump/")
os.system(cmd_mysql)
os.system(cmd_tar)
if __name__ == '__main__':
data = time.strftime('%Y-%m-%d', time.localtime())
name = 'all_%s.sql' % data
mysql_dump(name)
main(name=name_tar)
Python3备份MySQL数据库并上传到七牛云储存
觉得不错,支持一下!
发表评论
共 0 条评论
暂无评论