Skip to content

Instantly share code, notes, and snippets.

@obelisk68
Last active April 20, 2018 11:13
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 obelisk68/67e3ca178f4b27fdfd59885bfdaf682d to your computer and use it in GitHub Desktop.
Save obelisk68/67e3ca178f4b27fdfd59885bfdaf682d to your computer and use it in GitHub Desktop.
home フォルダのバックアップ
require 'fileutils'
class String
def cut
/.+tomoki\/(.+)$/.match(self)[1]
end
end
def get_file_names(f)
dirs = Dir.glob("*")
dirs += Dir.glob(".*") - [".", ".."] if f
dirs
end
#ファイル名中にひとつでも隠しファイルが入れば @color = true
def handle_color(fname)
color_set = ->{
@color = if /^\./.match(fname)
print "\e[36m"
true
else
false
end
}
color = false
color = color_set.() unless @color
yield
if color
print "\e[97m"
@color = false
end
end
def handle_directory(send, receive, f)
Dir.chdir(send)
get_file_names(f).each do |fname|
handle_color(fname) do
backup(File.join(send, fname), File.join(receive, fname), f)
end
end
Dir.chdir(receive)
get_file_names(f).each do |fname|
handle_color(fname) do
backup(File.join(send, fname), File.join(receive, fname), f)
end
end
end
def backup(send, receive, f)
send_exist = FileTest.exist?(send)
receive_exist = FileTest.exist?(receive)
if !send_exist and receive_exist
FileUtils.rm_rf(receive)
puts "delete: #{receive.cut}"
elsif send_exist and !receive_exist
FileUtils.cp_r(send, receive)
puts "copy: #{receive.cut}"
elsif send_exist and receive_exist
s = File.stat(send ).mtime
r = File.stat(receive).mtime
if s > r or (@not_kakushi_at_top and @color)
if FileTest.directory?(send)
handle_directory(send, receive, f)
else
handle_color(File.basename(send)) do
FileUtils.cp(send, receive)
puts "copy: #{receive.cut}"
end
end
end
else
puts "予期しない入力です: #{send.cut}"
end
rescue
return
end
t = Time.now
f = true #trueならば探索に隠しファイルも含める(隠しディレクトリの中の変更は検知できない)
@color = false
dir = ['/home/tomoki', '/media/tomoki/Transcend/Linux Mint 18 backup/tomoki']
Dir.chdir(dir[0])
get_file_names(f).each do |fname|
send = File.join(dir[0], fname)
receive = File.join(dir[1], fname)
handle_color(fname) do
if FileTest.directory?(send)
if FileTest.exist?(receive)
@not_kakushi_at_top = /^\./.match(fname) ? false : true
#@not_kakushi_at_top = true #コメントアウトすると全隠しファイルを探索
handle_directory(send, receive, f)
else
FileUtils.cp_r(send, receive)
puts "copy: #{receive.cut}"
end
else
backup(send, receive, f)
end
end
end
Dir.chdir(dir[1])
get_file_names(f).each do |fname|
send = File.join(dir[0], fname)
receive = File.join(dir[1], fname)
unless FileTest.exist?(send)
handle_color(File.basename(receive)) do
FileUtils.rm_rf(receive)
puts "delete: #{receive.cut}"
end
end
end
puts "\nMail copy"
FileUtils.cp_r("/home/tomoki/.thunderbird/922vkyux.default",
"/media/tomoki/Transcend/Linux Mint 18 backup/922vkyux.default")
printf("\nRuntime: %.3f [sec]\n", Time.now - t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment