Using SCP in Linux

Using SCP in Linux

After securing your Linux VPS and deploying web server applications, it is time to synchronize web server files from old VPS to VPSie.

This article assumes that source and destination systems have the same users with the same user IDs – especially daemon application users.

There are new fashion methods (like puppet or git) and multiple old fashion and straight forward methods: using rsync (not installed by default in all Linux distributions), scp if you are copying from another Linux machine or WinSCP if you are copying from a Windows machine.

I will shortly discuss SCP command here. To synchronize a directory from local host to your remote new VPSie, one needs to use especially the “-r Recursively copy entire directories. Note that scp follows symbolic links encountered in the tree traversal.” option.

Example:

$ scp -r /tmp/test.origin myfirstvpsie:/tmp/test.destination/
user@myfirstvpsie$ ls -la /tmp/test.destination/
total 12
drwxr-xr-x 3 root root 4096 Nov 23 15:59 .
drwxrwxrwt 3 root root 4096 Nov 23 15:57 ..
drwxr-xr-x 2 root root 4096 Nov 23 15:59 test.origin

Scp also supports shell wildcard input for selective file names copy:

$ ls -la
total 320
drwxr-xr-x 2 root root   4096 Nov 23 22:56 .
drwxrwxrwt 6 root root 319488 Nov 23 23:21 ..
-rw-r--r-- 1 root root      0 Nov 23 22:56 abcd
-rw-r--r-- 1 root root      0 Nov 23 22:56 bcde
-rw-r--r-- 1 root root      0 Nov 23 22:56 degh
-rw-r--r-- 1 root root      0 Nov 23 22:56 eghi

Below, I will copy only files that contain the string “bc” in the middle.

$ scp /tmp/test.origin/*bc* 162.220.55.158:/tmp/test.destination
user@myfirstvpsie$ ls -la
total 8
drwxr-xr-x 2 root root 4096 Nov 23 16:22 .
drwxrwxrwt 3 root root 4096 Nov 23 16:17 ..
-rw-r--r-- 1 root root    0 Nov 23 16:22 abcd
-rw-r--r-- 1 root root    0 Nov 23 16:22 bcde

Few other scp options that need to be checked out are:
*** Mandatory *** User ownership, file timestamps and permissions:
-p Preserves modification times, access times, and modes from the original file.
-P port
Specifies the port to connect to on the remote host. Note that this option is written with a capital “Pâ”, because -p is already reserved for preserving the times and modes of the file

Next steps:
To confirm origin and destination directories are in sync, one can use find command to confirm same number of files:

$ find /tmp/test.origin -type f | wc -l
$ find /tmp/test.destination -type f | wc -l

Check destination directory on the destination server have the same permissions as origin one.

Leave A Comment?

19 − 14 =