自动备份脚本

简介:

自动查找相关文件.上传到服务器

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/abian/1547295,如需转载请自行联系原作者

相关文章
|
机器学习/深度学习 算法 决策智能
智能解决装箱问题:使用优化算法实现高效包装
装箱问题(Bin Packing Problem)是组合优化领域中的一个经典问题,主要涉及如何将一系列对象高效地装入有限数量的容器(或“箱”)中,同时满足特定的约束条件。这个问题的目标是最小化所需使用的箱子数量或者最大化箱子的装载效率,以减少空间或资源的浪费。
|
API 开发者
1688店铺订单接口指南攻略
1688店铺订单接口用于获取订单列表、查询订单详情及操作订单等。首先需注册为1688开放平台开发者,创建应用获取App Key与App Secret。使用API获取订单列表时,要申请订单查询权限,并通过HTTP请求传递参数(如seller_id、page、page_size和access_token)调用接口。此功能适用于管理1688店铺订单数据。
|
10月前
|
数据采集 存储 运维
2025年主流接口监控平台对比分析与最佳实践指南
文章围绕2025年接口监控平台展开,对比商业SaaS、开源自建、全栈整合型三种主流方案。分析行业背景、痛点,阐述各方案优劣及适用场景,介绍技术原理与最佳实践路径,还解答常见选型问题,助力企业根据自身情况选择合适方案,保障接口稳定。
|
10月前
|
数据采集 机器学习/深度学习 人工智能
未来趋势:AI 时代下 python 爬虫技术的发展方向
未来趋势:AI 时代下 python 爬虫技术的发展方向
|
10月前
|
JavaScript 前端开发 Shell
技术实践:快速批量下载网站图片资源实现方法
本文介绍了三种批量下载图片的方法:使用HTML页面、Node.js脚本和Bash结合Wget命令。重点推荐第三种,仅需几行命令即可高效完成下载,无需编程环境,兼容性强,适合快速批量获取网络图片资源。
1405 0
|
文字识别 算法 小程序
【项目总结】快瞳医疗化验单的OCR识别
快瞳科技通过图像识别技术,成功解决了医疗化验单OCR识别难题。项目要求精准识别表格内容,尤其是化验数值和名称,准确率达85%以上。针对化验单来源多样、干扰因素多的问题,团队采用智能文档抽取模型、opencv技术(如霍夫变换)进行图片扶正与裁剪,优化识别精度。最终,项目不仅达到药企要求,还实现超越,为医疗行业智能化转型提供了高性价比解决方案,助力快瞳科技在医疗信息化领域树立良好口碑。
1274 43
|
存储 人工智能 Serverless
10 分钟打造你的专属 AI 客服
10 分钟打造你的专属 AI 客服
927 9
|
网络协议 Unix 应用服务中间件
|
JSON 计算机视觉 数据格式
LangChain-17 FunctionCalling 利用大模型对函数进行回调 扩展大模型的额外的能力 比如实现加减乘除等功能
LangChain-17 FunctionCalling 利用大模型对函数进行回调 扩展大模型的额外的能力 比如实现加减乘除等功能
713 4
|
存储 固态存储 Go
M.2移动硬盘打造Win To Go系统:高效分区存储文件全攻略
【10月更文挑战第11天】Win To Go 是一种将 Windows 系统安装在 M.2 移动硬盘上的技术,便于用户携带自定义系统跨设备使用。需准备高性能 M.2 硬盘及合适硬盘盒,并使用 DiskGenius 或 Rufus 进行分区与系统安装。系统分区用于安装 Windows,其余分区可根据需求存储工作或娱乐文件,便于管理和备份。
2507 2

热门文章

最新文章