lib/backup/storage/rsync.rb in backup-3.0.20 vs lib/backup/storage/rsync.rb in backup-3.0.21
- old
+ new
@@ -1,12 +1,8 @@
# encoding: utf-8
##
-# Require the tempfile Ruby library when Backup::Storage::RSync is loaded
-require 'tempfile'
-
-##
# Only load the Net::SSH library when the Backup::Storage::RSync class is loaded
Backup::Dependency.load('net-ssh')
module Backup
module Storage
@@ -28,87 +24,83 @@
##
# Flag to use local backups
attr_accessor :local
##
- # This is the remote path to where the backup files will be stored
- def remote_path
- File.join(path, TRIGGER)
- end
+ # Creates a new instance of the storage object
+ def initialize(model, storage_id = nil, &block)
+ super(model, storage_id)
- ##
- # Performs the backup transfer
- def perform!
- super
- write_password_file!
- transfer!
- ensure
- remove_password_file!
- end
-
- private
-
- ##
- # Set configuration defaults before evaluating configuration block,
- # after setting defaults from Storage::Base
- def pre_configure
- super
@port ||= 22
@path ||= 'backups'
@local ||= false
+
+ instance_eval(&block) if block_given?
+
+ @path = path.sub(/^\~\//, '')
end
+ private
+
##
- # Adjust configuration after evaluating configuration block,
- # after adjustments from Storage::Base
- def post_configure
- super
- @path = path.sub(/^\~\//, '')
+ # This is the remote path to where the backup files will be stored
+ #
+ # Note: This overrides the superclass' method
+ def remote_path_for(package)
+ File.join(path, package.trigger)
end
##
- # Establishes a connection to the remote server and returns the Net::SSH object.
+ # Establishes a connection to the remote server
def connection
- Net::SSH.start(ip, username, :password => password, :port => port) do |ssh|
- yield ssh
- end
+ Net::SSH.start(
+ ip, username, :password => password, :port => port
+ ) {|ssh| yield ssh }
end
##
# Transfers the archived file to the specified remote server
def transfer!
- create_remote_directories!
+ write_password_file! unless local
- Logger.message "#{storage_name} started transferring " +
- "'#{ filename }' to '#{ ip }'."
+ remote_path = remote_path_for(@package)
- if local
- run(
- "#{ utility(:rsync) } '#{ File.join(local_path, filename) }' " +
- "'#{ File.join(remote_path, filename[20..-1]) }'"
- )
- else
- run(
- "#{ utility(:rsync) } #{ rsync_options } #{ rsync_port } " +
- "#{ rsync_password_file } '#{ File.join(local_path, filename) }' " +
- "'#{ username }@#{ ip }:#{ File.join(remote_path, filename[20..-1]) }'"
- )
+ create_remote_path!(remote_path)
+
+ files_to_transfer_for(@package) do |local_file, remote_file|
+ if local
+ Logger.message "#{storage_name} started transferring " +
+ "'#{ local_file }' to '#{ remote_path }'."
+ run(
+ "#{ utility(:rsync) } '#{ File.join(local_path, local_file) }' " +
+ "'#{ File.join(remote_path, remote_file) }'"
+ )
+ else
+ Logger.message "#{storage_name} started transferring " +
+ "'#{ local_file }' to '#{ ip }'."
+ run(
+ "#{ utility(:rsync) } #{ rsync_options } #{ rsync_port } " +
+ "#{ rsync_password_file } '#{ File.join(local_path, local_file) }' " +
+ "'#{ username }@#{ ip }:#{ File.join(remote_path, remote_file) }'"
+ )
+ end
end
+
+ ensure
+ remove_password_file! unless local
end
##
- # Note: RSync::Storage doesn't cycle
- def remove!
- nil
- end
+ # Note: Storage::RSync doesn't cycle
+ def remove!; end
##
# Creates (if they don't exist yet) all the directories on the remote
# server in order to upload the backup file.
- def create_remote_directories!
+ def create_remote_path!(remote_path)
if @local
- mkdir(remote_path)
+ FileUtils.mkdir_p(remote_path)
else
connection do |ssh|
ssh.exec!("mkdir -p '#{ remote_path }'")
end
end
@@ -128,9 +120,10 @@
##
# Removes the previously created @password_file
# (temporary file containing the password)
def remove_password_file!
@password_file.delete if @password_file
+ @password_file = nil
end
##
# Returns Rsync syntax for using a password file
def rsync_password_file