Skip to content

Instantly share code, notes, and snippets.

@voidet
Created December 13, 2012 11:51
Show Gist options
  • Save voidet/4275935 to your computer and use it in GitHub Desktop.
Save voidet/4275935 to your computer and use it in GitHub Desktop.
Syncd Just a script I knocked up for a friend. Use case for it was they had a small HDD they wished to sync to a larger data set. They would then delete the files gradually and only sync newer files, or files that they previously had no space to sync with. Basically this is a system that remembers previously deleted files and makes sure they don…
require 'fileutils'
require 'pathname'
require 'digest'
require 'colorize'
class Syncd
def initialize
@dir = '/Volumes/MUSIC'
@remote = '/Users/richard/mew'
@currentDir = @dir
@crumbName = '.syncd'
end
def sync(dir = nil)
dir = @dir unless dir
Dir.foreach(dir) { |f|
path = dir + '/' + f
next if (f == '..' || f == '.')
if File.directory?(path)
self.sync(path)
else
self.syncFile(path)
end
}
end
@private
def previouslySyncd(file)
filename = File.basename(file)
syncdCrumb = File.dirname(file) + "/#{@crumbName}"
if !File.exists?(syncdCrumb)
FileUtils.touch(syncdCrumb)
end
if File.readlines(syncdCrumb).grep(filename).size == 0
open(syncdCrumb, 'a') { |f|
f.puts File.basename(file)
}
return false
else
return true
end
end
def fileIsIdentical(file, remoteFile)
return false unless File.exists?(remoteFile)
return FileUtils.compare_file(file, remoteFile)
end
def syncFile(file)
remoteFile = @remote + file[@dir.size..file.size]
print "Syncing #{file}... ".light_green
if !previouslySyncd(file) && !fileIsIdentical(file, remoteFile)
begin
Dir.mkdir(File.dirname(remoteFile)) unless File.exists?(File.dirname(remoteFile))
FileUtils.cp(file, remoteFile)
puts "Success!".green
rescue Exception => e
puts "Error: #{e}".red
end
else
puts "Skipped, no need to sync".blue
end
end
end
syncd = Syncd.new()
syncd.sync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment