pqwsa 发表于 2018-9-27 07:23:34

MySQL备份方案(综述及脚本)

#!/usr/bin/python  
#coding:utf-8
  
#auther:Bran Guo
  
#date:10/26/2015
  
#version:V1.0
  

  
import os,ConfigParser
  

  
#读取配置文件
  
conf = ConfigParser.ConfigParser()
  
conf.read("mysqlbak.conf")
  

  
bakdir_new = conf.get('file_path','bakdir_new')
  
bakdir_last = conf.get('file_path','bakdir_last')
  
mysql_data_path = conf.get('mysql_set','mysql_data_path')
  
dirlist_new = os.popen('ls %s' % bakdir_new).readlines()
  
dirlist_last = os.popen('ls %s' % bakdir_last).readlines()
  

  
#备份函数
  
def restore(dir_count=len(dirlist_new),bakdir=bakdir_new,dirlist=dirlist_new):
  if dir_count == 1:
  print dir_count,bakdir,dirlist
  ret=os.system('innobackupex --apply-log --redo-only %s/%s' %(bakdir,dirlist))
  if ret != 0:
  print "prepare failed"
  exit()
  else:
  ret=os.system('innobackupex --apply-log --redo-only %s/%s' %(bakdir,dirlist))
  count = 1
  while (count < dir_count):
  incrdir = dirlist
  basedir = dirlist
  os.environ['incrdir'] = str(incrdir)
  os.environ['basedir'] = str(basedir)
  os.environ['bakdir'] = str(bakdir)
  ret=os.system('innobackupex --apply-log --redo-only $bakdir/$basedir--incremental-dir=$bakdir/$incrdir')
  if ret != 0:
  print "prepare failed"
  count +=1
  os.system('service mysqld stop')
  os.system('rm -rf %s' % mysql_data_path)
  os.system('innobackupex --copy-back %s/%s' %(bakdir,dirlist))
  os.system('chown -R mysql:mysql %s' % mysql_data_path)
  os.system('service mysqld start')
  

  
#输入菜单
  
while True:
  user_input = raw_input('Command (m for help):').strip()
  if user_input == 'm':
  print '''Warning: The following command will remove mysql datafile, should be used with caution.
  rrestore to recent backup
  sshow backup list
  nchoose backup restore from new
  lchoose backup restore from last
  qquit
  
''',
  elif user_input == 'r':
  restore()
  elif user_input == 'q':
  exit()
  elif user_input == 's':
  print 'New:'
  os.system('ls %s' % bakdir_new)
  print 'Last'
  os.system('ls %s' % bakdir_new)
  elif user_input == 'n':
  os.system('ls -l %s' % bakdir_new)
  while True:
  user_input = raw_input('Please enter line number restore:').strip()
  if user_input == 'q':
  exit()
  try:
  line_number = int(user_input)
  dir_count = len(dirlist_new)
  if line_number
页: [1]
查看完整版本: MySQL备份方案(综述及脚本)