自動備份指令碼

來源:互聯網
上載者:User

標籤:ruby   backup   

自動尋找相關檔案.上傳到伺服器

#!/usr/bin/ruby# coding: utf-8require ‘date‘$result = []bak_ser = "[email protected]"def check(top_dir, ext_type, exclude, dir_deep)  content = Dir.entries(top_dir)  for i in content    i = top_dir + "/" + i    # hidden dir or hidden file     next if File.basename(i)[0] == ‘.‘    if File.directory?(i)      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‘)        check(i, ext_type, exclude, dir_deep)      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  endenddef write_file(path,str)aFile = File.new(path,"w")    aFile.puts straFile.closeenddef delete_dir_line(dir)  dir[0] == ‘/‘ ? dir[1..-1] : direnddef import_config(home_name)  path_in, path_ex, filetype = [], [], []  config = {:deep => 12, :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 => 12, :exclude => path_ex, :filetype => filetype, :include => path_in}  end  path_ex.each { |x| path_ex.delete(x) if path_in.index(x) }  return configend# get home userhome_user = `grep $(hostname) /etc/auto.nfs | awk -F \‘/local_home/\‘ \‘{print $2}\‘`.split.join(‘|‘)`rm -rf /tmp/auto_tar_bak` if File.directory?(‘/tmp/auto_tar_bak‘)`ls -1 /local_home`.split.each do |list|  if list.match(/#{home_user}/)    `mkdir -p /tmp/auto_tar_bak/#{list}`    tar_list_path = "/tmp/auto_tar_bak/#{list}/tar.list"    tar_file_name = "/tmp/auto_tar_bak/#{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])        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 = []  endend

運行前會載入設定檔 (/local_home/USERNAME/config.txt)

格式如下

include=DIR1;DIR2

exclude=DIR1;DIR2

deep=6

filetype=dat;avi

本文出自 “阿邊的雜記” 部落格,請務必保留此出處http://abian.blog.51cto.com/751059/1547295

自動備份指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.