Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make SCP ignore symbolic links during copy?

Tags:

scp

I need to reinstall one of ours servers, and as a precaution, I want to move /home, /etc, /opt, and /Services to backup server.

However, I have a problem: because of plenty of symbolic links a lot of files are copied multiple times.

Is it possible to make scp ignore the symbolic links (or actually to copy link as a link not as a directory or file)? If not, is there another way to do it?

like image 239
Kris_R Avatar asked Jun 14 '12 09:06

Kris_R


People also ask

Does scp copy symbolic links?

TL;DR: when using recursive scp, symbolic links aren't preserved and are copied as if they are normal directories.

What happens when you copy a symlink?

A symbolic link encountered in the tree traversal is copied instead of the file pointed to by the symbolic link. If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. This option causes cp to create special files rather than copying them as normal files.

What is difference between scp and rsync?

Copying files and directories with SCP or Rsync Secure Copy (SCP) uses SSH to copy only the files or directories that you select. On first use, Rsync copies all files and directories and then it copies only the files and directories that you have changed. It does not copy all the files and directories again.

Do symbolic links work both ways?

Yes, a symbolic link is a pointer to another location. This means that any changes you make are in fact updating at the target location.


2 Answers

I knew that it was possible, I just took wrong tool. I did it with rsync

rsync --progress -avhe ssh /usr/local/  XXX.XXX.XXX.XXX:/BackUp/usr/local/
like image 96
Kris_R Avatar answered Oct 16 '22 10:10

Kris_R


I found that the rsync method did not work for me, however I found an alternative that did work on this website (www.docstore.mik.ua/orelly).

Specifically section 7.5.3 of "O'Reilly: SSH: The Secure Shell. The Definitive Guide".

7.5.3. Recursive Copy of Directories

...

Although scp can copy directories, it isn't necessarily the best method. If your directory contains hard links or soft links, they won't be duplicated. Links are copied as plain files (the link targets), and worse, circular directory links cause scp1 to loop indefinitely. (scp2 detects symbolic links and copies their targets instead.) Other types of special files, such as named pipes, also aren't copied correctly.A better solution is to use tar, which handles special files correctly, and send it to the remote machine to be untarred, via SSH:

$ tar cf - /usr/local/bin | ssh server.example.com tar xf -
like image 39
Luke Avatar answered Oct 16 '22 08:10

Luke