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

Source: Internet
Author: User
Tags timedelta

[Python]
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
 
Import OS
Import re
Import tarfile
Import datetime
Import pexpect
Basedir = '/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 blank rows.
Continue
Else:
Line = line. lstrip ()
Line = line. rstrip () # Remove the carriage return (\ n)
Ret_list.append (line)
# Print ('debug info of get_list: \ n', ret_list)
Return 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. add (filename)
Tar. close ()


Scp = pexpect. spawn ('scp-R' + tarname + 'root @ IP:/data/databak/FilesBack /')
Scp. Distinct CT ('. ssword :*')
Scp. sendline ('Password ')
Scp. Reverse 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", "")
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

#! /Usr/bin/python
#-*-Coding: UTF-8 -*-

Import OS
Import re
Import tarfile
Import datetime
Import pexpect
Basedir = '/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 blank rows.
Continue
Else:
Line = line. lstrip ()
Line = line. rstrip () # Remove the carriage return (\ n)
Ret_list.append (line)
# Print ('debug info of get_list: \ n', ret_list)
Return 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. add (filename)
Tar. close ()


Scp = pexpect. spawn ('scp-R' + tarname + 'root @ IP:/data/databak/FilesBack /')
Scp. Distinct CT ('. ssword :*')
Scp. sendline ('Password ')
Scp. Reverse 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", "")
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

 

Related Article

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.