Last active
December 11, 2015 19:29
-
-
Save arton/4648978 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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