class Astrails::Safe::Ftp

Protected Instance Methods

active?() click to toggle source
# File lib/astrails/safe/ftp.rb, line 7
def active?
  host && user
end
cleanup() click to toggle source
# File lib/astrails/safe/ftp.rb, line 38
def cleanup
  return if $LOCAL || $DRY_RUN

  return unless keep = @config[:keep, :ftp]

  puts "listing files: #{host}:#{base}*" if $_VERBOSE
  if !port
     port = 21
  end
  Net::FTP.open(host) do |ftp|
    ftp.connect(host, port)
    ftp.login(user, password)
    files = ftp.nlst(path)
    pattern = File.basename("#{base}")
    files = files.reject{ |x| !x.start_with?(pattern)}
    puts files.collect {|x| x} if $_VERBOSE

    files = files.
      collect {|x| x }.
      sort

    cleanup_with_limit(files, keep) do |f|
      file = File.join(path, f)
      puts "removing ftp file #{host}:#{file}" if $DRY_RUN || $_VERBOSE
      ftp.delete(file) unless $DRY_RUN || $LOCAL
    end
  end
end
host() click to toggle source
# File lib/astrails/safe/ftp.rb, line 67
def host
  @config[:ftp, :host]
end
password() click to toggle source
# File lib/astrails/safe/ftp.rb, line 75
def password
  @config[:ftp, :password]
end
path() click to toggle source
# File lib/astrails/safe/ftp.rb, line 11
def path
  @path ||= expand(config[:ftp, :path] || config[:local, :path] || ":kind/:id")
end
port() click to toggle source
# File lib/astrails/safe/ftp.rb, line 79
def port
  @config[:ftp, :port]
end
save() click to toggle source
# File lib/astrails/safe/ftp.rb, line 15
def save
  raise RuntimeError, "pipe-streaming not supported for FTP." unless @backup.path

  puts "Uploading #{host}:#{full_path} via FTP" if $_VERBOSE || $DRY_RUN

  unless $DRY_RUN || $LOCAL
    if !port
      port = 21
    end
    Net::FTP.open(host) do |ftp|
      ftp.connect(host, port)
      ftp.login(user, password)
      puts "Sending #{@backup.path} to #{full_path}" if $_VERBOSE
      begin
        ftp.put(@backup.path, full_path)
      rescue Net::FTPPermError
        puts "Ensuring remote path (#{path}) exists" if $_VERBOSE
      end
    end
    puts "...done" if $_VERBOSE
  end
end
user() click to toggle source
# File lib/astrails/safe/ftp.rb, line 71
def user
  @config[:ftp, :user]
end