Python 生產環境MySQL資料庫增量備份指令碼

來源:互聯網
上載者:User

標籤:

MySQL資料庫常用的辦法是通過MySQLdump匯出sql進行備份,但是不適合資料量很大的資料庫,速度,鎖表是兩個嚴重的問題。前面寫了一遍文章介紹xtrabackup的熱備工具,見 http://www.linuxidc.com/Linux/2015-02/113058.htm。下面的指令碼是基於xtrabackup實現自動備份資料庫的功能。

需求描述:

每天晚上23點,對資料庫進行一次完整備份。第二天0-22點,每小時進行一次增量備份。每次備份前把上次的完整備份和23次增量備份移動到指定目錄裡,保留7天的資料。

ps:不要問我,為什麼是23點執行完整備份,0點不更好處理嗎?bingo,這是我們的業務需求,呵呵,不能說太細,你懂得。不要問我,為啥用不用shell,至少不用寫subprocess.call來調系統命令。我只能說,我樂意,你管的著?哈哈!

#-*- coding: UTF-8 -*- 
#!/usr/bin/python 
#==================================================== 
# Author: gaochenchao - EMail:[email protected] 
# Last modified: 2015-2-5 
# Filename: innobackup.py 
# Description: backup mysql files,base percona xtrabackup 
# blog:gccmx.blog.51cto.com 
#==================================================== 
import datetime 
import subprocess 
import os 
import sys 
import logging 
logging.basicConfig(level=logging.DEBUG, 
                format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘, 
                datefmt=‘%a, %d %b %Y %H:%M:%S‘, 
                filename=‘backup.log‘, 
                filemode=‘a‘) 
backuser = ‘bkuser‘
backpass = ‘bk2015‘
basedir = ‘/mnt/backups‘
tomorrowdate = datetime.date.fromordinal(datetime.date.today().toordinal()+1).strftime("%y%m%d") 
todaydate = datetime.datetime.now().strftime("%y%m%d") 
fullback_dir = "%s/%s" %(basedir,tomorrowdate) 
cuhour = datetime.datetime.now().strftime("%H") 
#cuhour = sys.argv[1] 
increment_dir = ‘%s/%s‘ %(basedir,cuhour) 
increbase_dir=‘‘ 
stores = ‘/mnt/stores‘
  
#轉儲老的備份資料,移動到以當前年月日命名的檔案夾內,目錄如150209-bak 
def storebefore(): 
    suffix = datetime.datetime.now().strftime("%y%m%d") 
    storedir = "%s/%s-bak" %(stores,suffix) 
    if not os.path.exists(storedir): 
        subprocess.call("mkdir -p %s" %(storedir),shell=True) 
    command = "cd %s && mv * %s" %(basedir,storedir) 
    subprocess.call(command,shell=True) 
      
# 刪除轉儲目錄中超過7天的備份資料 
def cleanstore(): 
    command = "find %s -type d -mtime +7 |xargs rm -fr" % stores 
    subprocess.call(command,shell=True) 
  
# 備份方法,每天23點完整備份,第二天0-22點增量備份 
def backup(): 
    if not os.path.exists(basedir): 
        subprocess.call("mkdir -p %s"%basedir,shell=True) 
    commandfull = "innobackupex --user=%s --password=%s --no-timestamp %s" %(backuser,backpass,fullback_dir) 
    if cuhour == ‘23‘: 
        storebefore() 
        subprocess.call("rm -fr %s/*"%basedir,shell=True) 
        subprocess.call(commandfull,shell=True) 
        logging.info(commandfull) 
    else: 
        if int(cuhour) - 1 >= 0: 
            increbase_dir = ‘%s/%s‘ %(basedir,str(int(cuhour) - 1)) 
        else: 
            increbase_dir = "%s/%s" %(basedir,todaydate) 
        if not os.path.exists(increbase_dir): 
            logging.info(‘上次的增量備份目錄 [%s] 不存在,終止執行‘%increbase_dir) 
            exit(0) 
        commandincre = "innobackupex --user=%s --password=%s --no-timestamp --incremental %s --incremental-basedir=%s" %( 
                        backuser,backpass,increment_dir,increbase_dir) 
        subprocess.call(commandincre,shell=True) 
        logging.info(commandincre) 
          
if __name__ == ‘__main__‘: 
    backup() 
    cleanstore()

MySQL管理之使用XtraBackup進行熱備 http://www.linuxidc.com/Linux/2014-04/99671.htm

MySQL開源備份工具Xtrabackup備份部署 http://www.linuxidc.com/Linux/2013-06/85627.htm

MySQL Xtrabackup備份和恢複 http://www.linuxidc.com/Linux/2011-12/50275.htm

用XtraBackup實現MySQL的主從複製快速部署【主不鎖表】 http://www.linuxidc.com/Linux/2012-10/71919p2.htm

安裝和使用 Percona 推出的 Xtrabackup 備份 MySQL http://www.linuxidc.com/Linux/2011-10/44451.htm

XtraBackup 的詳細介紹:請點這裡
XtraBackup 的:請點這裡

本文永久更新連結地址:http://www.linuxidc.com/Linux/2015-02/113057.htm

Python 生產環境MySQL資料庫增量備份指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.