Python multi-threaded application--DB2 database backup

Source: Internet
Author: User
Tags db2



Preface: DB2 An instance, can exist multiple databases, before using shell backup script, but only one database at the same time, for hundreds of g of backup files, this speed is obviously too slow, today learned Python multi-threading, just apply it.






Analysis: 

1, disk I/O allows the use of multi-threading, saving time, quite feasible.



2, Python multithreading in some scenarios is a chicken, but for I/o-intensive scenarios most suitable, here just.



3, the thread module has many problems, here use threading module.



4, the previous backup script modifies the port to clean the connected application, too violent, although all cold, but each restart overhead is too large, here use DB2 Force application all.



5, if the fourth step to clean the connection exception, then can not be backed up, temporarily did not find the appropriate way to catch the exception, to be considered, but using three consecutive cleanup, not special circumstances, 99.9% is OK.






Here is the specific code:



mythread.py


#!/usr/bin/env python
# encoding: utf-8
import threading

class MyThread(threading.Thread):
    #Thread子类
    def __init__(self,func,args,name=‘‘):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args
     
    def run(self):
        self.func(*self.args)


db2backup.py


#!/usr/bin/env python
# encoding: utf-8
import threading
import logging
import commands
Import sys
import MyThread
from time import ctime, sleep
logging.basicConfig(level=logging.DEBUG,
format=‘%(asctime)s %(filename)s[line:%(lineno)-1d] %(levelname)s %(message)s‘,
datefmt=‘%a, %d %b %Y %H:%M:%S‘,
filename=‘db2_backup.log‘,
filemode=‘a‘)
# logging.debug(‘This is debug message‘)
# logging.info(‘This is info message‘)
# logging.warning(‘This is warning message‘)
def db2_backup(inst_name, db_name, bak_dir):
#Implementing DB2 backup
#Check instance users
whoami = commands.getstatusoutput(‘whoami‘)
if whoami[1] != inst_name:
#Print 'incorrect user identity, current instance% s, profile instance% s'% (whoamI, inst_name)
Logging.error ('wrong user identity, current instance% s, profile instance% s'% (whoamI, Inst? Name))
Sys.exit ()
#Start backup
cmd = ‘db2 backup database %s to %s‘ % (db_name,bak_dir)
exec_res = commands.getstatusoutput(cmd)
if exec_res[0] == 0:
#Print% s backup succeeded. "% db_name
Logging.info ('%% s backup succeeded.'% (ctime(), db_name))
print exec_res[1]
return exec_res
def read_conf(file_path):
#Read database configuration to back up
Db_conf = []
with open(file_path,‘r‘) as f:
for line in f:
line_list = line.strip().split(‘:‘)
if len(line_list) != 3:
Logging. Warning ('% s line is not configured correctly,'% line. Strip())
Continue
cur_line = (line_list[0],line_list[1],line_list[2])
db_conf.append(cur_line)
return db_conf
Def main ():
db_conf = read_conf(‘database.cfg‘)
thread_num = len(db_conf)
#Instantiation thread
Threads = []
for line in db_conf:
t = MyThread.MyThread(db2_backup,line,db2_backup.__name__)
threads.append(t)
#Clear established connections
force_num = 3
while force_num:
commands.getstatusoutput(‘db2 force application all‘)
if force_num == 3:
Sleep (1)
force_num -= force_num
#Execution thread
for i in range(thread_num):
threads[i].start()
#Wait for thread synchronization, end process
for i in range(thread_num):
threads[i].join()
if __name__ == ‘__main__‘:
Main ()





Configuration information for the database to be backed up:



Database.cfg



Instance name, database name, backup directory


db2inst1:PTEST:/home/db2inst1
db2inst1:PTEST2:/home/db2inst1





Python multi-threaded application--DB2 database backup


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.