自动查找相关文件.上传到服务器
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#!/usr/bin/ruby # coding: utf-8 require 'date'
$result = []
bak_ser = "bacula@192.168.100.24"
tar_dir = "/tmp/auto_tar_bak"
def check(top_dir, ext_type, exclude, dir_deep)
content = `ls - 1 #{top_dir}`.split
for i in content
i = top_dir + "/" + i
if File .directory?(i)
next if File .ftype(i) == 'link'
begin
next if i.scan( '/' ).length >= dir_deep
rescue
next if i.force_encoding( 'GBK' ).scan( '/' ).length >= dir_deep
end
next if i.match(/ #{exclude}/)
unless Dir .entries(i).include?( '.svn' )
begin
check(i, ext_type, exclude, dir_deep)
rescue
end
end
elsif File .ftype(i) == 'file'
begin # maybe no ext name
unless i.scan(/\.[^\.]+$/)[ 0 ].match(/ #{ext_type}/i)
if File .size(i) <= 1024000 && File .size(i) >= 120
if `file \" #{i}\"`.include?('ASCII') || i.scan(/\.[^\.]+$/)[0].match(/doc|docx|xlsx|xls/i)
$result << "." + i
end
end
end
rescue
#unless i.force_encoding('GBK').scan(/\.[^\.]+$/)[0].match(/bmp|png|pdf|vsd|rar|log|dat|bak/i)
if File .size(i) <= 1024000 && File .size(i) >= 120
if `file \" #{i}\"`.include?('ASCII') #|| i.force_encoding('GBK').scan(/\.[^\.]+$/)[0].match(/doc|docx|xlsx|xls/i)
$result << "." + i
end
end
#end
end
end
end
end def write_file(path,str)
aFile = File . new (path, "w" )
aFile.puts str
aFile.close end def delete_dir_line(dir)
dir[ 0 ] == '/' ? dir[ 1 ..- 1 ] : dir
end def import_config(home_name)
path_in, path_ex, filetype = [], [], []
config = { :deep => 8 , :exclude => path_ex, :filetype => filetype, :include => path_in}
begin
File .open( "/local_home/#{home_name}/config.txt" , 'r' ). each do |line|
if line.match(/^deep/)
config[ :deep ] = line.split( '=' )[- 1 ].to_i if line.split( '=' )[- 1 ].to_i + 2 >= 1
elsif line.match(/^exclude/)
line.split( '=' )[- 1 ].split( ';' ). each {|x| path_ex << "/local_home/#{home_name}/" + delete_dir_line(x.chomp) if x.length > 1 }
elsif line.match(/^include/)
line.split( '=' )[- 1 ].split( ';' ). each {|x| path_in << "/local_home/#{home_name}/" + delete_dir_line(x.chomp) if x.length > 1 }
elsif line.match(/^filetype/)
line.split( '=' )[- 1 ].split( ';' ). each {|x| filetype << x.chomp if x.length > 1 }
end
end
rescue
config = { :deep => 8 , :exclude => path_ex, :filetype => filetype, :include => path_in}
end
path_ex. each { |x| path_ex.delete(x) if path_in.index(x) }
return config
end # get home user home_user = `grep $(hostname) /etc/auto.nfs | awk - F \ '/local_home/\' \'{print $2}\'`.split.join(' |')
`rm -rf #{tar_dir}` if File.directory?(tar_dir)
`ls - 1 /local_home`.split. each do |list|
if list.match(/ #{home_user}/)
`mkdir -p #{tar_dir + "/" + list}`
tar_list_path = "#{tar_dir + " / " + list}/tar.list"
tar_file_name = "#{tar_dir + " / " + list}/#{DateTime.now.strftime(" % Y -%m-%d ")}.tar.bz2"
config = import_config(list)
if config[ :filetype ].length > 0
filetype = config[ :filetype ].join( '|' )
else
filetype = "bmp|png|pdf|vsd|rar|log|dat|bak"
end
if config[ :exclude ].length > 0
exclude = config[ :exclude ].join( '|' )
else
exclude = "Code|RTL|INCA.libs"
end
if list.match(/ #{home_user}/)
if config[ :include ].length > 0
config[ :include ]. each do |in_path|
check(in_path, filetype, exclude, config[ :deep ]) if File .directory?(in_path)
end
else
check( "/local_home/" + list, filetype, exclude, config[ :deep ])
end
end
$result << "." + tar_list_path
# write tar_list to tmp file
write_file(tar_list_path, $result )
# tar file
`cd / && tar -jc - T #{tar_list_path} -f #{tar_file_name}`
# scp file
`scp #{tar_file_name} #{bak_ser + ":/bak/auto_bak/" + list + "/" + File.basename(tar_file_name)}`
$result = []
end
`rm -rf #{tar_dir}` if File.directory?(tar_dir)
end |
运行前会加载配置文件 (/local_home/USERNAME/config.txt)
格式如下
include=DIR1;DIR2
exclude=DIR1;DIR2
deep=6
filetype=dat;avi
本文转自 nonono11 51CTO博客,原文链接:http://blog.51cto.com/*/1547295,如需转载请自行联系原作者