lib/backup/storage/rsync.rb in backup-3.0.14 vs lib/backup/storage/rsync.rb in backup-3.0.15

- old
+ new

@@ -1,8 +1,12 @@ # 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 @@ -30,10 +34,11 @@ @port ||= 22 @path ||= 'backups' instance_eval(&block) if block_given? + write_password_file! @time = TIME @path = path.sub(/^\~\//, '') end @@ -45,10 +50,11 @@ ## # Performs the backup transfer def perform! transfer! + remove_password_file! end private ## @@ -56,19 +62,19 @@ # Not doing any instance variable caching because this object gets persisted in YAML # format to a file and will issues. This, however has no impact on performance since it only # gets invoked once per object for a #transfer! and once for a remove! Backups run in the # background anyway so even if it were a bit slower it shouldn't matter. def connection - Net::SSH.start(ip, username, :password => password, :port => port) + Net::SSH.start(ip, username, :password => @password, :port => port) end ## # Transfers the archived file to the specified remote server def transfer! Logger.message("#{ self.class } started transferring \"#{ remote_file }\".") create_remote_directories! - run("#{ utility(:rsync) } #{ options } '#{ File.join(local_path, local_file) }' '#{ username }@#{ ip }:#{ File.join(remote_path, remote_file[20..-1]) }'") + run("#{ utility(:rsync) } #{ options } #{ password } '#{ File.join(local_path, local_file) }' '#{ username }@#{ ip }:#{ File.join(remote_path, remote_file[20..-1]) }'") end ## # Removes the transferred archive file from the server def remove! @@ -90,9 +96,33 @@ # -z = Compresses the bytes that will be transferred to reduce bandwidth usage # --port = the port to connect to through SSH # -Phv = debug options def options "-z --port='#{ port }'" + end + + ## + # Returns Rsync syntax for using a password file + def password + "--password-file='#{@password_file.path}'" unless @password.nil? + end + + ## + # Writes the provided password to a temporary file so that + # the rsync utility can read the password from this file + def write_password_file! + unless @password.nil? + @password_file = Tempfile.new('backup-rsync-password') + @password_file.write(@password) + @password_file.close + end + end + + ## + # Removes the previously created @password_file + # (temporary file containing the password) + def remove_password_file! + @password_file.unlink unless @password.nil? end end end end