Perry
管理员
管理员
  • 发帖数213
  • QQ396572376
  • 金币43188两
  • 威望11394点
  • 钻石8888枚
  • 注册日期2016-11-09
  • 最后登录2024-11-03
  • VIP会员
  • 荣誉会员
  • 优秀斑竹
  • 最爱沙发
  • 原创写手
  • 社区居民
阅读:11679回复:2

如何利用python 备份mysql、4gl代码,并且上传到腾讯云存储备份。

楼主#
更多 发布于:2016-11-23 08:58
代码包含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()
喜欢1 评分0

最新喜欢:

rich5168rich51...
官方QQ群:556775727  
ubuntop
超级会员
超级会员
  • 发帖数41
  • QQ88888888
  • 金币421两
  • 威望184点
  • 钻石0枚
  • 注册日期2016-11-11
  • 最后登录2017-11-02
沙发#
发布于:2016-11-23 11:44
666666
回复(0) 喜欢(0)     评分
shoushaodon
钻石会员
钻石会员
  • 发帖数59
  • QQ549173465
  • 金币654两
  • 威望302点
  • 钻石0枚
  • 注册日期2020-03-27
  • 最后登录2020-09-24
  • 社区居民
  • 忠实会员
板凳#
发布于:2020-04-13 13:12
标记,攒分
回复(0) 喜欢(0)     评分
游客

返回顶部