Python automatically backs up the SVN version library and copies it to the remote host script
#! /Usr/bin/python #-*-coding: UTF-8-*-import osimport reimport tarfileimport datetimeimport pexpectbasedir = '/data/bak/' # folder iplist = [''] # IP address def get_list (txt_file ): ret_list = [] fin = open (txt_file, 'r') for line in fin: if (re. match ('^ \ s * $', line): # skip a blank row. continue else: line = line. lstrip () line = line. rstrip () # Press enter (\ n) to remove ret_list.append (line) # print ('debug info of get_list: \ n', ret_list) r Eturn ret_list def copy_svn (filelist): # This function completes 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] + '_policy1_m1_d_1_h1_m1_s.tar.gz ') # print tarname tar = tarfile. open (tarname, 'W | gz ') tar. ad D (filename) tar. close () scp = pexpect. spawn ('scp-R' + tarname + 'root @ IP:/data/databak/FilesBack/') scp. CT ('. ssword: * ') scp. sendline ('password') scp. secondary CT (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}) [_] \ d1_62.16.(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. 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 g Eted_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 tarfileimport datetimeimport pexpectbasedir = '/data/bak/' # folder iplist = [''] # IP address def get_list (txt_file ): ret_list = [] fin = open (txt_file, 'r') for line in fin: if (re. match ('^ \ s * $', line): # skip a blank row. continue else: line = line. lstrip () line = line. rstrip () # Press enter (\ n) to remove ret_list.append (line) # print ('debug info of get_list: \ n', ret_list) return ret_list def copy_svn (filelist ): # This function is mainly used to complete 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] + '_policy1_m1_d_1_h1_m1_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. CT ('. ssword: * ') scp. sendline ('password') scp. secondary CT (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}) [_] \ d1_62.16.(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. 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 _ name _ = '_ main __': copy_files ('/data/bak/filebak.txt') print '=' * 20, '\ ncopy_OKOKOK \ n',' = '* 20