Instantly share code, notes, and snippets.

@arton /diffcp
Last active Dec 11, 2015

Embed
What would you like to do?
#!/Users/local/bin/ruby
# coding: utf-8
require 'fileutils'
$user = 'user'
$pwd = 'pwd'
$server = 'nas-hostname'
module MusicBackup
def self.to_path(d, f)
d + File::SEPARATOR + f
end
def self.cmpfile(src, dst)
s = File::Stat.new(src)
d = File::Stat.new(dst)
return s.mtime == d.mtime && s.size == d.size
end
def self.recp(src, dst)
puts "start #{src}" if $DEBUG
org = Dir.pwd
Dir.chdir(src)
as = Dir.foreach(src).to_a.sort
ad = Dir.foreach(dst).to_a.sort
add = as - ad
del = ad - as
dup = as - add
dup.each do |x|
next if x[0] == '.'
if FileTest.directory?(x)
unless FileTest.directory?(to_path(dst, x))
FileUtils.rm_f to_path(dst, x)
FileUtils.mkdir to_path(dst, x)
end
recp(to_path(src, x), to_path(dst, x))
elsif !cmpfile(x, to_path(dst, x))
if FileTest.directory?(to_path(dst, x))
FileUtils.rm_rf to_path(dst,x)
end
FileUtils.cp(x, dst, :preserve => true)
end
end
add.each do |x|
next if x[0] == '.'
FileUtils.cp_r(x, dst, :preserve => true)
end
del.each do |x|
next if x[0] == '.'
FileUtils.rm_rf to_path(dst, x)
end
Dir.chdir(org)
end
end
unless File.exist?('/Volumes/tmp')
FileUtils.mkdir '/Volumes/tmp'
system("mount_afp afp://#{$user}:#{$pwd}@#{$server}/media /Volumes/tmp")
end
src = "/Users/#{$user}/Music/iTunes"
dst = '/Volumes/tmp/Music/iTunes'
MusicBackup.recp(src, dst)
system('umount /Volumes/tmp/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment