Class: Rsync

Inherits:
CliFuncs show all
Defined in:
lib/cli_funcs_rsync.rb

Instance Attribute Summary collapse

Attributes inherited from CliFuncs

#ignore_bad_exit

Instance Method Summary collapse

Methods inherited from CliFuncs

#clear_values, #flag_add, #flags_run, #run_and_capture, #set_dirs

Constructor Details

#initializeRsync

Returns a new instance of Rsync.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cli_funcs_rsync.rb', line 8

def initialize
  super
  @utility = CliUtils.new("rsync").utility_path
  @uptodate = Array.new
  @deleted = Array.new
  @modified = Array.new
  @created = Array.new
  @excluded = Hash.new
  @ignored = Array.new
  @duplicates = Array.new
  @transfer_stats = Hash.new
  @source = Array.new
  @destination = String
  @output_filter_junk = Regexp
  @output_filter_excluded = Regexp
  @output_filter_warn_err = Regexp
  @output_filter_stats = Regexp
  @output_filter_duplicates = Regexp
  @output_filter_created = Regexp
  set_flags_base
  set_output_filters
end

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def base_dir
  @base_dir
end

#cmd_runObject

Returns the value of attribute cmd_run.



5
6
7
# File 'lib/cli_funcs_rsync.rb', line 5

def cmd_run
  @cmd_run
end

#createdObject (readonly)

Returns the value of attribute created.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def created
  @created
end

#data_dirObject (readonly)

Returns the value of attribute data_dir.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def data_dir
  @data_dir
end

#deletedObject (readonly)

Returns the value of attribute deleted.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def deleted
  @deleted
end

#destinationObject

Returns the value of attribute destination.



5
6
7
# File 'lib/cli_funcs_rsync.rb', line 5

def destination
  @destination
end

#duplicatesObject (readonly)

Returns the value of attribute duplicates.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def duplicates
  @duplicates
end

#excludedObject (readonly)

Returns the value of attribute excluded.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def excluded
  @excluded
end

#flags_allObject

Returns the value of attribute flags_all.



5
6
7
# File 'lib/cli_funcs_rsync.rb', line 5

def flags_all
  @flags_all
end

#ignoredObject (readonly)

Returns the value of attribute ignored.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def ignored
  @ignored
end

#modifiedObject (readonly)

Returns the value of attribute modified.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def modified
  @modified
end

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def output
  @output
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/cli_funcs_rsync.rb', line 5

def source
  @source
end

#transfer_statsObject (readonly)

Returns the value of attribute transfer_stats.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def transfer_stats
  @transfer_stats
end

#uptodateObject (readonly)

Returns the value of attribute uptodate.



6
7
8
# File 'lib/cli_funcs_rsync.rb', line 6

def uptodate
  @uptodate
end

Instance Method Details

#flag_archiveObject



316
317
318
# File 'lib/cli_funcs_rsync.rb', line 316

def flag_archive
  flag_add("-a")
end

#flag_bwlimit(kbps) ⇒ Object



332
333
334
# File 'lib/cli_funcs_rsync.rb', line 332

def flag_bwlimit(kbps)
  flag_add("--bwlimit=#{kbps}")
end

#flag_checksumObject



324
325
326
# File 'lib/cli_funcs_rsync.rb', line 324

def flag_checksum
  flag_add("-c")
end

#flag_compressObject



304
305
306
# File 'lib/cli_funcs_rsync.rb', line 304

def flag_compress
  flag_add("-z")
end

#flag_deleteObject



300
301
302
# File 'lib/cli_funcs_rsync.rb', line 300

def flag_delete
  flag_add("--delete")
end

#flag_dryrunObject



308
309
310
# File 'lib/cli_funcs_rsync.rb', line 308

def flag_dryrun 
  flag_add("-n")
end

#flag_exclude(pattern) ⇒ Object



340
341
342
# File 'lib/cli_funcs_rsync.rb', line 340

def flag_exclude(pattern)
  flag_add("--exclude=#{pattern[1..-1]}")
end

#flag_itemizedObject



320
321
322
# File 'lib/cli_funcs_rsync.rb', line 320

def flag_itemized
  flag_add("-i")
end

#flag_rsync_path(path) ⇒ Object



336
337
338
# File 'lib/cli_funcs_rsync.rb', line 336

def flag_rsync_path(path)
  flag_add("--rsync-path=#{path}")
end

#flag_statsObject



328
329
330
# File 'lib/cli_funcs_rsync.rb', line 328

def flag_stats
  flag_add("--stats")
end

#flag_verboseObject



312
313
314
# File 'lib/cli_funcs_rsync.rb', line 312

def flag_verbose
  flag_add("-vv")
end

#output_processObject



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
# File 'lib/cli_funcs_rsync.rb', line 44

def output_process
  @output.each do |line|
    if line.match @output_filter_junk
      next
    elsif line.match @output_filter_duplicates
      puts "#{line.chomp} DUPLICATE!" if DEBUG
      @duplicates.push(line)
      next
    elsif line.match @output_filter_excluded
      puts "#{line.chomp} EXCLUDED!" if DEBUG
      @excluded[$3] = $4
      next
    elsif line.match @output_filter_warn_err
      # Capture warnings / errors here
      next
    elsif line.match @output_filter_stats
      # Set hash of stats
      @transfer_stats[$1] = $2
      @transfer_stats[$3] = $4
      @transfer_stats[$5] = $6
      @transfer_stats[$7] = $8
      @transfer_stats[$9] = $10
      @transfer_stats[$11] = $12
      @transfer_stats[$13] = $14
      @transfer_stats[$15] = $16
      @transfer_stats[$17] = $18
      @transfer_stats[$19] = $20
      @transfer_stats[$21] = $22
      next
    elsif line.match @output_filter_created
      #@created.push(line)
      next
    else
      # catch all, this is the main content
      process_itemized(line)
    end
  end
  puts @transfer_stats.inspect if DEBUG
end

#process_itemized(line) ⇒ Object



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
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
# File 'lib/cli_funcs_rsync.rb', line 84

def process_itemized(line)
  # Break apart the line by spaces (e.g. ".f          9/file9")
  attrs,item = line.split(/\s+/, 2)
  # Break apart itemized attrs on each character 0 = . 1 = f 2 = nil, 3 = nil ...
  attrs_p = attrs.split("")
  # Begin check's for file/directory disposition according to rsync
  # If element 0 contains a '.', it means no update has occurred, but may have attribute changes
  if(attrs_p[0] == ".")
     # This first check ignores directories which have had their timestamp changed
     if(
        attrs_p[1] == "d" and 
        attrs_p[2] == "." and
        attrs_p[3] == "." and
        attrs_p[4] == "t" and
        attrs_p[5] == "." and
        attrs_p[6] == "." and
        attrs_p[7] == "." and
        attrs_p[8] == "." and
        attrs_p[9] == "." and
        attrs_p[10] == "."
     )
        puts "#{line.chomp} IGNORED!" if DEBUG
        @ignored.push(line)
        #return
     # checks if nothing has changed with this item
     elsif(
        attrs_p[1] =~ /f|d|L|D|S/ and 
        attrs_p[2] == nil and
        attrs_p[3] == nil and
        attrs_p[4] == nil and
        attrs_p[5] == nil and
        attrs_p[6] == nil and
        attrs_p[7] == nil and
        attrs_p[8] == nil and
        attrs_p[9] == nil and
        attrs_p[10] == nil
     )
        puts "#{line.chomp} UPTODATE!" if DEBUG
        @uptodate.push(line)
     # something must have changed, like an attribute (e.g. ownership or mode)
     else
        puts "#{line.chomp} MODIFIED OWNERSHIP OR MODE!" if DEBUG
        @modified.push(line) 
     end
  elsif(attrs_p[0] =~ /\*|<|>|c|h/)
    # checks if item is being deleted
    if(
       attrs_p[1] == "d" and 
       attrs_p[2] == "e" and
       attrs_p[3] == "l" and
       attrs_p[4] == "e" and
       attrs_p[5] == "t" and
       attrs_p[6] == "i" and
       attrs_p[7] == "n" and
       attrs_p[8] == "g" and
       attrs_p[9] == nil and
       attrs_p[10] == nil
    )
       puts "#{line.chomp} DELETED!" if DEBUG
       @deleted.push(line) 

    # checks if item is being created (i.e. new file/dir)
    elsif(
      attrs_p[1] =~ /f|d/ and 
      attrs_p[2] == "+" and
      attrs_p[3] == "+" and
      attrs_p[4] == "+" and
      attrs_p[5] == "+" and
      attrs_p[6] == "+" and
      attrs_p[7] == "+" and
      attrs_p[8] == "+" and
      attrs_p[9] == "+" and
      attrs_p[10] == "+"
    )
      puts "#{line.chomp} CREATED!" if DEBUG
      @created.push(line)

    # everthing else is considered a modification
    else
      puts "#{line.chomp} MODIFIED CATCH ALL 1!" if DEBUG
      @modified.push(line) 
    end
  else
    puts "#{line.chomp} MODIFIED CATCH ALL 2!" if DEBUG
    @modified.push(line) 
  end
end

#rsyncObject



31
32
33
# File 'lib/cli_funcs_rsync.rb', line 31

def rsync
  run_and_capture(cmd_run)
end

#set_flags_baseObject



344
345
346
347
348
349
350
# File 'lib/cli_funcs_rsync.rb', line 344

def set_flags_base
  flag_archive
  flag_verbose
  flag_itemized
  flag_delete
  flag_stats
end

#set_output_filter_createdObject



185
186
187
188
189
190
191
192
193
194
# File 'lib/cli_funcs_rsync.rb', line 185

def set_output_filter_created
  filter = Array.new

  # These should be rare, for some reason it doesn't show up in the itemized list
  # and rsync doesn't count it as a 'created file', it seems like the only time 
  # this happens is if the destination root directory doesn't already exist.
  filter.push("^created directory .*")

  @output_filter_created = /#{filter[0]}/
end

#set_output_filter_duplicatesObject



220
221
222
223
224
225
226
227
# File 'lib/cli_funcs_rsync.rb', line 220

def set_output_filter_duplicates
  filter = Array.new

  # These are file/directories which are the same in the sources list
  filter.push("^removing duplicate name .* from file list .*")

  @output_filter_duplicates = /#{filter[0]}/
end

#set_output_filter_excludedObject



215
216
217
218
# File 'lib/cli_funcs_rsync.rb', line 215

def set_output_filter_excluded
  # These are files/directories which have been excluded by a pattern we passed to rsync
  @output_filter_excluded = /^\[generator\] (excluding|protecting) (file|directory) (.*) because of pattern (.*)$/
end

#set_output_filter_junkObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/cli_funcs_rsync.rb', line 229

def set_output_filter_junk
  filter = Array.new

  # blank line
  filter.push("^$")
  # ignore line
  filter.push("^sending incremental file list")
  # ignore line
  filter.push("^building file list ...")
  # ignore line
  filter.push("^expand file_list\s\w+")
  # ignore line
  filter.push("^rsync: expand\s\w+")
  # ignore line
  filter.push("^opening connection\s\w+")
  # ignore line - should probably capture this info
  filter.push("^total")
  # ignore line - should probably capture this info
  filter.push("^wrote")
  # ignore line - should probably capture this info
  filter.push("^sent")
  # ignore line
  filter.push("^done")
  # ignore line
  filter.push("^excluding")
  # ignore line
  filter.push("^hiding")
  # ignore line
  filter.push("^delta( |-)transmission (dis|en)abled")
  # ignore line
  filter.push("^deleting in \./")
  # ignore line
  filter.push("^opening connection using: ssh")
  # ignore line
  filter.push("^rsync\\[\\d+\\] \\(sender\\) heap statistics:")
  # ignore line
  filter.push("^rsync\\[\\d+\\] \\(server receiver\\) heap statistics:")
  # ignore line
  filter.push("^rsync\\[\\d+\\] \\(server generator\\) heap statistics:")
  # ignore line
  filter.push("^  arena:")
  # ignore line
  filter.push("^  ordblks:")
  # ignore line
  filter.push("^  smblks:")
  # ignore line
  filter.push("^  hblks:")
  # ignore line
  filter.push("^  hblkhd:")
  # ignore line
  filter.push("^  allmem:")
  # ignore line
  filter.push("^  usmblks:")
  # ignore line
  filter.push("^  fsmblks:")
  # ignore line
  filter.push("^  uordblks:")
  # ignore line
  filter.push("^  fordblks:")
  # ignore line
  filter.push("^  keepcost:")

  @output_filter_junk = /#{filter.join("|")}/
end

#set_output_filter_statsObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/cli_funcs_rsync.rb', line 196

def set_output_filter_stats
  filter = Array.new

  # These are for rsync stats which are output after the transfer is complete
  filter.push("(Number of files): (\\d+)")
  filter.push("(Number of files transferred): (\\d+)")
  filter.push("(Total file size): (\\d+) bytes")
  filter.push("(Total transferred file size): (\\d+) bytes")
  filter.push("(Literal data): (\\d+) bytes")
  filter.push("(Matched data): (\\d+) bytes")
  filter.push("(File list size): (\\d+)")
  filter.push("(File list generation time): (.*) seconds")
  filter.push("(File list transfer time): (.*) seconds")
  filter.push("(Total bytes sent): (\\d+)")
  filter.push("(Total bytes received): (\\d+)")

  @output_filter_stats = /#{filter.join("|")}/
end

#set_output_filter_warn_errObject



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cli_funcs_rsync.rb', line 172

def set_output_filter_warn_err
  filter = Array.new

  # These are warnings and errors kicked out by rsync during it's run
  filter.push("WARNING: .* failed verification -- update discarded \(will try again\)\.")
  filter.push("IO error encountered -- skipping file deletion")
  filter.push("file has vanished: .*")
  filter.push("rsync (error|warning): .*")
  filter.push("cannot delete non-empty directory: .*")

  @output_filter_warn_err = /#{filter.join("|")}/
end

#set_output_filtersObject



35
36
37
38
39
40
41
42
# File 'lib/cli_funcs_rsync.rb', line 35

def set_output_filters
  set_output_filter_junk
  set_output_filter_excluded
  set_output_filter_warn_err
  set_output_filter_stats
  set_output_filter_duplicates
  set_output_filter_created
end