Vlad Cenan
Vlad Cenan

Reputation: 172

Copy a folder from server1 to server2 running the script from localhost linux

I want to create a backup script in ruby and I didn't find any solution for copying a folder/file from an instance to another instance running the script from my localhost. Something like this:

Net::SSH.start("ip_address", "username",:password => "*********") do |session|
  session.scp.download!("/home/logfiles/*", "/home/backupstorage",  :recursive => true)
end

Upvotes: 1

Views: 272

Answers (2)

Zach Dennis
Zach Dennis

Reputation: 1784

Manoel's answer is in the right direction, but here are the details of using rsync:

rsync -avz /home/logfiles/* user@my-awesome-backup-machine:/home/backupstorage/
  • rsync works over SSH, so if you have ssh on your backup server you'll be good to go
  • rsync backs up by using deltas, that is it only copies blocks that are different, so the first time you run this it may take a while, but every time after that it only copies over things that have changed
  • -avz tells rsync to archive and compress and to be verbose (print out what it's doing to STDOUT).
  • user is your user on your backup server.
  • my-awesome-backup-machine is the ip address or hostname of your backup server.

If you want to also delete files that are no longer on your host machine then you may want to look into using the --delete option.

scp kind of sucks because you copy a file at a time. rsync is awesome because you can copy entire directory trees, recursively, exclude file patterns, etc.

UPDATE

Updating based on OP's comment below.

You can SSH from your machine into the first server, then run the command to copy things to the second server. If you personally have access to both machines you can turn on SSH forward with SSH's ForwardAgent option on your machine so that when you rsync from server1 to server2 it will fall back and try to use your key pair. Reference: https://developer.github.com/guides/using-ssh-agent-forwarding/

If you don't want to turn on SSH agent forwarding you can generate a new key pair for the user on server1 and put the public key in the ~/.ssh/authorized_keys file of the user you are rsync'ing as on server2.

Another option instead of using keys would be turn on host-based authentication on server2. That would allow you to avoid having to generate key pairs.

Upvotes: 2

This way you will replace files that haven't changed at all. Would be better to use rsync in this case. See rsync backup or this rsync gem if you really want to use ruby

Upvotes: 3

Related Questions