sur says ajax :on => rails

Monday, April 17, 2006

Net-SSH and Net-SFTP


Hello readers!
This post is all about the power of Ruby.
After the day full of work I need to take the backup of my work there on remote as well as on my local machine.
Ruby Scripting helps the way out in a beautiful manner. There are two modules Net::SSH and Net::SFTP available at RUBY FORGE which can add a lots of more geek functionalities to your daily routine. One of them has relaxed me a lot.
Here I am going to explain a small n great use of Net::SSH and Net::SFTP.
-----------------------Pre-Requisites------------------------
To use this functionality from your local machine u will have to install ruby on your local machine.
After that install these two modules simply using Ruby GEM as follows...

1.)
2.)

after installation write the following code and save it as a ruby file say "backup.rb"
here is the code for "backup.rb"



#----------------BACKUP YOUR DATA ON LOCAL MACHINE--------------#
require 'net/ssh'
require 'net/sftp'
#----------------CONNECTION PARAMETERS--------------------------#
#... If u wanna use the default connection parameters then set the $default variable to "y" and
#... set the default values for $host,$username,$password variables in following elsif block.
#... Or Set $default to "n" to provide the connection parameters dynamically.
$default = "n"
if $default == "n"
print " Host : "
$host = gets().chop
print "Username : "
$username = gets().chop
print "Password : "
$password = gets().chop
elsif $default == "y"
$host = "localhost"
$username = "user"
$password = "pass"
end
#----------------TO BE MODIFIED AS PER NEED-----------------------#
$file_path = "exe/"
$file_exist = "temp1"
$download_path = "c:/test_backup/"
#-----------------------------------------------------------------#
time_now = Time.now()
$file_new = $file_exist + "_" + time_now.strftime("%a_%b_%d_%Y_%H_%M_%S") + ".tar.gz"
#------------------------NET::SSH----------------------------------#
Net::SSH.start($host, $username, $password) do ¦session¦
shell = session.shell.open
puts "SHELL IS RUNNING..."
shell.tar "-czf " + $file_path + $file_new + " " + $file_path + $file_exist
puts $file_new + " is successfully created "
shell.exit
sleep 0.5
$stdout.print shell.stdout while shell.stdout?
session.loop
end
#------------------------NET::SFTP----------------------------------#
Net::SFTP.start($host, $username, $password) do ¦sftp¦
sftp.get_file $file_path + $file_new, $download_path + $file_new
end
#------feel free to send comments/queries at sur.max@gmail.com------#


now through command prompt run the file "backup.rb"
and enjoy the simple and trendy way to backup your work.

0 Comments:

Post a Comment

<< Home