class Backup::Syncer::RSync::Push

Attributes

additional_ssh_options[RW]

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.

compress[RW]

Flag for compressing (only compresses for the transfer)

host[RW]

Server Address

mode[RW]

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.

port[RW]

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[RW]

RSync Password

If specified, Backup will write the password to a temporary file and use it with rsync’s ‘–password-file` option for daemon authentication.

Note that setting this will override ‘rsync_password_file`.

Used only for ‘:ssh_daemon` and `:rsync_daemon` modes.

rsync_password_file[RW]

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[RW]

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[RW]

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

new(syncer_id = nil) click to toggle source
Calls superclass method Backup::Syncer::RSync::Base::new
# File lib/backup/syncer/rsync/push.rb, line 101
def initialize(syncer_id = nil)
  super

  @mode ||= :ssh
  @port ||= mode == :rsync_daemon ? 873 : 22
  @compress ||= false
end

Public Instance Methods

perform!() click to toggle source
# File lib/backup/syncer/rsync/push.rb, line 109
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

compress_option() click to toggle source
# File lib/backup/syncer/rsync/push.rb, line 161
def compress_option
  compress ? " --compress" : ""
end
create_dest_path!() click to toggle source

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 136
def create_dest_path!
  return unless mode == :ssh && dest_path.index("/").to_i > 0

  run "#{utility(:ssh)} #{ssh_transport_args} #{host} " +
    %("mkdir -p '#{dest_path}'")
end
dest_path() click to toggle source

Remove any preceeding ‘~/’, since this is on the remote, and remove any trailing ‘/`.

# File lib/backup/syncer/rsync/push.rb, line 127
def dest_path
  @dest_path ||= path.sub(/^~\//, "").sub(/\/$/, "")
end
host_options() click to toggle source

For Push, this will prepend the dest_path. For Pull, this will prepend the first path in paths_to_pull.

# File lib/backup/syncer/rsync/push.rb, line 146
def host_options
  if mode == :ssh
    "#{host}:"
  else
    user = "#{rsync_user}@" if rsync_user
    "#{user}#{host}::"
  end
end
password_option() click to toggle source
# File lib/backup/syncer/rsync/push.rb, line 165
def password_option
  return "" if mode == :ssh

  path = @password_file ? @password_file.path : rsync_password_file
  path ? " --password-file='#{File.expand_path(path)}'" : ""
end
remove_password_file!() click to toggle source
# File lib/backup/syncer/rsync/push.rb, line 195
def remove_password_file!
  @password_file.delete if @password_file
end
rsync_command() click to toggle source

Common base command, plus options for Push/Pull

# File lib/backup/syncer/rsync/push.rb, line 157
def rsync_command
  super << compress_option << password_option << transport_options
end
ssh_transport_args() click to toggle source
# File lib/backup/syncer/rsync/push.rb, line 180
def ssh_transport_args
  args = "-p #{port} "
  args << "-l #{ssh_user} " if ssh_user
  args << Array(additional_ssh_options).join(" ")
  args.rstrip
end
transport_options() click to toggle source
# File lib/backup/syncer/rsync/push.rb, line 172
def transport_options
  if mode == :rsync_daemon
    " --port #{port}"
  else
    %( -e "#{utility(:ssh)} #{ssh_transport_args}")
  end
end
write_password_file!() click to toggle source
# File lib/backup/syncer/rsync/push.rb, line 187
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