You are not logged in.

#1 2010-04-28 21:14:42

gdirty
Member
Registered: 2008-09-10
Posts: 16

cp question

So in backing up a server a couple months ago, I managed to lose a website because the folder it was under was only a symlink.  I used simple cp -r.  After doing that, I read the man pages for cp a few dozen times and I'm confused about how it deals with symlinks.
Mainly, I read that -a will work recursively, not folow symbolic links and "preserve" everything including links. 
What are some good examples using ln and cp to demonstrate the different behaviors of cp so I understand this right?

Offline

#2 2010-04-28 21:40:25

grey
Member
From: Europe
Registered: 2007-08-23
Posts: 679

Re: cp question

[~]$ mkdir delta
[~]$ cd delta/
[delta]$ touch a
[delta]$ ln -s a b
[delta]$ cd ..
[~]$ cp -r delta/ d_r
[~]$ cp -a delta/ d_a
[~]$ ls -l delta/
total 0
-rw-r--r-- 1 grey grey 0 Apr 28 22:33 a
lrwxrwxrwx 1 grey grey 1 Apr 28 22:33 b -> a
[~]$ ls -l d_a
total 0
-rw-r--r-- 1 grey grey 0 Apr 28 22:33 a
lrwxrwxrwx 1 grey grey 1 Apr 28 22:33 b -> a
[~]$ ls -l d_r
total 0
-rw-r--r-- 1 grey grey 0 Apr 28 22:34 a
lrwxrwxrwx 1 grey grey 1 Apr 28 22:34 b -> a
[~]$ cp -LR delta/ d_LR
[~]$ ls -l d_LR
total 0
-rw-r--r-- 1 grey grey 0 Apr 28 22:36 a
-rw-r--r-- 1 grey grey 0 Apr 28 22:36 b

Good ideas do not need lots of lies told about them in order to gain public acceptance.

Offline

#3 2010-04-28 21:41:35

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: cp question

> mkdir -p a/d
> touch a/d/t
> ls a
d/
> ls a/d
t
> mkdir b
> cd b
> ln -s ../a/d e
> cd ..
> ls b
e@
> ls b/e/
t
> cp -a b c
> ls c
e@
> rm -rf c
> cp -aL b c
> ls c
e/

Edit: sad

Last edited by fsckd (2010-04-28 21:42:23)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#4 2010-04-28 21:46:57

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: cp question

cp -L will follow symbolic links.  I would advise against using it because there are some (actually, many) situations where dereferencing symlinks destroys the purpose of the symlink.  Instead, make sure that everywhere you keep stuff is backed up (at once or separately) and back up the symlinks as symlinks.

cp -a (--archive) recurses into subdirectories, like -r, but also includes --preserve=all, which preserves not only links (which seems to be default behavior) but timestamps, ownership, and permissions.

If you're looking for a simple backup solution somewhat more sophisticated than cp, investigate rsync, which can eliminate multiple backups of the same file and accepts some of the same options.

Offline

#5 2010-04-28 21:52:44

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: cp question

I don't know how good of an example this is, but it might help. Create a new directory with a symlink to the pacman package in your cache:

mkdir foo
ln -s /var/cache/pacman/pkg/pacman-3.3.3-5-x86_64.pkg.tar.gz foo/pacman
ls -l foo/
total 0
lrwxrwxrwx 1 xyne users 54 Jan 01 01:01 pacman -> /var/cache/pacman/pkg/pacman-3.3.3-5-x86_64.pkg.tar.gz

The "pacman" file in "foo" is a reference to the package file in your cache. Think of it as a sign. If someone comes looking for the file, they find the sign which directs them to another location where they can find the file (/var/cache/pacman/pkg/pacman-3.3.3-5-x86_64.pkg.tar.gz in this case).

Now let's copy that directory, first without dereferencing and then with.

Without dereferencing:

cp -R --no-dereference foo/ bar/
ls -l bar/
total 0
lrwxrwxrwx 1 xyne users 54 Jan 01 01:01 pacman -> /var/cache/pacman/pkg/pacman-3.3.3-5-x86_64.pkg.tar.gz

The file is still a symlink because we told "cp" not to dereference it. Using the sign analogy, telling it not to dereference symlinks would be telling it not to follow any signs but instead take the signs themselves.



With dereferencing:

rm bar/ -fr
cp -R --dereference foo/ bar/
ls -l bar/
total 0
-rw-r--r-- 1 xyne users 652947 Jan 01 01:01 pacman

With dereferencing we get the file that it pointed to (referenced). Using the sign analogy, this tells it follow any signs that it finds and take whatever they point to.

In your case it seems that you had a symlink to a folder that was located outside of the directory that you copied. Be default "cp" does not dereference so you only copied the symlink to the website directory that you wanted to back up. A symlink doesn't know anything about the file that it references, just as a sign doesn't know whether the town it points has been destroyed by a natural disaster.

Dereferencing should be used when the actual files are outside of the directory structure that you want to copy. Referencing can be useful when you have symlinks nested within the directory hierarchy that you want to preserve (e.g. a projects directory with symlinks in each project subdirectoy that point to a common license file, which is also in the project directory).


You should also take a look at "rsync"|"grsync" for backing up files. It's more versatile than "cp".

Last edited by Xyne (2010-04-28 21:54:00)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#6 2010-04-29 03:15:36

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: cp question

May I (dis)respectfully steal Alan Cox's backup-script:

(cd $source && tar cf - .) | (cd $target && tar xvpf -)

But of course - nothing can beat 'rsync' ... which only takes _changed_ (or newer) files ...

rsync -av --delete $source/ $target

Last edited by perbh (2010-04-29 03:18:09)

Offline

Board footer

Powered by FluxBB