Skip to main content
edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

I have written this Ruby script to download some file from server using SFTP. Help me improviseimprove this one. Find the original script at this linklink

I have written this Ruby script to download some file from server using SFTP. Help me improvise this one. Find the original script at this link

I have written this Ruby script to download some file from server using SFTP. Help me improve this one. Find the original script at this link

Source Link

SFTP Ruby script for downloading files from server

I have written this Ruby script to download some file from server using SFTP. Help me improvise this one. Find the original script at this link

#This script is used to download file from the server to your local machine
#The implementation is based on pure Ruby implementation of SFTP using gem Net SFTP
#To install this on your local machine please run the following command in your terminal 'gem install net-sftp'
#Author : Ashish Wadekar <[email protected]>
#Date : 18 December 2016

#This is the gem / library required for the SFTP connection management
require 'net/sftp'

#The host address
HOST = "www.example.com"

#User for connection
USER = "admin"

#The location of the authentication key in pem format
SERVER_KEY_LOC = "/location/of/serverauthenticationkey.pem"

#The location on server where DB backups are done
SERVER_BACKUP = "/location/of/file/on/server"

#The location on local machine where the db has to be downloaded
LOCAL_BACKUP = "/location/on/local/machine/where/file/needs/to/be/downloaded"

#Array to hold the file names for easier sorting
files = Array.new

#Array to hold the file details for easier sorting
files_details = Array.new

#Inititalising the connection
Net::SFTP.start(HOST, USER, :keys => [SERVER_KEY_LOC]) do |sftp|
  puts "Connection to Server successful"
  entries = sftp.dir.entries([SERVER_BACKUP]).sort_by(&:name) #Sorting the files by name as timestamp is used as filename
  entries.each do |entry|
    files << entry.name
    files_details << entry.longname
  end

  download_size = files_details[-1].split(' ')[4].to_i

  puts "Total #{(files.size.to_i) - 2} backups found on server."
  puts "The latest file is : #{files[-1]}, Size : #{(download_size)/1048576} MB"
  puts "Now donwloading the file to your local machine at path : #{LOCAL_BACKUP}"


  sftp.download!(SERVER_BACKUP+files[-1], LOCAL_BACKUP+files[-1]) do |event, downloader, *args|
    case event
    when :open then
      # args[0] : file metadata
      puts "Starting download from server: #{args[0].remote} -> #{args[0].local}"
      puts "Perhaps you can sip a coffee till then!"
    when :get then
      # args[0] : file metadata
      # args[1] : byte offset in remote file
      # args[2] : data that was received
      STDOUT.write "Downloading complete: #{args[1].to_i/1048576}MB, #{args[1].to_i}bytes of #{download_size} bytes, #{((args[1].to_i / download_size) * 100.0)}% \r" unless args[1].to_i == 0

    when :close then
      # args[0] : file metadata
      puts "Done! Closing connection to server."
    when :mkdir then
      # args[0] : local path name
      puts "Creating directory #{args[0]}"
    when :finish then
      puts "Backup download complete!"
    end
  end
end