-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmktape
executable file
·419 lines (360 loc) · 9.37 KB
/
mktape
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env ruby
# The contents of this file are hereby dedicated to the public domain.
# Andrew H. Armenia
# 2015-11-28
require 'digest/sha1'
require 'etc'
SIZE_SUFFIXES = ["bytes", "KiB", "MiB", "GiB", "TiB"]
EXCLUDES = [ "*.xmp", "archive-catalog" ]
def format_size(sz)
divides = 0
while sz >= 1024.0 && divides < SIZE_SUFFIXES.size - 1
sz /= 1024.0
divides += 1
end
"%.2f %s" % [sz, SIZE_SUFFIXES[divides]]
end
class FileStats < Struct.new(:filename, :sha1, :mtime, :size)
end
class VolumeList < Hash
def initialize
super
@tapelist_filename = 'tape-list'
@diff_mode = :name_only
if File.exists?(@tapelist_filename)
read_file
else
STDERR.puts <<EOF
There is currently no tape list file in this directory.
If you're sure you're working in the right directory, and no tapes have
been made from it yet, run 'touch #{@tapelist_filename}' to create a
new, empty tape list file.
EOF
fail "no tape list found"
end
end
# load and save volume list from file
def read_file
clear
tape_id = nil
IO.readlines(@tapelist_filename).each do |line|
if (line =~ /^TAPE (.*)$/)
# start new tape
tape_id=$1
else
sha1 = nil
mtime = nil
size = nil
# parse extended file data out of line
if (line =~ /sha1:([0-9a-fA-F]{40})/)
line = $` + $'
sha1 = $1
end
if (line =~ /mtime:(\d+)/)
line = $` + $'
mtime = Time.at($1.to_i)
end
if (line =~ /size:(\d+)/)
line = $` + $'
size = $1.to_i
end
if (line =~ /^\s(.*)$/)
# add this to the file list for this tape
self[tape_id] ||= []
data = FileStats.new
data.filename = $1.strip
data.mtime = mtime
data.sha1 = sha1
data.size = size
self[tape_id] << data
else
fail "corrupt tape list"
end
end
end
end
def write_file
File.rename(@tapelist_filename, @tapelist_filename + ".bak")
File.open(@tapelist_filename, 'w') do |tapelist|
each_pair do |tape, files|
tapelist.puts "TAPE #{tape}"
files.sort_by{ |fs| fs.filename }.each do |fs|
tapelist.write " #{fs.filename.strip}"
if fs.sha1
tapelist.write " sha1:#{fs.sha1}"
end
if fs.mtime
tapelist.write " mtime:#{fs.mtime.to_i}"
end
if fs.size
tapelist.write " size:#{fs.size}"
end
tapelist.write "\n"
end
end
end
end
def tape_exists?(tape)
has_key?(tape)
end
def check_mtime
@diff_mode = :mtime
end
def check_sha1
@diff_mode = :sha1
end
def archived_files
if @diff_mode == :name_only
# this works if we only care about filenames
values.flatten
elsif @diff_mode == :mtime
# if we care about mtimes, then we need to reject
# files where the mtime on disk is different from
# the mtime in the tape list
values.flatten.select do |f|
if f.mtime.nil?
# assume files with unknown mtime have
# not been backed up yet
false
elsif File.exists?(f.filename)
# ruby seems to do something dumb here,
# so convert the timestamps to integers first
f.mtime.to_i == File.mtime(f.filename).to_i
else
false # file is not archived if it does not exist
end
end
elsif @diff_mode == :sha1
# if we care about sha1s, then we must reject files
# where the sha1 of the file on disk is different
# from the sha1 in the tape list.
values.flatten.select do |f|
if f.sha1.nil?
# assume files with unknown sha1
# have not been backed up yet
false
elsif File.exists?(f.filename)
f.sha1 == Digest::SHA1.file(f.filename).hexdigest
else
false
end
end
else
fail "don't recognize that diff mode, bailing out"
end
end
def archived_files_names
archived_files.map { |f| f.filename }
end
def unarchived_files
dir_files - archived_files_names
end
def excluded(fn)
bn = File.basename(fn)
if fn == @tapelist_filename
true
elsif fn == @tapelist_filename + '.bak'
true
elsif EXCLUDES.any? { |exclude| File.fnmatch(exclude, bn) }
true
else
false
end
end
def dir_files
Dir.new(".").select do |fn|
File.file?(fn) && !excluded(fn)
end.sort
end
def next_tapeid
if self.size == 0
"TAPE_000"
else
self.keys.sort.last.succ
end
end
# check the files to determine if they may have been modified
def check_files(files)
ok = true
files.each do |fs|
puts "checking: #{fs.filename}"
current_mtime = File.mtime(fs.filename)
current_sha1 = Digest::SHA1.file(fs.filename).hexdigest
if current_mtime != fs.mtime or current_sha1 != fs.sha1
ok = false
break
end
end
ok
end
# add extended data (hash, mtime) for files that don't have it
def upgrade
each_pair do |tape, files|
files.each do |file|
if not File.exists?(file.filename)
puts "warning: #{file.filename} no longer exists, cannot hash"
next
end
if file.sha1.nil?
file.sha1 = Digest::SHA1.file(file.filename).hexdigest
end
if file.mtime.nil?
file.mtime = File.mtime(file.filename)
end
if file.size.nil?
file.size = File.size(file.filename)
end
p file
write_file # so we don't lose all hashing work if interrupted
end
end
end
# add a tape to the tape list and return the list of files that
# should go on the tape. This list of files is returned so that
# the actual process of writing the files to tape may be initiated
def mktape(tape_id, tape_size, opts = {}, new_file_delay)
fail if tape_exists?(tape_id)
files = []
tape_remaining = tape_size
unarchived_files.each do |f|
# don't grab anything but size just yet
fs = FileStats.new
fs.size = File.size(f)
fs.filename = f
if (fs.size > tape_size)
STDERR.puts "warning: file #{f} too large for single tape, skipping"
elsif (Time.now.to_i - File.mtime(f).to_i < new_file_delay.to_r * 3600)
STDERR.puts "warning: file #{f} has a modified time less than #{new_file_delay} hours ago, skipping"
elsif (fs.size < tape_remaining)
fs.mtime = File.mtime(f)
if opts[:hash] or @diff_mode == :sha1
# compute hash of the file
fs.sha1 = Digest::SHA1.file(f).hexdigest
end
files << fs
tape_remaining = tape_remaining - fs.size
gb = format "%.2f", tape_remaining/1000000000.0
end
end
if files.length > 0
self[tape_id] = files
puts("remaining tape: #{format_size(tape_remaining)}")
end
files
end
# Find the most recent tape containing a file with the given name.
def find_tape_for_file(filename)
tapes = self.keys.sort.reverse!
tapes.each do |tape_id|
tape_files = self[tape_id]
tape_files.each do |file_stats|
if file_stats.filename == filename
return tape_id
end
end
end
nil
end
end
# this is set to 399GB for LTO3 tapes so we don't go over
TAPE_SIZE = 799*1000*1000*1000
TAPE_DEVICE = '/dev/st0'
def mktape(tapeid, check_hash, new_file_delay)
unless tapeid =~ /^[A-Za-z0-9_]+$/
fail "invalid tape id specified on command line"
end
vl = VolumeList.new
if vl.tape_exists?(tapeid)
fail "that tape ID is already in use"
end
if (check_hash)
vl.check_sha1
else
vl.check_mtime
end
files = vl.mktape(tapeid, TAPE_SIZE, :hash => true, new_file_delay => new_file_delay)
if files.size == 0
fail "there are no files to be written to tape"
end
# we get a list of FileStats structures, turn it into a list of filenames
filenames = files.map { |f| f.filename }
begin
STDERR.puts "writing tape..."
system('tar', '--rsh-command=/usr/bin/ssh', '-b', '1024', '-cvf', TAPE_DEVICE, *filenames) or fail "write failed with status #{$?}"
STDERR.puts "verifying tape..."
system('tar', '--rsh-command=/usr/bin/ssh', '-b', '1024', '-dvf', TAPE_DEVICE, *filenames) or fail "verify failed with status #{$?}"
vl.check_files(files) or fail "files changed while we were writing them"
vl.write_file
rescue RuntimeError => e
STDERR.puts "failed to write tape #{tapeid}: #{e.message}"
end
# eject the tape
system('mt', '--rsh-command=/usr/bin/ssh', '-f', TAPE_DEVICE, 'offline')
end
def report_status(new_file_delay)
vl = VolumeList.new
vl.check_mtime
n_tapes = 0
desired_uid = Etc.getpwnam("rpitv-sys").uid
loop do
tapeid = vl.next_tapeid
files = vl.mktape(vl.next_tapeid, TAPE_SIZE, new_file_delay)
if files.size > 0
puts tapeid
files.each do |fs|
<<<<<<< HEAD
puts "\t#{fs.filename} #{format_size(fs.size)}"
=======
s = File.stat(fs.filename)
if s.uid != desired_uid
puts "\t(!)#{fs.filename}"
else
puts "\t#{fs.filename}"
end
>>>>>>> refs/remotes/origin/master
end
else
break
end
end
end
def list_archived
puts "FILES"
vl = VolumeList.new
vl.check_mtime
vl.archived_files_names.each { |f| puts f }
end
check_hash = ARGV.delete('-h')
no_act = ARGV.delete('-n')
upgrade = ARGV.delete('-u')
find_tapes = ARGV.delete('-f')
ls = ARGV.delete('-l')
new_file_delay = ARGV.delete('-t')
if (new_file_delay == '-t')
new_file_delay = ARGV.shift
else
new_file_delay = 0
end
if (upgrade)
puts "upgrading tape list, this may take a very long time"
vl = VolumeList.new
vl.upgrade
elsif (ls)
list_archived
elsif (no_act)
report_status(new_file_delay)
elsif (find_tapes)
# mktape -f file [file] [file]
# loads the tape list from the current working directory and finds the
# tapes containing the given files.
# prints a list of lines in the format:
# TAPE FILE
vl = VolumeList.new
ARGV.each do |file|
tape = vl.find_tape_for_file(file)
puts "#{tape} #{file}"
end
else
tapeid = ARGV[0]
mktape(tapeid, check_hash, new_file_delay)
end