linein designs

27 Jan, 2009

Copy/Transfer Files Between Two Servers Using Linux scp

Posted by: john In: linux|programing|web

transfer_images

The other day we ran into a good problem that our site has grown and we are in need of a new server. So we ordered the server and now we need to transfer all the files from the old server to the new one. But rather than downloading all the files to our local machines then re-cuploading them to the new server, we’re going to show you how you can skip the middle man and transfer files from server to server.

The only requirements is that you need to have SSH access to both servers.

We’re going to be using the Linux command scp which stands for secure copy. (see http://linux.about.com/od/commands/l/blcmdl1_scp.htm for a complete listing of all the available options for scp)

We’re going to be using the following options:

  • r = recursively copy entire directories
  • C = compression enable
  • p = preserves modification times, access times, and modes from the original file
// To transfer all the files in the httpdocs directory to a folder on the remote machine
scp -rpC /var/www/httpdocs/* remote_user@remote_domain.com:/var/www/httpdocs

// Transfer only PHP files
scp -rpC /var/www/httpdocs/*.php remote_user@remote_domain.com:/var/www/httpdocs

Also, if you’re going to transfer a lot of data between the webservers, you probably want to add the nohup command too. nohup runs a command even if the session is disconnected or the user logs out. Another bit you can add is trailing ampersand (&) to the end the command to launch it in the background and get your command prompt back right away.

nohup scp -rpC /var/www/httpdocs/* remote_user@remote_domain.com:/var/www/httpdocs &

Hope this helps someone as much as it did me today. :)

This great tip came from Wes Widner of werxltd.com.

Similar Posts:

7 Comments »

 
Comment by Fernando

Simple, concise, perfect!

Thanks.-

 
Comment by Pete

Hi,

I tried the nohup method above, and when I run the command it bypasses the password prompt for some reason. Any idea how I can pass the password to the command? This is transfering files between two different servers.

Thanks,
Pete

Note: When I run the SCP command in regular mode it works fine. Just not in nohup mode.

 
Comment by Emmanuel

Very helpful…thanks a lot man…

 
Comment by Jerome

Thanks for this, just made my day.

 
Comment by zdenis

type your scp command with nohup at the beginning:
>nohup scp…

enter your password

stop temporarily the command with
CTRL+z

put on background the command, type:
>bg

yo can check the process with:
>jobs

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post


  • Seren: Thanks - google did not fail me in finding this snippet (after 2 hours of 'why isn't this working!' before I choose to look for answers. That select
  • zdenis: type your scp command with nohup at the beginning: >nohup scp... enter your password stop temporarily the command with CTRL+z put on b
  • Criz: php script >> How to get the number of hours and minutes of a two set of time like this sample. 1st set of time : 8:00 to 17:00 2nd set of


Please Support Our Friends

Banner
FreshBooks

About

My name is John Veldboom and have been in design and web programing for just over 8 years now. Started off as a hobby in high school but it has developed into a full time job now. I always tell people that if I wasn't doing this at work each day, I would be at home doing it for free. I love it!

Please visit the contact page to drop me a message.