Rsync 没有同步.htaccess 文件

我正在尝试将 server1的目录 A 与 server2的目录 B 同步。

我在 server1的目录 A 中运行以下命令。

rsync -av * server2::sharename/B

但有趣的是,它同步所有的文件和目录,除了。Htaccess 或目录 A 中的任何隐藏文件。子目录中的任何隐藏文件都得到同步。

我还尝试了以下命令:

rsync -av --include=".htaccess" * server2::sharename/B

但结果是一样的。

知道为什么 A 目录中的隐藏文件不能同步吗? 以及如何修复它。我以 root 用户身份运行。

谢谢

71134 次浏览

This is due to the fact that * is by default expanded to all files in the current working directory except the files whose name starts with a dot. Thus, rsync never receives these files as arguments.

You can pass . denoting current working directory to rsync:

rsync -av . server2::sharename/B

This way rsync will look for files to transfer in the current working directory as opposed to looking for them in what * expands to.

Alternatively, you can use the following command to make * expand to all files including those which start with a dot:

shopt -s dotglob

See also shopt manpage.

The * tell to rsynch to not synch hidden files. You should not omit it.

I think the problem is due to shell wildcard expansion. Use . instead of star.

Consider the following example directory content

$ ls -a .
. .. .htaccess a.html z.js

The shell's wildcard expansion translates the argument list that the rsync program gets from

-av * server2::sharename/B

into

-av a.html z.js server2::sharename/B

before the command starts getting executed.

For anyone who's just trying to sync directories between servers (including all hidden files) -- e.g., syncing somedirA on source-server to somedirB on a destination server -- try this:

rsync -avz -e ssh --progress user@source-server:/somedirA/ somedirB/

Note the slashes at the end of both paths. Any other syntax may lead to unexpected results!


Also, for me its easiest to perform rsync commands from the destination server, because it's easier to make sure I've got proper write access (i.e., I might need to add sudo to the command above).

Probably goes without saying, but obviously your remote user also needs read access to somedirA on your source server. :)

I had the same issue.

For me when I did the following command the hidden files did not get rsync'ed

rsync -av /home/user1 server02:/home/user1

But when I added the slashes at the end of the paths, the hidden files were rsync'ed.

rsync -av /home/user1/ server02:/home/user1/

Note the slashes at the end of the paths, as Brian Lacy said the slashes are the key. I don't have the reputation to comment on his post or I would have done that.

On a related note, in case any are coming in from google etc trying to find while rsync is not copying hidden subfolders, I found one additional reason why this can happen and figured I'd pay it forward for the next guy running into the same thing: if you are using the -C option (obviously the --exclude would do it too but I figure that one's a bit easier to spot).

In my case, I had a script that was copying several folders across computers, including a directory with several git projects and I noticed that the I couldn't run any of the normal git commands in the copied repos (yes, normally one should use git clone but this was part of a larger backup that included other things). After looking at the script, I found that it was calling rsync with 7 or 8 options.

After googling didn't turn up any obvious answers, I started going through the switches one by one. After dropping the -C option, it worked correctly. In the case of the script, the -C flag appears to have been added as a mistake, likely because sftp was originally used and -C is a compression-related option under that tool.

per man rsync, the option is described as

--cvs-exclude, -C auto-ignore files in the same way CVS does

Since CVS is an older version control system, and given the man page description, it makes perfect sense that it would behave this way.