Gentoo Archives: gentoo-user

From: Renat Golubchyk <ragermany@×××.net>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] How to exclude a directory from rsync
Date: Sun, 28 Nov 2010 13:38:48
Message-Id: 20101128143545.363a6977@mating-tux
In Reply to: Re: [gentoo-user] How to exclude a directory from rsync by Helmut Jarausch
1 On Fri, 26 Nov 2010 09:02:49 +0100 Helmut Jarausch
2 <jarausch@××××××××××××××××.de> wrote:
3 > On 11/25/10 22:51:36, Renat Golubchyk wrote:
4 > > The man page is actually pretty clear on this issue. Quote:
5 > >
6 > > if the pattern starts with a / then it is anchored to a particular
7 > > spot in the hierarchy of files, otherwise it is matched against
8 > > the
9 > > end of the pathname. This is similar to a leading ^ in regular
10 > > expressions. Thus "/foo" would match a name of "foo" at either
11 > > the "root of the transfer" (for a global rule) or in the
12 > > merge-file’s directory (for a per-directory rule). An
13 > > unqualified "foo" would
14 > > match a name of "foo" anywhere in the tree because the
15 > > algorithm is
16 > > applied recursively from the top down; it behaves as if each path
17 > > component gets a turn at being the end of the filename. Even the
18 > > unanchored "sub/foo" would match at any point in the
19 > > hierarchy where a "foo" was found within a directory named "sub".
20 > >
21 > > "Root of the transfer" is the directory you want to sync. Thus, if
22 > > you
23 > > run e.g. "rsync /var/log/ /mnt/backups/ --exclude=/portage/" then
24 > > root
25 > > of the transfer is /var/log, and therefore the directory
26 > > /var/log/portage will be excluded. If on the other hand you write
27 > > --exclude=portage/ then a directory named portage anywhere in the
28 > > tree
29 > > under /var/log will be excluded. Without the trailing slash, i.e.
30 > > just
31 > > --exclude=portage any file (regular file, directory, link, whatever)
32 > > named portage anywhere in the tree gets excluded. And finally
33 > > --exclude=/portage would exclude a file only at the top of the tree
34 > > that
35 > > is going to be synchronsed.
36 > >
37 >
38 > Let me add some caveat which has trapped me recently.
39 >
40 > I had (in your terms)
41 > rsync /var/log/ /mnt/backups/ --delete --exclude=/portage/
42 >
43 > and /var/log/portage was just a symlink to some other directory
44 > while /mnt/backups/portage was a real directory.
45 > In that case rsync deletes /mnt/backups/portage !
46 > It looks as if the "directory property" is check in the source tree
47 > only. Quite an unpleasant surprise.
48
49 The behavior is logical and consistent with the command you provided.
50 If you tell rsync to synchronise two directories and delete everything
51 in the destination that is not in the source then rsync does just that.
52 And a symbolic link is not a directory. One might think that exclude
53 rule applies to both source and destination, but it doesn't. That's a
54 common pitfall. But the documentation is clear on that too. It says,
55
56 "This option allows you to add rules to selectively exclude certain
57 files from the list of files to be transferred."
58
59 Since destination directory is not going to be transferred it is clear
60 that this option doesn't apply there.
61
62 In order to avoid the problem of files being accidentally deleted I
63 just never run rsync without running it with --dry-run first if I
64 specify a --delete option. Thus I have an opportunity to review the
65 file list before anything unpleasant is done.
66
67
68 Cheers,
69 Renat
70
71 --
72 Probleme kann man niemals mit derselben Denkweise loesen,
73 durch die sie entstanden sind.
74 (Einstein)

Attachments

File name MIME type
signature.asc application/pgp-signature