Sha256: afa5cfb86bdb9cdc03936bc8e19bbccefa9da9076f8e16930e0631aba72e3d0d

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8
require 'net/scp'

module Backup
  module Storage
    class SCP < Base
      class Error < Backup::Error; end

      ##
      # Server credentials
      attr_accessor :username, :password, :ssh_options

      ##
      # Server IP Address and SCP port
      attr_accessor :ip, :port

      def initialize(model, storage_id = nil)
        super

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

      private

      def connection
        Net::SSH.start(
          ip, username, { :password => password, :port => port }.merge(ssh_options)
        ) {|ssh| yield ssh }
      end

      def transfer!
        connection do |ssh|
          ssh.exec!("mkdir -p '#{ remote_path }'")

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

      # Called by the Cycler.
      # Any error raised will be logged as a warning.
      def remove!(package)
        Logger.info "Removing backup package dated #{ package.time }..."

        errors = []
        connection do |ssh|
          ssh.exec!("rm -r '#{ remote_path_for(package) }'") do |ch, stream, data|
            errors << data if stream == :stderr
          end
        end
        unless errors.empty?
          raise Error, "Net::SSH reported the following errors:\n" +
              errors.join("\n")
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
backup-3.11.0 lib/backup/storage/scp.rb
backup-3.10.0 lib/backup/storage/scp.rb
backup-3.9.0 lib/backup/storage/scp.rb
backup-3.8.0 lib/backup/storage/scp.rb
backup-3.7.2 lib/backup/storage/scp.rb