Skip to content

Instantly share code, notes, and snippets.

@ashishwadekar
Created December 28, 2016 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishwadekar/464b06db404c67b6d67f0a8c5fd8f601 to your computer and use it in GitHub Desktop.
Save ashishwadekar/464b06db404c67b6d67f0a8c5fd8f601 to your computer and use it in GitHub Desktop.
This script is used to download the latest file from the server to your local machine using Ruby & pem key
#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 <ashishawadekar@gmail.com>
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment