class Backup::Syncer::RSync::Push
Attributes
Additional SSH Options
Used to supply a String or Array of options to be passed to the SSH command in `:ssh` and `:ssh_daemon` modes.
For example, if you need to supply a specific SSH key for the `ssh_user`, you would set this to: “-i '/path/to/id_rsa'”. Which would produce:
rsync -e "ssh -p 22 -i '/path/to/id_rsa'"
Arguments may be single-quoted, but should not contain any double-quotes.
Used only for `:ssh` and `:ssh_daemon` modes.
Flag for compressing (only compresses for the transfer)
Server Address
Mode of operation
- :ssh (default)
-
Connects to the remote via SSH. Does not use an rsync daemon on the remote.
- :ssh_daemon
-
Connects to the remote via SSH. Spawns a single-use daemon on the remote, which allows certain daemon features (like modules) to be used.
- :rsync_daemon
-
Connects directly to an rsync daemon via TCP. Data transferred is not encrypted.
SSH or RSync
port
For `:ssh` or `:ssh_daemon` mode, this specifies the SSH port to use and defaults to 22.
For `:rsync_daemon` mode, this specifies the TCP port to use and defaults to 873.
RSync
Password File
If specified, this path will be passed to rsync's `–password-file` option for daemon authentication.
Used only for `:ssh_daemon` and `:rsync_daemon` modes.
RSync
User
If the user running the backup is not the same user that needs to authenticate with the rsync daemon, specify the user here.
Used only for `:ssh_daemon` and `:rsync_daemon` modes.
SSH User
If the user running the backup is not the same user that needs to authenticate with the remote server, specify the user here.
The user must have SSH keys setup for passphrase-less access to the remote. If the SSH User does not have passphrase-less keys, or no default keys in their `~/.ssh` directory, you will need to use the `-i` option in `:additional_ssh_options` to specify the passphrase-less key to use.
Used only for `:ssh` and `:ssh_daemon` modes.
Public Class Methods
Backup::Syncer::RSync::Base::new
# File lib/backup/syncer/rsync/push.rb, line 104 def initialize(syncer_id = nil) super @mode ||= :ssh @port ||= mode == :rsync_daemon ? 873 : 22 @compress ||= false end
Public Instance Methods
# File lib/backup/syncer/rsync/push.rb, line 112 def perform! log!(:started) write_password_file! create_dest_path! run("#{ rsync_command } #{ paths_to_push } " + "#{ host_options }'#{ dest_path }'") log!(:finished) ensure remove_password_file! end
Private Instance Methods
# File lib/backup/syncer/rsync/push.rb, line 164 def compress_option compress ? ' --compress' : '' end
Runs a 'mkdir -p' command on the remote to ensure the dest_path
exists. This used because rsync will attempt to create the path, but will only call 'mkdir' without the '-p' option. This is only applicable in :ssh mode, and only used if the path would require this.
# File lib/backup/syncer/rsync/push.rb, line 139 def create_dest_path! return unless mode == :ssh && dest_path.index('/').to_i > 0 run "#{ utility(:ssh) } #{ ssh_transport_args } #{ host } " + %Q["mkdir -p '#{ dest_path }'"] end
Remove any preceeding '~/', since this is on the remote, and remove any trailing `/`.
# File lib/backup/syncer/rsync/push.rb, line 130 def dest_path @dest_path ||= path.sub(/^~\//, '').sub(/\/$/, '') end
# File lib/backup/syncer/rsync/push.rb, line 168 def password_option return '' if mode == :ssh path = @password_file ? @password_file.path : rsync_password_file path ? " --password-file='#{ File.expand_path(path) }'" : '' end
# File lib/backup/syncer/rsync/push.rb, line 198 def remove_password_file! @password_file.delete if @password_file end
Common base command, plus options for Push/Pull
Backup::Syncer::RSync::Base#rsync_command
# File lib/backup/syncer/rsync/push.rb, line 160 def rsync_command super << compress_option << password_option << transport_options end
# File lib/backup/syncer/rsync/push.rb, line 183 def ssh_transport_args args = "-p #{ port } " args << "-l #{ ssh_user } " if ssh_user args << Array(additional_ssh_options).join(' ') args.rstrip end
# File lib/backup/syncer/rsync/push.rb, line 175 def transport_options if mode == :rsync_daemon " --port #{ port }" else %Q[ -e "#{ utility(:ssh) } #{ ssh_transport_args }"] end end
# File lib/backup/syncer/rsync/push.rb, line 190 def write_password_file! return unless rsync_password && mode != :ssh @password_file = Tempfile.new('backup-rsync-password') @password_file.write(rsync_password) @password_file.close end