Python automatically backs up the SVN version library and copies it to the remote host script

Source: Internet
Author: User
Tags timedelta
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

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.