#!/usr/bin/env python import urllib2,os,datetime,subprocess,sys,glob,re,ConfigParser,getopt #cmd [$path] | <make|bak> #qq:123769752 #g_linux@qq.com #version 2.0 #2012/11/28 ################### def recover(): #vail_day=7 logfile_path="/var/update/bakfile" logfile_format="bakweb+" today=datetime.date.today() old_day=datetime.timedelta(days=7) file_date=today-old_day files=glob.glob("/var/update/bakfile"+logfile_format+"*"+str(file_date)+"*") #take file for file in files: subprocess.os.remove(file) #### ################################ def calc(path): filter={'filter':'--exclude "*.jpg" --exclude "*.tar" --exclude "*.sql" --exclude "*.SQL" --exclude "*.gz" --exclude "*.exe" --exclude "*.gif" --exclude "*.log" --exclude "*.png" --exclude "*.apk" --exclude "*.rar" --exclude "*.zip"'} i=0 #server_name j=0 #root ser_root={} ser_name={} tjroot=re.compile("[\s]+root[\s]+\/") tjserv=re.compile("[\s]+server_name[\s]+[\w]+") filename=subprocess.os.popen("find "+path+" -name '*.conf'") for file in filename.read().split("\n"): if subprocess.os.path.exists(file): fd=open(file,'r') body=fd.read().split("\n") for tj in body: if '#' not in tj: #Starting with # are ignored if "server_name" in tj: #Find this line if there is 'server_name' if i==j: if tjserv.findall(tj): #if this line have server_name rule so use re compare i=i+1 ser_name[i]=tj.strip() #tj line data add ser_name elif i>j: if tjserv.findall(tj): ser_name[i-1]=tj.strip() if "root" in tj: if i-j==1: if tjroot.findall(tj): j=j+1 ser_root[j]=tj.strip() elif j==i: if tjroot.findall(tj): ser_root[j-1]=tj.strip() if subprocess.os.path.exists("/var/log/baksit.conf"): subprocess.os.remove("/var/log/baksit.conf") fd=open("/var/log/baksit.conf","w+") for key in ser_root.keys(): for name in re.split(r"\s+|;",ser_name[key])[1:2]: fd.writelines("["+str(name)+"]"+"\n") fd.writelines("name="+str(name)+"\n") for root in re.split(r"\s+|;",ser_root[key])[1:2]: fd.writelines("path="+str(root)+"\n") fd.writelines("filter="+str(filter['filter'])+"\n") fd.flush() fd.close() ###############bak file def baksitfile(): sitfile=ConfigParser.ConfigParser() sitfile.read("/var/log/baksit.conf") node=sitfile.sections() for inode in node: name=sitfile.get(inode,"name") path=sitfile.get(inode,"path") filter=sitfile.get(inode,"filter") bfdate=str(datetime.date.today()) subprocess.call('tar %s -cvjf /var/update/bakfile/bakweb+%s%s.tar.gz %s'%(filter,name,bfdate,path),shell=True) ##################main def main(path,action): recover() if path =="NULL": if subprocess.os.path.exists("/usr/local/nginx/conf"): ngpath="/usr/local/nginx/conf" elif subprocess.os.path.exists("/opt/www/nginx/conf"): ngpath="/opt/www/nginx/conf" else: ngpath=str(path) if action == "bak": baksitfile() subprocess.sys.exit() elif action == "make": calc(ngpath) ########################### def menu(): path="NULL" action="NULL" opts,args=getopt.getopt(sys.argv[1:],"hrbmp:",["bak","make","path="]) for opt,arg in opts: if opt in ("-h","--help"): print '''<[-b][-m]> <-p nginx config file path> -b is bakfile -r is recover file -m is make config file -p is find nginx config file''' elif opt in ("-r","--recover"): recover() elif opt in ("-b","--bak"): action="bak" elif opt in ("-m","--make"): action="make" elif opt in ("-p","--path"): path=arg if path == "NULL" and action=="NULL": subprocess.sys.exit() else: main(path,action)