.

Maintain Unmanaged VPS – Part 10: Copy Site Files between Linux/Linux Computers with the CLI

English   English (change)

Maintain Unmanaged VPS – Part 10: Copy Site Files between Linux/Linux Computers with the CLI

SCP image

This tutorial shows how to copy files between Linux computers using Secure Copy & a command line interface. Fast, easy & safe, better than FTP or SFTP.

If you’re using Windows locally, skip to VPS Admin Part 11: Copy Website Files between Windows/Linux Computers using the CLI, ‘cos the method’s different.

If you’re after database backup, that’s different too, so check this out.

But for Linux-to-Linux file transfers, you’re in the right place, and whether local to remote machines, remote to local or remote to remote, Secure Copy is a super-fast program for transfers .. and the OpenSSH encryption protocol behind it means sensitive data is encrypted.

[sniplet guvSellBox]
[sniplet vpsIndexSell]

Just one prereq: you need the SSH (Secure Shell) protocol enabled on each machine. But hey, if you’ve just built or are looking to administer a remote server, that’s a must anyhow. If you’re not sure or want to sort that, read VPS Bible 3 – Setup PuTTY & VPS Bible 5 – PuTTY with OpenSSH.

Copy/Upload Linux Local TO Linux Remote

This first example will securely copy a local file to your remote user directory (ie, /home/user).

Logged into your shell, goto the local folder where the file to copy is located:-

[text]cd /path/to/folder[/text]

And execute something like this:-

[text]scp fileToCopy.txt username@12.34.56.78:[/text]

Chocks away.

Secure Copy Syntax Broken Down

  • scp starts the Secure Copy program
  • fileToCopy.txt is the file to upload
  • username is your remote Linux username
  • IP address is your remote IP address or hostname
  • : and don’t forget that!

.. that example presumes you’re using the default port, 22. If not – and you shouldn’t be * – specify another using this syntax:-

[text]scp -P 54321 fileToCopy.txt username@12.34.56.78:[/text]

.. where -P (not -p) tells scp we’re using a bespoke port, and 54321 is the example port.

* To find out about changing your Nginx port, for added security, read VPS BIBLE Part 6: Harden the Secure Shell (SSH) & Create a Firewall.

Or if you want to copy a folder and its contents, try this:-

[text]scp -r folder username@12.34.56.78:[/text]

.. where the recursive command -r tells scp we’re copying a folder and it’s total content. Or:-

[text]scp -rP 54321 folder username@12.34.56.78:[/text]

.. here we’re combining the recursive statement with the port.

If you want to change the filename from, say, abc.txt to xyz.txt, we can handle that:-

[text]scp -P 54321 abc.txt username@12.34.56.78:xyz.txt[/text]

.. just add the revised filename to the end. Or, maybe you wanna change the folder path? Fussy, huh? ..

[text]scp -P 54321 abc.txt username@12.34.56.78:/home/user/someFolder/xyz.txt[/text]

.. using the complete /path/to/folder and ensuring you’ve previously created the destination directory.

Finally, to preserve file timestamps and, if poss, the user, group and permissions, use this. I’ll example a folder and its contents, and put them in a specific folder:-

[text]scp -rpP 54321 fileToCopy.txt username@12.34.56.78:/some/path[/text]

.. where the little p means preserve. Aha, so that’s why you gotta use the big P for port.

Top notch.

Copy/Backup Linux Local FROM Linux Remote

All the principles are the same, just the other way round.

So instead of:-

[text]scp fileToCopy.txt username@12.34.56.78:[/text]

.. we syntax:-

[text]scp username@12.34.56.78:fileToCopy.txt fileToCopy.txt[/text]

.. where username@12.34.56.78:fileToCopy.txt specifies the file to copy from the remote host and fileToCopy.txt is the name we want to give the file, locally. We’ve previously cd‘ed to the directory into which we want to receive the file but, as with uploading, we could instead use a folder path. We could also preserve (p) the metadata and copy folders recursively.

To get a little more complicato:-

[text]scp -pP 54321 username@12.34.56.78:/home/user/Directory/abc.txt /home/user/Folder/xyz.txt[/text]

.. that says we want to copy the remote file abc.txt, specifying its location, but renaming it to xyz.txt in the specified folder which has been created previously. We’re not using port 21 so pinpoint the port 54321 with -P, and are preserving the files metadata with p.

[text]scp -rpP 54321 username@12.34.56.78:/home/user/Directory /home/user/Folder[/text]

.. this time, its basically the same, except we’re moving a folder and its content, so are employing the recursive, r, syntax.

Secure Copy Between Two Linux Hosts

Hmmn. Fact is, I dunno, cos I’ve not yet tried, so maybe someone can fill me in? Pretty please.

My guess would be something like:-

[text]scp username@IP_addressONE:/path.to/fileToCopy.txt username@IP_addressTWO:/path.to/fileToCopy.txt[/text]

.. but really that is untested so, hey, be sure to play with mock files.

Right. Quite enough of that. You see the deal. This is powerful stuff, ideal for uploading/moving/backing up a site and, relative to SFTP or FTP, super-fast. Yup. Way quicker than opening FileZilla SnailZilla, wading through the ridiculously slow-loading directory tree, yaaawn, all that. You get the picture.

[sniplet vpsIndex]

.