Python automatically backs up the SVN repository and replicates to the remote host script

Source: Internet
Author: User
#!/usr/bin/python #-*-coding:utf-8-*-import osimport reimport tarfileimport datetimeimport pexpectbasedir= '/data/b        ak/' #文件夹 iplist=[']# IP address def get_list (txt_file): Ret_list = [] fin = open (txt_file, ' R ') for line in fin:            if (Re.match (' ^\\s*$ ', line)): #跳过是空白的行 Continue else:line = Line.lstrip () line = Line.rstrip () #将回车 (\ n) remove Ret_list.append (line) #print (' Debug info of get_list: \ n ', ret_list) r Eturn ret_list def copy_svn (filelist): #这个函数主要是完成dump name= [] name=os.path.split (filelist) now = DateTime  . DateTime.Now () filename = Now.strftime (basedir+iplist[0] +name[-1]+ ' _%y%m%d_%h%m%s.dump ') os.system (' Svnadmin dump ' + filelist + ' > ' +filename ' tarname = now.strftime (basedir+iplist[0]+ ' _svndump_ ' +name[-1]+ ' _%y%m%d_%h%m%                  S.tar.gz ') #print tarname tar = Tarfile.open (tarname, ' W|gz ') tar.add (filename) tar.close () SCP = Pexpect.spawn (' SCP -R ' + tarname + ' root@ip:/data/databak/filesback/') scp.expect ('. ssword:* ') scp.sendline (' Password ') scp.expect ( Pexpect.              EOF, Timeout=none) olddate = (Now-datetime.timedelta (5)). Strftime ("%y%m%d") print Olddate For I in Os.listdir (basedir): File = Re.search (R ' \w*[_] (\d{8}) [_]\d{6}. ( Tar.gz|dump) ', i) #print i, file if file and Olddate>=file.group (1): Os.remove (basedir + file . Group (0)) print ' del: ', File.group (0) Filelog=open ("/data/bak/bak.log", "a") Filelog.wri Te ("============date:%s============= \ n"% now.strftime ("%y%m%d")) Filelog.write ("Del file:%s \ n"% (Basedir+fil E.group (0)) filelog.write ("============date:%s============= \ n"% now.strftime ("%y%m%d")) Filelog.close       () def copy_files (txt_file): Geted_list = Get_list (txt_file) for file in Geted_list:copy_svn (file) if __name__ = = ' __main__ ': copy_files ('/data/bak/fileBak.txt ') print ' = ' *20, ' \ncopy_okokok\n ', ' = ' *20 #!/usr/bin/python#-*-coding:utf-8-*-import osimport reimport tar  FileImport datetimeimport pexpectbasedir= '/data/bak/' #文件夹iplist =[']# IP address def get_list (txt_file): Ret_list = [] fin = Open (Txt_file, ' R ') for line in Fin:if (Re.match (' ^\\s*$ ', line)): #跳过是空白的行 continue Els E:line = Line.lstrip () line = Line.rstrip () #将回车 (\ n) Remove the Ret_list.append (line) #print ( ' Debug info of get_list: \ n ', ret_list) return ret_list def copy_svn (filelist): #这个函数主要是完成dump name= [] name=o S.path.split (filelist) now = Datetime.datetime.now () filename = Now.strftime (basedir+iplist[0] +name[-1]+ ' _%y%m%d_% H%m%s.dump ') os.system (' svnadmin dump ' + filelist + ' > ' +filename) tarname = Now.strftime (Basedir+iplist[0] + ' _svndump_ ' +name[-1]+ ' _%y%m%d_%h%m%s.tar.gz ') #print tarname tar = Tarfile.open (tarname, ' W|gz ') Tar.add (filen     AME) Tar.close ()         SCP = pexpect.spawn (' scp-r ' + tarname + ' root@ip:/data/databak/filesback/') scp.expect ('. ssword:* ') SC P.sendline (' password ') scp.expect (pexpect.  EOF, Timeout=none) olddate = (Now-datetime.timedelta (5)). Strftime ("%y%m%d") print olddate for I In Os.listdir (basedir): File = Re.search (R ' \w*[_] (\d{8}) [_]\d{6}. ( Tar.gz|dump) ', i) #print i, file if file and Olddate>=file.group (1): Os.remove (basedir + file. Group (0)) print ' del: ', File.group (0) Filelog=open ("/data/bak/bak.log", "a") filelog.write ("============date:%s============= \ n"% now.strftime ("%y%m%d")) Filelog.write ("Del file:%s \ n"% (basedir+file.    Group (0)) filelog.write ("============date:%s============= \ n"% now.strftime ("%y%m%d")) Filelog.close () def copy_files (txt_file): Geted_list = Get_list (txt_file) for file in Geted_list:copy_svn (file) if __na me__ = = ' __main__ ': copy_files('/data/bak/filebak.txt ') print ' = ' *20, ' \ncopy_okokok\n ', ' = ' *20 
  • 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.