Skip to content

Instantly share code, notes, and snippets.

@arton
Last active December 11, 2015 19:29
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 arton/4648978 to your computer and use it in GitHub Desktop.
Save arton/4648978 to your computer and use it in GitHub Desktop.
#!/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