Skip to content

Commit

Permalink
Merge branch 'push_pull' of git://github.com/splendeo/backup into burns
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian D. Burns committed Jan 6, 2012
2 parents 139ffe7 + d9e453f commit ccfe4a1
Show file tree
Hide file tree
Showing 23 changed files with 861 additions and 253 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
backup (3.0.19)
backup (3.0.20)
POpen4 (~> 0.1.4)
thor (~> 0.14.6)

Expand Down Expand Up @@ -72,7 +72,7 @@ GEM
net-ssh (2.1.4)
nokogiri (1.5.0)
oauth (0.4.5)
open4 (1.2.0)
open4 (1.3.0)
polyglot (0.3.2)
prowler (1.3.1)
rack (1.3.2)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -99,7 +99,7 @@ Storage Features
Syncers
-------

- RSync
- RSync (Push, Pull and Local)
- Amazon Simple Storage Service (S3)

[Syncer Wiki Page](https://github.com/meskyanichi/backup/wiki/Syncers)
Expand Down
37 changes: 32 additions & 5 deletions lib/backup.rb
Expand Up @@ -36,7 +36,7 @@ module Backup
STORAGES = ['S3', 'CloudFiles', 'Ninefold', 'Dropbox', 'FTP', 'SFTP', 'SCP', 'RSync', 'Local']
COMPRESSORS = ['Gzip', 'Bzip2', 'Pbzip2', 'Lzma']
ENCRYPTORS = ['OpenSSL', 'GPG']
SYNCERS = ['RSync', 'S3']
SYNCERS = ['S3', 'Rsync' => ['Push', 'Pull', 'Local']]
NOTIFIERS = ['Mail', 'Twitter', 'Campfire', 'Presently', 'Prowl', 'Hipchat']

##
Expand Down Expand Up @@ -105,8 +105,12 @@ module Storage
# Autoload Backup syncer files
module Syncer
autoload :Base, File.join(SYNCER_PATH, 'base')
autoload :RSync, File.join(SYNCER_PATH, 'rsync')
autoload :S3, File.join(SYNCER_PATH, 's3')
module RSync
autoload :Push, File.join(SYNCER_PATH, 'rsync', 'push')
autoload :Pull, File.join(SYNCER_PATH, 'rsync', 'pull')
autoload :Local, File.join(SYNCER_PATH, 'rsync', 'local')
end
end

##
Expand Down Expand Up @@ -195,8 +199,12 @@ module Storage
end

module Syncer
autoload :RSync, File.join(CONFIGURATION_PATH, 'syncer', 'rsync')
autoload :S3, File.join(CONFIGURATION_PATH, 'syncer', 's3')
module RSync
autoload :Push, File.join(CONFIGURATION_PATH, 'syncer', 'rsync', 'push')
autoload :Pull, File.join(CONFIGURATION_PATH, 'syncer', 'rsync', 'pull')
autoload :Local, File.join(CONFIGURATION_PATH, 'syncer', 'rsync', 'local')
end
end

module Database
Expand All @@ -209,12 +217,31 @@ module Database
end
end

private

def self.create_empty_class(class_name, scope = Backup::Finder)
scope.const_set(class_name, Class.new) unless scope.const_defined?(class_name)
end

def self.get_or_create_empty_module(module_name)
if Backup::Finder.const_defined?(module_name)
return Backup::Finder.const_get(module_name)
else
return Backup::Finder.const_set(module_name, Module.new)
end
end

##
# Dynamically defines all the available database, storage, compressor, encryptor and notifier
# classes inside Backup::Finder to improve the DSL for the configuration file
(DATABASES + STORAGES + COMPRESSORS + ENCRYPTORS + NOTIFIERS + SYNCERS).each do |constant|
unless Backup::Finder.const_defined?(constant)
Backup::Finder.const_set(constant, Class.new)
if constant.is_a?(Hash)
constant.each do |module_name, class_names|
mod = get_or_create_empty_module(module_name)
class_names.each{ |class_name| create_empty_class(class_name, mod) }
end
else
create_empty_class(constant)
end
end
end
7 changes: 5 additions & 2 deletions lib/backup/configuration/helpers.rb
Expand Up @@ -9,8 +9,11 @@ module Helpers
# configuration for these methods, if they respond then they will
# assign the object's attribute(s) to that particular global configuration's attribute
def load_defaults!
c = self.class.name.split('::')
configuration = Backup::Configuration.const_get(c[1]).const_get(c[2])
module_names = self.class.name.split('::')[1..-1]
configuration = Backup::Configuration
module_names.each do |module_name|
configuration = configuration.const_get(module_name)
end

getter_methods.each do |attribute|
if configuration.respond_to?(attribute)
Expand Down
45 changes: 0 additions & 45 deletions lib/backup/configuration/syncer/rsync.rb

This file was deleted.

31 changes: 31 additions & 0 deletions lib/backup/configuration/syncer/rsync/local.rb
@@ -0,0 +1,31 @@
# encoding: utf-8

module Backup
module Configuration
module Syncer
module RSync
class Local < Configuration::Base
class << self

##
# Directories to sync
attr_accessor :directories

##
# Path to store the synced files/directories to
attr_accessor :path

##
# Flag for mirroring the files/directories
attr_accessor :mirror

##
# Additional options for the rsync cli
attr_accessor :additional_options

end
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/backup/configuration/syncer/rsync/pull.rb
@@ -0,0 +1,12 @@
# encoding: utf-8

module Backup
module Configuration
module Syncer
module RSync
class Pull < Push
end
end
end
end
end
31 changes: 31 additions & 0 deletions lib/backup/configuration/syncer/rsync/push.rb
@@ -0,0 +1,31 @@
# encoding: utf-8

module Backup
module Configuration
module Syncer
module RSync
class Push < Local
class << self

##
# Server credentials
attr_accessor :username, :password

##
# Server IP Address and SSH port
attr_accessor :ip

##
# The SSH port to connect to
attr_accessor :port

##
# Flag for compressing (only compresses for the transfer)
attr_accessor :compress

end
end
end
end
end
end
49 changes: 21 additions & 28 deletions lib/backup/model.rb
Expand Up @@ -125,61 +125,49 @@ def initialize(trigger, label, &block)
# Adds a database to the array of databases
# to dump during the backup process
def database(database, &block)
@databases << Backup::Database.const_get(
last_constant(database)
).new(&block)
@databases << get_class_from_scope(Backup::Database, database).new(&block)
end

##
# Adds an archive to the array of archives
# to store during the backup process
def archive(name, &block)
@archives << Backup::Archive.new(name, &block)
def archive(archive_name, &block)
@archives << Backup::Archive.new(archive_name, &block)
end

##
# Adds an encryptor to the array of encryptors
# to use during the backup process
def encrypt_with(name, &block)
@encryptors << Backup::Encryptor.const_get(
last_constant(name)
).new(&block)
def encrypt_with(encryptor, &block)
@encryptors << get_class_from_scope(Backup::Encryptor, encryptor).new(&block)
end

##
# Adds a compressor to the array of compressors
# to use during the backup process
def compress_with(name, &block)
@compressors << Backup::Compressor.const_get(
last_constant(name)
).new(&block)
def compress_with(compressor, &block)
@compressors << get_class_from_scope(Backup::Compressor, compressor).new(&block)
end

##
# Adds a notifier to the array of notifiers
# to use during the backup process
def notify_by(name, &block)
@notifiers << Backup::Notifier.const_get(
last_constant(name)
).new(&block)
def notify_by(notifier_name, &block)
@notifiers << get_class_from_scope(Backup::Notifier, notifier_name).new(&block)
end

##
# Adds a storage method to the array of storage
# methods to use during the backup process
def store_with(storage, storage_id = nil, &block)
@storages << Backup::Storage.const_get(
last_constant(storage)
).new(storage_id, &block)
@storages << get_class_from_scope(Backup::Storage, storage).new(storage_id, &block)
end

##
# Adds a syncer method to the array of syncer
# methods to use during the backup process
def sync_with(syncer, &block)
@syncers << Backup::Syncer.const_get(
last_constant(syncer)
).new(&block)
@syncers << get_class_from_scope(Backup::Syncer, syncer).new(&block)
end

##
Expand Down Expand Up @@ -323,11 +311,16 @@ def procedure_instance_variables
end

##
# Returns the string representation of the last value of a nested constant
# example: last_constant(Backup::Model::MySQL) becomes and returns "MySQL"
def last_constant(constant)
constant.to_s.split("::").last
# Returns the class/model specified by a string inside a scope.
# For example, given the scope Backup::Model and the name "MySQL",
# it returns the class Backup::Model::MySQL. It accepts deeply nested class/modules
def get_class_from_scope(scope, constant)
return constant if constant.is_a? Class
klass = scope
constant.split('::').each do |chunk|
klass = klass.const_get(chunk)
end
klass
end

end
end

0 comments on commit ccfe4a1

Please sign in to comment.