Skip to content

Instantly share code, notes, and snippets.

@AlexKalinin
Last active February 15, 2023 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexKalinin/91f53fc0aa7ef15c57ea0460be26b579 to your computer and use it in GitHub Desktop.
Save AlexKalinin/91f53fc0aa7ef15c57ea0460be26b579 to your computer and use it in GitHub Desktop.
Checking that the folder was continuesly mounted while the long operation was being performed.

Checking that the folder was continuesly mounted while the long operation was being performed.

Why?

I have external hard-drive where I store backups created by MacOS TimeMachine. This hard drive is connected by usb dock-station, which is not protected from power failure. I just wanted to be sure, that, first TimeMachine backup (which is quite a lengthy operation) was not interrapted.

Usage:

# assume that you already have ruby on your computer
# if no, go to https://rvm.io/

# clone this mini-project
git clone git@gist.github.com:91f53fc0aa7ef15c57ea0460be26b579.git folder_check
cd folder_check

# edit and run your checks
ruby run.rb

# run some long operation and go calmly about your business

# stop by Ctrl + C

# run check for time gaps (did the folder was contnuesly mounted?)
ruby check.rb
require 'json'
require 'time'
last_idx = 0
prev_time = nil
File.open('./log.json', 'r').each_line.with_index do |line, idx|
next if line.strip == ''
parsed_line = JSON.parse(line)
if parsed_line['status'] != 'ok'
puts "#{idx}: ERR: not_ok: #{line}"
end
current_time = Time.parse(parsed_line['time'])
if prev_time.nil?
puts "#{idx}: ERR: prev_time_is_nil: #{line}" if last_idx != 0
else
if (current_time - prev_time).to_i > 9
puts "#{idx}: ERR: time-gap: #{line}"
end
end
prev_time = current_time
last_idx = idx
end
puts "Processed #{last_idx} lines."
require 'json'
def perform_check
res = %x[ mount |grep '/dev/disk4s2 on /Volumes/Tosh6TbTimeMach' ]
raise 'mount_point_not_exists!' if res == ''
res = 'ok_found_mountpoint'
# 1 / 0
end
def perform_check2
`ls -l "/Volumes/Untitled 2"`.split("\n")[1].end_with?('001.mp4')
end
loop do
msg = {
time: Time.now
}
begin
msg[:check_payload] = perform_check
msg[:check2_payload] = perform_check2
msg[:status] = :ok
rescue Exception => ex
msg[:status] = :error
msg[:ex_class] = ex.class.to_s
msg[:ex_message] = ex.message
end
File.open('log.json', 'a'){ |f| f.puts msg.to_json }
sleep 8
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment