Class: Remotebackup::BackupInfo
- Inherits:
-
Object
- Object
- Remotebackup::BackupInfo
- Defined in:
- lib/remotebackup.rb
Instance Attribute Summary collapse
-
#file_map ⇒ Object
Returns the value of attribute file_map.
-
#last_file ⇒ Object
Returns the value of attribute last_file.
-
#mod ⇒ Object
Returns the value of attribute mod.
Instance Method Summary collapse
- #cleanFileInfo ⇒ Object
- #date_to_filename ⇒ Object
- #differencial_copy(out_dir) ⇒ Object
- #ignore_check(target) ⇒ Object
-
#initialize(args) ⇒ BackupInfo
constructor
A new instance of BackupInfo.
- #load_last_yaml(output_dir) ⇒ Object
- #make_file_map_list(session, target) ⇒ Object
- #makeFileMap(session, target, lines) ⇒ Object
- #outputYaml(out_dir) ⇒ Object
- #zeropadd(i) ⇒ Object
Constructor Details
#initialize(args) ⇒ BackupInfo
Returns a new instance of BackupInfo.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/remotebackup.rb', line 13 def initialize(args) @mod = false @name = args["name"] || args["server"] @server = args["server"] @user = args["user"] @options = {} @options[:port] = args["port"].to_i if args["port"] @options[:password] = args["password"] if args["password"] @options[:passphrase] = args["passphrase"] if args["passphrase"] if args["key"] @options[:keys] = [File.(args["key"])] end @path = args["path"] @ignore_list = args["ignore_list"] || [] @nowTime = DateTime.now @file_map = {"file" => Hash.new,"symbolic" => Hash.new,"directory"=>Array.new} Net::SSH.start(@server,@user,@options) { |session| make_file_map_list(session,"") } end |
Instance Attribute Details
#file_map ⇒ Object
Returns the value of attribute file_map.
12 13 14 |
# File 'lib/remotebackup.rb', line 12 def file_map @file_map end |
#last_file ⇒ Object
Returns the value of attribute last_file.
12 13 14 |
# File 'lib/remotebackup.rb', line 12 def last_file @last_file end |
#mod ⇒ Object
Returns the value of attribute mod.
12 13 14 |
# File 'lib/remotebackup.rb', line 12 def mod @mod end |
Instance Method Details
#cleanFileInfo ⇒ Object
111 112 113 |
# File 'lib/remotebackup.rb', line 111 def cleanFileInfo() FileUtils.touch([@last_file]) end |
#date_to_filename ⇒ Object
184 185 186 |
# File 'lib/remotebackup.rb', line 184 def date_to_filename() @nowTime.year.to_s + "_" + zeropadd(@nowTime.month) + "_" + zeropadd(@nowTime.day) + "_" + zeropadd(@nowTime.hour) + "_" + zeropadd(@nowTime.min) end |
#differencial_copy(out_dir) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/remotebackup.rb', line 128 def differencial_copy(out_dir) output_dir = out_dir + "/" + @name FileUtils.mkdir_p(output_dir) old_file_info_map = load_last_yaml(output_dir) @file_map["directory"].each do |dir| unless old_file_info_map["directory"].include?(dir) @mod = true msg_out "create directory:#{dir}" FileUtils.mkdir_p(output_dir + "/" + dir) end end old_file_map = old_file_info_map["symbolic"] @file_map["symbolic"].each do |key,val| if !old_file_map[key] || old_file_map[key]["source"] != val["source"] @mod = true if !old_file_map[key] msg_out "create symbolic:#{key}" else msg_out "modified symbolic:#{key}" end end end old_file_map = old_file_info_map["file"] Net::SSH.start(@server, @user,@options) do |ssh| @file_map["file"].each do |key,val| utf8_key = key.dup.force_encoding("utf-8") if key.respond_to?(:force_encoding) if !old_file_map[utf8_key] || old_file_map[utf8_key]["date"] != val["date"] @mod = true file_name = output_dir + "/" + key + date_to_filename orig = "#{@path}/#{key}" msg_out "scp #{orig} #{file_name}" begin ssh.scp.download!(orig,file_name) rescue $log.error("#{orig} can't copy. ignore.") next end val["file_name"] = file_name if !old_file_map[utf8_key] msg_out "create file:#{key}" else msg_out "modified file:#{key}" end else val["file_name"] = old_file_map[utf8_key]["file_name"] end end end end |
#ignore_check(target) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/remotebackup.rb', line 33 def ignore_check(target) @ignore_list.each do |ignore| if target =~ /#{ignore}/ return false end end return true end |
#load_last_yaml(output_dir) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/remotebackup.rb', line 114 def load_last_yaml(output_dir) files = Array.new if FileTest.directory?(output_dir) dirs = Dir.glob(output_dir+ "/*.yml") dirs.each do |file| files.push file end end if files.length == 0 return {"file" => Hash.new,"symbolic" => Hash.new,"directory"=>Array.new} end @last_file = files.sort.pop return YAML.load(File.read(@last_file)) end |
#make_file_map_list(session, target) ⇒ Object
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 |
# File 'lib/remotebackup.rb', line 41 def make_file_map_list(session,target) if target != "" target_path = @path +"/"+target else target_path = @path end cmd = "export LANG=C;ls -la #{target_path}" out = "" channel = session.open_channel do |ch| ch.exec cmd do |ch,success| ch.on_data do |c,data| out += data end end end channel.wait if out =~ /No such file/ $log.error("can't open path:#{target_path}") return end if out == "" return end lines = StringIO.new(out).readlines if !Node.build(lines[0].chomp) @file_map["directory"].push target unless target == "" makeFileMap(session,target,lines) else node = Node.build(lines[0].chomp) if node.ftype == :symbolic @file_map["symbolic"][File.basename(@path)] = node.make_map.call elsif node.ftype == :file @file_map["file"][File.basename(@path)] = node.make_map.call end end end |
#makeFileMap(session, target, lines) ⇒ Object
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 |
# File 'lib/remotebackup.rb', line 77 def makeFileMap(session,target,lines) lines.each do |line| node = Node.build(line.chomp) next unless node if not node.name or node.name == "." or node.name == ".." next end if target != "" target_name = target +"/"+ node.name else target_name = node.name end unless ignore_check(target_name) next end case node.ftype when :directory make_file_map_list(session,target_name) when :special next when :file @file_map["file"][target_name] = node.make_map.call when :symbolic @file_map["symbolic"][target_name] = node.make_map.call end end end |
#outputYaml(out_dir) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/remotebackup.rb', line 104 def outputYaml(out_dir) output_dir = out_dir + "/" + @name f = output_dir + "/" + date_to_filename + ".yml" File.open(f,"w") do |f| f.write YAML.dump(@file_map) end end |
#zeropadd(i) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/remotebackup.rb', line 177 def zeropadd(i) if i < 10 "0" + i.to_s else i.to_s end end |