class Backup::Storage::SFTP

Attributes

ip[RW]

Server IP Address and SFTP port

password[RW]

Server credentials

port[RW]

Server IP Address and SFTP port

ssh_options[RW]

Server credentials

username[RW]

Server credentials

Public Class Methods

new(model, storage_id = nil) click to toggle source
Calls superclass method Backup::Storage::Base::new
# File lib/backup/storage/sftp.rb, line 17
def initialize(model, storage_id = nil)
  super

  @ssh_options ||= {}
  @port        ||= 22
  @path        ||= 'backups'
  path.sub!(/^~\//, '')
end

Private Instance Methods

connection() { |sftp| ... } click to toggle source
# File lib/backup/storage/sftp.rb, line 28
def connection
  Net::SFTP.start(
    ip, username, { :password => password, :port => port }.merge(ssh_options)
  ) {|sftp| yield sftp }
end
create_remote_path(sftp) click to toggle source

Creates (if they don’t exist yet) all the directories on the remote server in order to upload the backup file. Net::SFTP does not support paths to directories that don’t yet exist when creating new directories. Instead, we split the parts up in to an array (for each ‘/’) and loop through that to create the directories one by one. Net::SFTP raises an exception when the directory it’s trying to create already exists, so we have rescue it

# File lib/backup/storage/sftp.rb, line 70
def create_remote_path(sftp)
  path_parts = Array.new
  remote_path.split('/').each do |path_part|
    path_parts << path_part
    begin
      sftp.mkdir!(path_parts.join('/'))
    rescue Net::SFTP::StatusException; end
  end
end
remove!(package) click to toggle source

Called by the Cycler. Any error raised will be logged as a warning.

# File lib/backup/storage/sftp.rb, line 49
def remove!(package)
  Logger.info "Removing backup package dated #{ package.time }..."

  remote_path = remote_path_for(package)
  connection do |sftp|
    package.filenames.each do |filename|
      sftp.remove!(File.join(remote_path, filename))
    end

    sftp.rmdir!(remote_path)
  end
end
transfer!() click to toggle source
# File lib/backup/storage/sftp.rb, line 34
def transfer!
  connection do |sftp|
    create_remote_path(sftp)

    package.filenames.each do |filename|
      src = File.join(Config.tmp_path, filename)
      dest = File.join(remote_path, filename)
      Logger.info "Storing '#{ ip }:#{ dest }'..."
      sftp.upload!(src, dest)
    end
  end
end