System Configuration File Backup comparison

Source: Internet
Author: User

# -*- coding: gbk -*-import difflibimport osimport timeimport  Shutilfrom afalogger import loggererror,loggerinforfrom traceback import format_ Exc;def comparefile (file1,file2): #比较两个文件的内容是否相同     try:         file_handle_1 = open (file1)              file_handle_1_read = file_handle_1.readlines ()          file_handle_1.close () #        loggerinfor (str (file_handle_ 1_read))                 file_ Handle_2 = open (file2)         file_handle_2_read =  file_handle_2.readlines ()         file_handle_2.close ()  #     &nbsP;  loggerinfor (str (file_handle_2_read))                  diff =  '. Join (Difflib.ndiff (file_handle_1_read,file_handle_2_ Read))         diffInfo=[]         for line in diff.split (' \ n '):             if line != ':                 if line[0] in  (' + ', '-'):                     diffinfo.append (line)                  else:                      continue        return diffinfo    except:         loggerinfor (str (file2+ ' \nerror '))   #将错误的文件写入日志, error occurs when the file name is garbled          return [file2+ ' \nerror ',]def compareresultwritefile (file2,diffInfo,resultFilename) : #将不同的地方写入文件     file_handle = open (Resultfilename, ' a ')      File_handle.write (file2+ '   start\n ')     for line in diffinfo:         file_handle.write (line+ ' \ n ')     file_ Handle.write (file2+ '   end\n\n ')     file_handle.close () Def getAllFilename ( Directoryorfilename): #在输入为目录的时候 to get all the files under the directory     allfilename=[]    for  name in directoryorfilename.split (', '):         if  os.path.isdir (name):  &NBSp;          for path,dirs,files in os.walk ( Name):                 for  fn in files:                     allfilename.append (path+ '/' +fn)          elif os.path.isfile (name):             allfilename.append (name)         else:             continue    return allfilenamedef  backupfile (resultdir,filename): #将本日文件备份     try:         filepath_list=filename.split ('/')         filepath= ' _ '. Join (Filepath_list[3:-1])  &NBSp;      shutil.copy (filename,resultdir+ '/' +filepath_list[-1]+ ' _ ' +filepath)     except:        return  ' def  Cfgcompare (Sourcefile,destinationdir):     "      @ Use example:     cfgcompare ('/home/iqms/lyb,/home/iqms/qm_info ', '/home/iqms/cfgdir ')       '     try:        if not type ( SourceFile)  is str:            return  [0,  "ERR001",  "sourcefile parameter must be str type", [none]];         if not type (Destinationdir)  is str:             return [0,  "ERR001",  "Destinationdir parameter must be str type", [none]];        &nBsp;        allfilename = getallfilename (sourceFile)          yesterdaydestinationdir = destinationdir+ '/' +time.strftime ('%y%m%d ', Time.localtime (Time.time () -24*60*60))         destinationdir  = destinationdir+ '/' +time.strftime ('%y%m%d ')          Os.mkdir (Destinationdir)         for filename in  allfilename:            filepath_list= Filename.split ('/')   #拼文件名             if  Filepath_list[-1].startswith ('. '):                 continue             Filepath= ' _ '. Join (Filepath_list[3:-1])   #拼文件名            backupfile (DestinationDir,filename)             if os.path.exists ( Yesterdaydestinationdir):#                 diffinfo=comparefile (yesterdaydestinationdir+ '/' +filename.split ('/') [ -1],filename] #                 diffinfo=comparefile ( yesterdaydestinationdir+ '/' +filename.split ('/') [ -1],destinationdir+ '/' +filename.split ('/') [-1])                  diffinfo=comparefile ( yesterdaydestinationdir+ '/' +filepath_list[-1]+ ' _ ' +filepath,destinationdir+ '/' +filepath_list[-1]+ ' _ ' +filepath '                  Compareresultwritefile (Filename.split ('/') [ -1],diffinfo,destinationdir+ '/log ')             else:                 continue         return [1, None, None, [None]];     Except exception, e:        loggererror (str (FORMAT_EXC ()));         return [2,  ' A015017 ',  ' exception: ' +str ( e ),  [None]];

System configuration file backup comparison

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.