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.
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:-
And execute something like this:-
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:-
.. 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:-
.. where the recursive command -r tells scp we're copying a folder and it's total content. Or:-
.. 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:-
.. just add the revised filename to the end. Or, maybe you wanna change the folder path? Fussy, huh? ..
.. 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:-
.. where the little p means preserve. Aha, so that's why you gotta use the big P for port.
Copy/Backup Linux Local FROM Linux Remote
All the principles are the same, just the other way round.
So instead of:-
.. we syntax:-
.. where firstname.lastname@example.org: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:-
.. 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.
.. 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:-
.. 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.