Chinaunix首页 | 论坛 | 博客
  • 博客访问: 212848
  • 博文数量: 70
  • 博客积分: 2050
  • 博客等级: 大尉
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-15 21:42
文章分类

全部博文(70)

文章存档

2013年(1)

2011年(5)

2010年(3)

2009年(9)

2008年(17)

2007年(6)

2006年(29)

我的朋友

分类: Python/Ruby

2008-11-22 19:00:12

#!/usr/bin/python
#

import platform, os, sys, re

replaces = (
        (re.compile('AUTO_INCREMENT=\d+ '), ''),
        )

info = ('username', 'password', 'DB1', 'DB2')
servers = (('192.168.0.1', 'A'), ('192.168.0.2', 'B'), ('192.168.0.3', 'C'), ('192.168.0.4', 'D'))
snapshot = os.getenv('HOME') + '/snapshot.sql'

def probe_snapshot_sql(host, save):
    result = []
    file = platform.popen('mysqldump -h%s -u%s -p%s -d --add-drop-table=false --compact=true --databases %s' % (host, info[0], info[1], ' '.join(info[2:])), 'r')
    while True:
        l = file.readline()
        if l == '':
            break

        for pattern, replace in replaces:
            try:
                match = pattern.search(l)
                l = l[:match.start()] + replace + l[match.end():]
            except:
                pass
        result.append(l)
    file.close()
    file = open(save, 'w')
    file.writelines(result)
    file.close()

def tmpnam(host, name):
    return name + '.snapshot.sql';

def export():
    probe_snapshot_sql(servers[0][0], snapshot)

def status():
    all = [(host, tmpnam(host, name)) for host, name in servers]
    index = 0
    counts = 0
    for host, path in all:
        probe_snapshot_sql(host, path)
        if index > 0:
            file = platform.popen('diff -p %s %s' % (all[0][1], all[index][1]), 'r')
            different = file.readlines()
            file.close()
            if len(different) != 0:
                if counts != 0:
                    print '\n%s\n' % ('/' * 80, )
                sys.stdout.writelines(different)
                counts = counts + 1
        index = index + 1

def clear():
    for host, name in servers:
        try:
            os.unlink(tmpnam(host, name))
        except:
            pass

key = sys.argv[1].upper()
for command in (export, status, clear):
    if command.__name__.upper().startswith(key):
        command()
        break

文件:dbma.zip
大小:1KB
下载:下载

文件:snap.zip(第二版)
大小:0KB
下载:下载
阅读(634) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~