阅读:11679回复:2
如何利用python 备份mysql、4gl代码,并且上传到腾讯云存储备份。
代码包含python备份mysql、备份目录,然后将备份的内容上传到腾讯云存储进行备份,多重备份。有兴趣的朋友可以借鉴看看,希望对大家有帮助。腾讯云存储去官网看sdk说明,很简单的。https://www.qcloud.com/doc/product/430/5943
# -*- coding: utf-8 -* import os import time import datetime from qcloud_cos import CosClient from qcloud_cos import UploadFileRequest from qcloud_cos import UploadSliceFileRequest from qcloud_cos import UpdateFileRequest from qcloud_cos import UpdateFolderRequest from qcloud_cos import DelFileRequest from qcloud_cos import MoveFileRequest from qcloud_cos import DelFolderRequest from qcloud_cos import CreateFolderRequest from qcloud_cos import StatFileRequest from qcloud_cos import StatFolderRequest from qcloud_cos import ListFolderRequest def back(): global BACKUP_PATH global WeekDay WeekDay = get_week_day(datetime.datetime.now()) BACKUP_PATH = '/u1/topprod/tiptop/' + WeekDay if not os.path.exists(BACKUP_PATH):os.makedirs(BACKUP_PATH) rm_command = 'rm -rfr ' + BACKUP_PATH + '/*' os.system(rm_command) DB_HOST = 'localhost' DB_USER = 'root' DB_USER_PASSWORD = 'xxxxxx' DB_NAME = 'mydbs' DATETIME = time.strftime('%Y%m%d') print "creating backup folder" dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + DB_NAME + " > " + BACKUP_PATH + "/" + DB_NAME + ".sql" os.system(dumpcmd) target = BACKUP_PATH + '/tiptop.tar.gz' source = '/u1/topprod/tiptop ' + BACKUP_PATH + '/' + DB_NAME + '.sql' tar_command = '/bin/tar czvf %s %s > /dev/null 2>&1' % (target,source) os.system(tar_command) print "Backup script completed" unload_cos() def unload_cos(): # 设置用户属性, 包括appid, secret_id和secret_key # 这些属性可以在cos控制台获取(https://console.qcloud.com/cos) appid = 10050997 # 替换为用户的appid secret_id = u'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # 替换为用户的secret_id secret_key = u'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # 替换为用户的secret_key cos_client = CosClient(appid, secret_id, secret_key) # 设置要操作的bucket bucket = u'mycos' ############################################################################ # 文件操作 # ############################################################################ # 2. 上传文件(覆盖文件) # 将本地的local_file_2.txt上传到bucket的根分区下,覆盖已上传的sample_file.txt load_target = '/u1/topprod/tiptop/' + WeekDay + '/tiptop.tar.gz' load_source = BACKUP_PATH + '/tiptop.tar.gz' request = UploadFileRequest(bucket,load_target.decode('gbk'),load_source.decode('gbk')) request.set_insert_only(0) # 设置允许覆盖 upload_file_ret = cos_client.upload_file(request) print 'overwrite file ret:', repr(upload_file_ret) def get_week_day(date): week_day_dict = { 0 : 'Mon', 1 : 'Tue', 2 : 'Wed', 3 : 'Thu', 4 : 'Fri', 5 : 'Sat', 6 : 'Sun', } day = date.weekday() return week_day_dict[day] if __name__=="__main__": back() |
|
最新喜欢:rich51... |
沙发#
发布于:2016-11-23 11:44
666666
|
|
板凳#
发布于:2020-04-13 13:12
标记,攒分
|
|