[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