Sha256: 47ac35a5a38f9f593ee6035a94f33a2534e07f98acf526d674aed3d62edec8ab

Contents?: true

Size: 1.6 KB

Versions: 56

Compression:

Stored size: 1.6 KB

Contents

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

module Backup
  module Storage
    class SCP < Base
      include Storage::Cycler
      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

56 entries across 56 versions & 7 rubygems

Version Path
backup-remote-0.0.7 lib/backup/storage/scp.rb
backup-remote-0.0.5 lib/backup/storage/scp.rb
backup-remote-0.0.3 lib/backup/storage/scp.rb
backup-remote-0.0.2 lib/backup/storage/scp.rb
backup-4.4.0 lib/backup/storage/scp.rb
backup-4.3.0 lib/backup/storage/scp.rb
backup-4.2.3 lib/backup/storage/scp.rb
backup-4.2.2 lib/backup/storage/scp.rb
backup_zh-4.2.0 lib/backup/storage/scp.rb
backup-4.2.0 lib/backup/storage/scp.rb
backup-4.1.12 lib/backup/storage/scp.rb
backup-4.1.11 lib/backup/storage/scp.rb
backup-ssh-4.1.10 lib/backup/storage/scp.rb
backup-4.1.10 lib/backup/storage/scp.rb
backup-4.1.9 lib/backup/storage/scp.rb
backup-4.1.8 lib/backup/storage/scp.rb
backup-4.1.7 lib/backup/storage/scp.rb
backup-4.1.6 lib/backup/storage/scp.rb
backup-4.1.5 lib/backup/storage/scp.rb
backup-4.1.4 lib/backup/storage/scp.rb