Skip to content

Commit

Permalink
update Syncers
Browse files Browse the repository at this point in the history
- add RSync::Base
- adjust paths according to direction
- ensure removal of temporary password file
- update all specs
  • Loading branch information
Brian D. Burns committed Jan 12, 2012
1 parent 9be16f7 commit d813160
Show file tree
Hide file tree
Showing 24 changed files with 1,009 additions and 694 deletions.
7 changes: 5 additions & 2 deletions lib/backup.rb
Expand Up @@ -81,9 +81,10 @@ module Syncer
autoload :Base, File.join(SYNCER_PATH, 'base')
autoload :S3, File.join(SYNCER_PATH, 's3')
module RSync
autoload :Base, File.join(SYNCER_PATH, 'rsync', 'base')
autoload :Local, File.join(SYNCER_PATH, 'rsync', 'local')
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 @@ -173,11 +174,13 @@ module Storage
end

module Syncer
autoload :Base, File.join(CONFIGURATION_PATH, 'syncer', 'base')
autoload :S3, File.join(CONFIGURATION_PATH, 'syncer', 's3')
module RSync
autoload :Base, File.join(CONFIGURATION_PATH, 'syncer', 'rsync', 'base')
autoload :Local, File.join(CONFIGURATION_PATH, 'syncer', 'rsync', 'local')
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

Expand Down
10 changes: 10 additions & 0 deletions lib/backup/configuration/syncer/base.rb
@@ -0,0 +1,10 @@
# encoding: utf-8

module Backup
module Configuration
module Syncer
class Base < Configuration::Base; end
end
end
end

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

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

##
# 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

22 changes: 1 addition & 21 deletions lib/backup/configuration/syncer/rsync/local.rb
Expand Up @@ -4,27 +4,7 @@ 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
class Local < Base; end
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/backup/configuration/syncer/rsync/pull.rb
Expand Up @@ -4,8 +4,7 @@ module Backup
module Configuration
module Syncer
module RSync
class Pull < Push
end
class Pull < Push; end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/backup/configuration/syncer/rsync/push.rb
Expand Up @@ -4,7 +4,7 @@ module Backup
module Configuration
module Syncer
module RSync
class Push < Local
class Push < Base
class << self

##
Expand Down
4 changes: 0 additions & 4 deletions lib/backup/configuration/syncer/s3.rb
Expand Up @@ -14,10 +14,6 @@ class << self
# Amazon S3 bucket name and path to sync to
attr_accessor :bucket, :path

##
# Directories to sync
attr_accessor :directories

##
# Flag to enable mirroring
attr_accessor :mirror
Expand Down
7 changes: 7 additions & 0 deletions lib/backup/syncer/base.rb
Expand Up @@ -5,6 +5,13 @@ module Syncer
class Base
include Backup::CLI::Helpers
include Backup::Configuration::Helpers

private

def syncer_name
self.class.to_s.sub('Backup::', '')
end

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

module Backup
module Syncer
module RSync
class Base < Syncer::Base

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

##
# Directories to sync
attr_writer :directories

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

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

##
# Instantiates a new RSync Syncer object
# and sets the default configuration
def initialize
load_defaults!

@path ||= 'backups'
@directories = Array.new
@mirror ||= false
@additional_options ||= Array.new
end

##
# Syntactical suger for the DSL for adding directories
def directories(&block)
return @directories unless block_given?
instance_eval(&block)
end

##
# Adds a path to the @directories array
def add(path)
@directories << path
end

private

##
# Returns the @directories as a space-delimited string of
# single-quoted values for use in the `rsync` command line.
# Each path is expanded, since these refer to local paths
# for both RSync::Local and RSync::Push.
# RSync::Pull does not use this method.
def directories_option
@directories.map do |directory|
"'#{ File.expand_path(directory) }'"
end.join(' ')
end

##
# Returns Rsync syntax for enabling mirroring
def mirror_option
'--delete' if @mirror
end

##
# Returns Rsync syntax for invoking "archive" mode
def archive_option
'--archive'
end

end
end
end
end
83 changes: 23 additions & 60 deletions lib/backup/syncer/rsync/local.rb
Expand Up @@ -3,87 +3,50 @@
module Backup
module Syncer
module RSync
class Local < Syncer::Base
class Local < Base

##
# Directories to sync
attr_writer :directories

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

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

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

##
# Instantiates a new RSync Syncer object and sets the default configuration
# specified in the Backup::Configuration::Syncer::RSync. Then it sets the object
# defaults if particular properties weren't set. Finally it'll evaluate the users
# configuration file and overwrite anything that's been defined
# Instantiates a new RSync::Local Syncer object.
# Default configuration values and any specified in
# Backup::Configuration::Syncer::RSync::Local are set from Base.
# The user's configuration file is then evaluated to overwrite
# these values or provide additional configuration.
def initialize(&block)
load_defaults!

@directories = Array.new
@additional_options ||= Array.new
@path ||= 'backups'
@mirror ||= false
super

instance_eval(&block) if block_given?

@path = path.sub(/^\~\//, '')
end

##
# Performs the RSync operation
# Performs the RSync::Local operation
# debug options: -vhP
def perform!
Logger.message("#{ self.class } started syncing #{ directories }.")
Logger.message(
"#{ syncer_name } started syncing the following directories:\n\s\s" +
@directories.join("\n\s\s")
)
Logger.silent(
run("#{ utility(:rsync) } #{ options } #{ directories } '#{ path }'")
run("#{ utility(:rsync) } #{ options } " +
"#{ directories_option } '#{ dest_path }'")
)
end

##
# Returns all the specified Rsync options, concatenated, ready for the CLI
def options
([archive, mirror] + additional_options).compact.join("\s")
end

##
# Returns Rsync syntax for enabling mirroring
def mirror
'--delete' if @mirror
end
private

##
# Returns Rsync syntax for invoking "archive" mode
def archive
'--archive'
# Return expanded @path
def dest_path
@dest_path ||= File.expand_path(@path)
end

##
# If no block has been provided, it'll return the array of @directories.
# If a block has been provided, it'll evaluate it and add the defined paths to the @directories
def directories(&block)
unless block_given?
return @directories.map do |directory|
"'#{directory}'"
end.join("\s")
end
instance_eval(&block)
# Returns all the specified Rsync::Local options,
# concatenated, ready for the CLI
def options
([archive_option, mirror_option] +
additional_options).compact.join("\s")
end

##
# Adds a path to the @directories array
def add(path)
@directories << path
end
end
end
end
Expand Down
21 changes: 18 additions & 3 deletions lib/backup/syncer/rsync/pull.rb
Expand Up @@ -6,17 +6,32 @@ module RSync
class Pull < Push

##
# Performs the RSync operation
# Performs the RSync::Pull operation
# debug options: -vhP
def perform!
write_password_file!

@directories.each do |directory|
Logger.message("#{ self.class } started syncing '#{ directory }'.")
Logger.message("#{ syncer_name } started syncing '#{ directory }'.")
Logger.silent(
run("#{ utility(:rsync) } #{ options } '#{ username }@#{ ip }:#{ directory }' '#{ path }'")
run("#{ utility(:rsync) } #{ options } " +
"'#{ username }@#{ ip }:#{ directory.sub(/^\~\//, '') }' " +
"'#{ dest_path }'")
)
end

ensure
remove_password_file!
end

private

##
# Return expanded @path
def dest_path
@dest_path ||= File.expand_path(@path)
end

end
end
end
Expand Down

0 comments on commit d813160

Please sign in to comment.