Sha256: 387ae00f3c888f511439925cf3836f9182ed0157a2c845af90146ba3ec164d09

Contents?: true

Size: 1.98 KB

Versions: 30

Compression:

Stored size: 1.98 KB

Contents

# encoding: utf-8
require 'fog'

module Backup
  module Storage
    class Ninefold < Base
      include Storage::Cycler
      class Error < Backup::Error; end

      ##
      # Ninefold Credentials
      attr_accessor :storage_token, :storage_secret

      def initialize(model, storage_id = nil)
        super

        @path ||= 'backups'
        path.sub!(/^\//, '')
      end

      private

      def connection
        @connection ||= Fog::Storage.new(
          :provider                => 'Ninefold',
          :ninefold_storage_token  => storage_token,
          :ninefold_storage_secret => storage_secret
        )
      end

      def transfer!
        directory = directory_for(remote_path, true)
        package.filenames.each do |filename|
          src = File.join(Config.tmp_path, filename)
          dest = File.join(remote_path, filename)
          Logger.info "Storing '#{ dest }'..."
          File.open(src, 'r') do |file|
            directory.files.create(:key => filename, :body => file)
          end
        end
      end

      ##
      # Queries the connection for the directory for the given +remote_path+
      # Returns nil if not found, or creates the directory if +create+ is true.
      def directory_for(remote_path, create = false)
        directory = connection.directories.get(remote_path)
        if directory.nil? && create
          directory = connection.directories.create(:key => remote_path)
        end
        directory
      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 }..."

        remote_path = remote_path_for(package)
        directory = directory_for(remote_path)

        raise Error, "Directory at '#{ remote_path }' not found" unless directory

        package.filenames.each do |filename|
          file = directory.files.get(filename)
          file.destroy if file
        end

        directory.destroy
      end

    end
  end
end

Version data entries

30 entries across 30 versions & 5 rubygems

Version Path
backup-4.0.6 lib/backup/storage/ninefold.rb
backup-4.0.5 lib/backup/storage/ninefold.rb
backup-4.0.4 lib/backup/storage/ninefold.rb
backup_zh-4.0.3.1 lib/backup/storage/ninefold.rb
backup-4.0.3 lib/backup/storage/ninefold.rb
backup-4.0.2 lib/backup/storage/ninefold.rb
nfm-backup-4.0.1a lib/backup/storage/ninefold.rb
backup-4.0.1 lib/backup/storage/ninefold.rb
backup-4.0.0 lib/backup/storage/ninefold.rb
backup-4.0.0rc1 lib/backup/storage/ninefold.rb