Python 备份MySQL,并同步rsync server

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# Copyright 2011 Justin Santa Barbara
# All Rights Reserved.
# Copyright (c) 2010 Citrix Systems, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
import os,sys,time,fcntl,struct
import commands
import traceback
import socket
import shutil
import sys
try:
    from hashlib import md5
except:
    from md5 import md5
def logging(item,level,mes):
    logpath = ‘/var/log/kxtools/‘
    if not os.path.exists(logpath):
        os.makedirs(logpath)
    fp = open(‘%s/kxbackup.log‘%logpath,‘a‘)
    fp.write(‘%s - %s - %s - %s \n‘%(time.ctime(),item,level,mes))
    fp.close()
"""Access file md5 value"""
def MD5(fname):
    filemd5 = ""
    try:
        file = open(fname, "rb")
        md5f = md5()
        strs = ""
        while True:
            strs = file.read(8096)
            if not strs:
                break
            md5f.update(strs)
        filemd5 = md5f.hexdigest()
        file.close()
        return filemd5
    except:
        logging(‘MySQL backup‘,‘ERROR‘,traceback.format_exc())
def get_em_ipaddr(dev):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    IP = socket.inet_ntoa(fcntl.ioctl(
           s.fileno(),
           0x8915,  # SIOCGIFADDR
           struct.pack(‘24s‘,dev)
    )[20:24])
    return IP
def IPADDR():
    """
    Get host name, ip
    return(hostname, ip)
    """
    def _get_ipaddr():
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(("8.8.8.8", 8000))
            return s.getsockname()[0]
        except:
            LOG.error(traceback.format_exc())
        s.close()
    return (socket.gethostname(), _get_ipaddr())
def COMM(cmd):
    # Call system commands
    try:
        x,y = commands.getstatusoutput(cmd)
        if x != 0:
            LOG.error(y)
            print x,y,cmd
        return x,y
    except:
        LOG.error(traceback.format_exc())
class BackupMySQLDB(object):
    def __init__(self,kwargs):
        version = 1.0
        self.kwargs = kwargs
    def deleteDB(self):
        # Keep the data backup before two days
        reserve = list()
        cctime = time.time()
        for f in os.listdir(self.kwargs[‘backup_path‘]):
            if f.find(‘gz‘) != -1:
                reserve.append(f)
        for  x in reserve:
            f = ‘%s%s‘ %(self.kwargs[‘backup_path‘],x)
            fctime =  os.stat(f).st_ctime
            if (cctime - fctime ) > 172800:
                shutil.move(f ,‘/data0/reserve/‘)
                mes = ‘Delete file %s is oK‘%f
                logging(‘MySQL backup‘,‘INFO‘,mes)
    def backupDB(self):
        # Dump MySQL db ,zip,rsync to server
        ctimes = time.strftime("%Y%m%d")
        namesql = ‘%s_%s.sql‘ %(ctimes,IPADDR()[1])
        fname = ‘%s/%s.gz‘ %(self.kwargs[‘backup_path‘],namesql)
        if not os.path.exists(self.kwargs[‘backup_path‘]):
            os.makedirs(kwargs[‘backup_path‘])
        # mysqldump file to /data0/backup/
        x, y  = COMM("/usr/local/mysql/bin/mysqldump -u%s -p%s -S /var/lib/mysql/mysql.sock --opt  %s > %s/%s"                %(self.kwargs[‘user‘],self.kwargs[‘pass‘],self.kwargs[‘dbname‘],self.kwargs[‘backup_path‘],namesql))
        if x != 0:
            mes = (‘mysqldump file %s is failure‘%namesql)
            logging(‘MySQL backup‘,‘ERROR‘,mes)
        else:
            mes = (‘mysqldump file %s is oK‘%namesql)
            logging(‘MySQL backup‘,‘INFO‘,mes)
        os.chdir(self.kwargs[‘backup_path‘])
        # Tar sql file
        x,y = COMM("tar -czvf %s.gz %s" %(namesql,namesql))
        if x != 0:
            mes = (‘tar file %s is failure‘%namesql)
            logging(‘MySQL backup‘,‘ERROR‘,mes)
        else:
            mes =(‘tar file %s is oK‘%namesql)
            logging(‘MySQL backup‘,‘INFO‘,mes)
        # Create MD5 values
        md5 =  MD5(fname)
        newname = fname.split(‘.sql.gz‘)[0] + ‘_%s‘%md5 + ‘.sql.gz‘
        shutil.move(fname , newname)
        # Rsync to server 192.168.223.51
        x, y = COMM("rsync  -avz %s  %s::%s/" %(newname,self.kwargs[‘rsync_ip‘],self.kwargs[‘rsync_model‘]))
        if x != 0:
            mes = "rsync file %s.gz is failure" %namesql
            logging(‘MySQL backup‘,‘ERROR‘,mes)
        else:
            mes = "rsync file %s.gz is oK" %namesql
            logging(‘MySQL backup‘,‘INFO‘,mes)
        # Delete sql file
        shutil.move(namesql,‘/dev/null‘)
    def work(self):
        self.deleteDB()
        self.backupDB()
if __name__ == "__main__":
    kwargs = {
        ‘user‘ : ‘admin‘,
        ‘pass‘ : ‘admin‘,
        ‘host‘ : ‘localhost‘,
        ‘dbname‘ : ‘abbs‘,
        ‘rsync_ip‘ : ‘192.168.223.51‘,
        ‘rsync_model‘ : ‘abbs_backup‘,
        ‘backup_path‘ : ‘/data0/backup/‘
        }
    sc = BackupMySQLDB(kwargs)
    sc.work()

Python 备份MySQL,并同步rsync server,布布扣,bubuko.com

时间: 2024-10-12 21:03:45

Python 备份MySQL,并同步rsync server的相关文章

linux定时备份mysql并同步到其它服务器

1.mysql的定期备份:2.同步到其它服务器 mysql 备份 备份还原某个数据库 备份还原 # 导出数据库 /usr/bin/mysqldump -u root -ppwd database > database20160929.sql # 导入数据库 mysql -u root -p database < database20160929.sql 备份到压缩文件从压缩文件导入 #备份到压缩文件 /usr/bin/mysqldump -u root -ppwd database  | gz

python备份mysql脚本

今天简单的写了个python的mysql备份脚本,其实也不是很难呀.比shell简洁了很多! 开整: 注释都用英文写了,有些英语基础的朋友应该都可以看得懂了! #!/usr/bin/env python #backup the gtshop #author:ley #encoding=utf8 #date:2015-06 import os,sys,datetime,time from stat import * #mysqlbackup user User = 'root' #mysqlbac

python备份mysql数据库并发送邮件

#!/usr/bin/env python #coding:utf-8 #Write by JIANGLEI.YU #Update On  2016-01-24 20:26 import os import time import sys import datetime import smtplib import string from stat import * #Define DB  Informations HOST = '192.168.0.135' USER = 'root' PASS

python备份mysql数据库

原本可以用shell完成的功能,现在学习python,就照抄照改.完成数据库备份. #!/usr/bin/python #-*-coding:utf-8-*- #MYSQL BACK import string,time,os,datetime import sys,logging,stat import subprocess #os.environ.get('PERONA_A') os.environ["PATH"]="/usr/local/mysql/bin/:"

一个好用的Python备份mysql的脚本

前几天打算用Python写一个mysql脚本,上Google看了下老外写的,写的挺好的,原地址在http://tecadmin.net/python-script-for-mysql-database-backup/#,所以就给 copy过来了 1 #!/usr/bin/python 2 ########################################################### 3 # 4 # This python script is used for mysql

rsync+inotify同步备份MYSQL数据

rsync+inotify同步备份MYSQL数据 rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,但是rsync不能实时的去监测.同步数据.inotify 是一种强大的.细粒度的.异步的文件系统事件监控机制,通过inotify可以监控文件系统中添加.删除,修改.移动等各种细微事件. 实验环境:备份端192.168.1.123(rsync server) 备份源192.168.124(rsync client inotify mysql)

linux和windows同步数据 cwrsync client to rsync server

linux和windows同步数据,rsync server  cwrsync client linux server一般系统都自带rsync,如果没有就挂载系统盘自己安装一下,安装挺简单的不用我再多说了vi /etc/rsyncd.confuid = daemon                            //这个用户是系统用户,当rsync客户端连接上服务器后,会映射成这个用户上传或者下载文件gid = daemon                            //组名

Windows服务器同步rsync,增量备份

这几天要同步两台服务器,都是Windows的服务器,我就拿自己的电脑先进行测试啊,毕竟服务器上的网站还在有人用,不能瞎搞,经过了两天辛酸的常试,终于成功了,现在分享给大家,言归正传. 说说的电脑配置以及使用的软件吧: 服务端: Windows7系统64bit 4G内存 ip地址为 192.168.1.13 cwRsyncServer_4.1.0_Installer.exe 客户端: Windows7系统64bit 4G内存 ip地址为192.168.1.56 cwRsync_4.0.3_Inst

mysql数据库负载均衡高可用之主从、主主备份,实时同步

一:MySQL Replication 什么是MySQL Replication Replication可以实现将数据从一台数据库服务器(master)复制到一或多台数据库服务器(slave) 默认情况下属于异步复制,无需维持长连接 通过配置,可以复制所有的库或者几个库,甚至库中的一些表 是MySQL内建的,本身自带的 Replication的原理 简单的说就是master将数据库的改变写入二进制日志,slave同步这些二进制日志,并根据这些二进制日志进行数据操作 DML:SQL操作语句,upd