class Backup::Storage::SFTP
Attributes
ip[RW]
Server IP Address and SFTP
port
password[RW]
Server credentials
port[RW]
Server IP Address and SFTP
port
ssh_options[RW]
Server credentials
username[RW]
Server credentials
Public Class Methods
new(model, storage_id = nil)
click to toggle source
Calls superclass method
Backup::Storage::Base::new
# File lib/backup/storage/sftp.rb, line 16 def initialize(model, storage_id = nil) super @ssh_options ||= {} @port ||= 22 @path ||= "backups" path.sub!(/^~\//, "") end
Private Instance Methods
connection() { |sftp| ... }
click to toggle source
# File lib/backup/storage/sftp.rb, line 27 def connection Net::SFTP.start( ip, username, { password: password, port: port }.merge(ssh_options) ) { |sftp| yield sftp } end
create_remote_path(sftp)
click to toggle source
Creates (if they don’t exist yet) all the directories on the remote server in order to upload the backup file. Net::SFTP does not support paths to directories that don’t yet exist when creating new directories. Instead, we split the parts up in to an array (for each ‘/’) and loop through that to create the directories one by one. Net::SFTP raises an exception when the directory it’s trying to create already exists, so we have rescue it
# File lib/backup/storage/sftp.rb, line 69 def create_remote_path(sftp) path_parts = [] remote_path.split("/").each do |path_part| path_parts << path_part begin sftp.mkdir!(path_parts.join("/")) rescue Net::SFTP::StatusException; end end end
remove!(package)
click to toggle source
Called by the Cycler
. Any error raised will be logged as a warning.
# File lib/backup/storage/sftp.rb, line 48 def remove!(package) Logger.info "Removing backup package dated #{package.time}..." remote_path = remote_path_for(package) connection do |sftp| package.filenames.each do |filename| sftp.remove!(File.join(remote_path, filename)) end sftp.rmdir!(remote_path) end end
transfer!()
click to toggle source
# File lib/backup/storage/sftp.rb, line 33 def transfer! connection do |sftp| create_remote_path(sftp) package.filenames.each do |filename| src = File.join(Config.tmp_path, filename) dest = File.join(remote_path, filename) Logger.info "Storing '#{ip}:#{dest}'..." sftp.upload!(src, dest) end end end