Skip to content

Instantly share code, notes, and snippets.

@lucansky
Created May 28, 2011 23:05
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 lucansky/2f81ccb2958dfda0e330 to your computer and use it in GitHub Desktop.
Save lucansky/2f81ccb2958dfda0e330 to your computer and use it in GitHub Desktop.
class Folder < ActiveRecord::Base
belongs_to :node
has_many :records
def run_backup
Net::SSH.start( self.node.host, self.node.user, :port => self.node.port, :timeout => 10 ) do |session|
session.open_channel do |channel|
channel.on_data do |chan,data|
(@buf ||= '') << data
while line = @buf.slice!(/(.*)\r?\n/)
puts line
puts "Size : #{@buf.size}"
file = line.split(" ")
hash = file[0] # SHA224 sum suboru
next if file[1] == nil
path = file[1].chomp # Kompletna cesta k suboru /home/...
if path =~ /(?-mix:[^A-Za-z0-9~\(\)._\-:\!\?\[\]\/\ \{\}\+\,\@\'])|(?-mix:^.+\.((0)|(ztmp)|(dat)|(dem)|(log)|(png)|(htm))$)/
Rails.logger.info "Bad file #{path}"
next
end
Rails.logger.info "#{self.node.name} - #{path}"
cloud_file = CloudFile.find_by_sha224(hash)
if cloud_file.nil?
#FileUtils.mkdir_p("/home/adam/backup/files/#{hash[0..2]}")
#ssh.scp.download!(path, "/home/adam/backup/files/#{hash[0..2]}/#{hash}") rescue $stderr.puts "Stahovanie '#{path}' #{$!}"
#print(".")
Rails.logger.info "UPLOAD : #{path}"
session.exec("cp '#{path}' /tmp/#{hash}; s3cmd put '/tmp/#{hash}' s3://crew-backup;rm '/tmp/#{hash}'") do |ch, stream, data|
if stream == :stderr
Rails.logger.info "ERROR : #{data}"
next
else
Rails.logger.info "s3cmd : #{data}"
end
end
cloud_file = CloudFile.create(:sha224 => hash)
end
end # While
end
channel.on_extended_data { |chan,type,output| print output }
channel.exec("find #{self.path} -type f -not \\( -name '*.log' -o -name '*.dat' \
-o -name '*.0' -o -name '*.ztmp' -o -name '*.dem' -o -name '*.htm' -o -name '*.png' \\) ! \
-type l -size -250000k -exec sha224sum '{}' \\;")
end
session.loop
# ssh.exec("find #{folder.path} -type f -not \\( -name '*.log' -o -name '*.dat' -o -name '*.0' -o -name '*.ztmp' -o -name '*.dem' -o -name '*.htm' -o -name '*.png' \\) ! -type l -size -250000k -exec sha224sum '{}' \\;") do|ch, stream, data|
# lines = data.split(/\n/)
# last_char = data[-1]
# count = lines.size
# lines.each_with_index do |l,i|
# valid_file = false
# unless $partial.nil? || $partial.empty?
# line = "#{$partial}#{l}"
# valid_file = true
# $partial = ""
# else
# unless count == i+1 # Pokial nieje na konci
# line = l
# valid_file = true
# else # Pokial je na konci
# if last_char == 10
# line = l
# valid_file = true
# else
# $partial = l
# valid_file = false
# end
# end
# end
end # end ssh
end # end def
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment