原来是ruby写的, 考虑不太全, 现在重写了一下。 按时间过滤相关东西


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
 
import  os
import  re
import  time
import  tarfile
import  string
 
bak_ser  =  "bacula@192.168.1.4"
tar_dir  =  "/tmp/auto_tar_bak"
ext_user  =  "/eda/bin/auto_bak.conf"
config  =  { "deep" : 8 "exclude" : "Code|INCA.libs" "filetype" : "bmp|png|pdf|vsd|rar|log|dat|bak|sdb" }
current_time  =  int (time.time())
 
def  check_bak(bak_dir):
     tmp_list  =  []
     for  filepath,pathlist,filelist  in  os.walk(bak_dir): 
         #filepath  fullpath
         #pathlist  fullpath child dir
         #filelist  child dir file
         if  filepath.count( "/" ) > =  config[ "deep" or  ".svn"  in  pathlist:
             continue
         for  filename  in  filelist:
             if  os.path.islink( "%s/%s"  %  (filepath, filename))  or  "/."  in  filepath  or  filename.startswith( "." or  re.findall(config[ "filetype" ], filename.split( "." )[ - 1 ], re.I)  or  re.findall(config[ "exclude" ], filepath):
                 continue
             elif  120  < =  os.path.getsize( "%s/%s"  %  (filepath, filename)) < =  10485760  and  current_time  -  os.stat( "%s/%s"  %  (filepath, filename))[ - 2 ] <  172800  and  istext( "%s/%s"  %  (filepath, filename)):
                 tmp_list.append( "%s/%s"  %  (filepath, filename))
     return  tmp_list           
 
def  istext(filename):
     s = open (filename).read( 512 )
     text_characters  =  " ".join(map(chr, range(32, 127)) + list(" \n\r\t\b"))
     _null_trans  =  string.maketrans(" ", " ")
     if  not  s:
         # Empty files are considered text
         return  True
     if  "\0"  in  s:
         # Files with null bytes are likely binary
         return  False
     # Get the non-text characters (maps a character to itself then
     # use the 'remove' option to get rid of the text characters.)
     =  s.translate(_null_trans, text_characters)
     # If more than 30% non-text characters, then
     # this is considered a binary file
     if  float ( len (t)) / float ( len (s)) >  0.30 :
         return  False
     return  True
 
def  create_tarfile(tar_name, filename, tar_list):
     if  not  os.path.isdir(tar_name):
         os.makedirs(tar_name)
     bak_tar  =  tarfile. open (tar_name  +  filename,  "w:bz2" )
     for  in  tar_list:
         bak_tar.add(i)
     bak_tar.close()
 
def  get_bak_dir():
     tmp  =  []
     with  open ( "/etc/auto.nfs" ) as f:
         for  in  f:
             if  os.uname()[ 1 in  and  not  i.startswith( "#" ):
                 tmp.append(i.split( "/" )[ - 1 ].rstrip())
     return  tmp
                 
if  __name__  = =  "__main__" :
     ext_list  =  []
     with  open (ext_user) as f:
         for  in  f.read():
             ext_list  =  i.split()
     for  dirname  in  get_bak_dir():
         if  dirname  in  ext_list:
             continue
         file_list  =  check_bak( "/local_home/"  +  dirname)
         if  file_list:
             filename  =  time.strftime( "/%F.tar.bz2" , time.localtime())
             create_tarfile(tar_dir  +  "/"  +  dirname, filename, file_list)
             os.system( "rsync -az --remove-source-files %s/%s%s %s:/bak/auto_bak/%s/"  % (tar_dir, dirname, filename, bak_ser, dirname))