我想rsync
从本地计算机到服务器。在一个不存在的目录上,我想rsync
先在服务器上创建该目录。
我怎样才能做到这一点?
如果您有多个要创建的叶目录,您可以ssh ... mkdir -p
先运行一个单独的目录,或者使用此处解释--rsync-path
的技巧:
rsync -a --rsync-path="mkdir -p /tmp/x/y/z/ && rsync" $source user@remote:/tmp/x/y/z/
或使用--relative
Tony 建议的选项。在这种情况下,您只需指定必须存在的目标的根目录,而不是将创建的源目录结构:
rsync -a --relative /new/x/y/z/ user@remote:/pre_existing/dir/
这样,您将得到 /pre_existing/dir/new/x/y/z/
如果你想创建“y/z/”,但不在“new/x/”中,你可以添加./
你想要--relative
开始的地方:
rsync -a --relative /new/x/./y/z/ user@remote:/pre_existing/dir/
将创建 /pre_existing/dir/y/z/。
假设你使用 ssh 连接 rsync,那么之前发送一个 ssh 命令怎么样:
ssh user@server mkdir -p existingdir/newdir
如果它已经存在,则什么都不会发生
该-R, --relative
选项将执行此操作。
例如:如果您想/var/named/chroot
在远程服务器上备份和创建相同的目录结构,那么-R
就可以这样做。
从rsync
手册页(man rsync
):
--mkpath create the destination's path component
这对我有用:
rsync /dev/null node:existing-dir/new-dir/
我确实收到了这条消息:
skipping non-regular file "null"
但我不必担心会有一个空目录。
我不认为你可以用一个 rsync 命令来做到这一点,但你可以先“预先创建”额外的目录,如下所示:
rsync --recursive emptydir/ destination/newdir
其中 'emptydir' 是本地空目录(您可能必须先将其创建为临时目录)。
这有点像黑客,但它对我有用。
干杯
克里斯
这个答案使用了其他一些答案,但希望它会更清楚地了解情况。您从未指定要同步的内容 - 单个目录条目或多个文件。
因此,假设您正在移动一个源目录条目,而不仅仅是移动其中包含的文件。
假设您有一个本地调用的目录,data/myappdata/
并且在它下面有很多子目录。您data/
在目标机器上有但没有data/myappdata/
- 这很容易:
rsync -rvv /path/to/data/myappdata/ user@host:/remote/path/to/data/myappdata
您甚至可以为远程目录使用不同的名称:
rsync -rvv --recursive /path/to/data/myappdata user@host:/remote/path/to/data/newdirname
如果您只是移动一些文件而不移动包含它们的目录条目,那么您将执行以下操作:
rsync -rvv /path/to/data/myappdata/*.txt user@host:/remote/path/to/data/myappdata/
它将myappdata
在远程计算机上为您创建目录以放置您的文件。同样,该data/
目录必须存在于远程计算机上。
顺便说一句,我使用-rvv
flag 是为了获得双倍详细的输出,因此很清楚它的作用以及必要的递归行为。
只是为了向您展示我在使用 rsync(Ubuntu 12.04 上的 3.0.9)时得到的结果
$ rsync -rvv *.txt user@remote.machine:/tmp/newdir/
opening connection using: ssh -l user remote.machine rsync --server -vvre.iLsf . /tmp/newdir/
user@remote.machine's password:
sending incremental file list
created directory /tmp/newdir
delta-transmission enabled
bar.txt
foo.txt
total: matches=0 hash_hits=0 false_alarms=0 data=0
希望这能澄清一点。
例如:
来自:/xxx/a/b/c/d/e/1.html
到:user@remote:/pre_existing/dir/b/c/d/e/1.html
同步:
cd /xxx/a/ && rsync -auvR b/c/d/e/ user@remote:/pre_existing/dir/
使用 rsync 两次~
1:传输临时文件,确保已创建远程相对目录。
tempfile=/Users/temp/Dir0/Dir1/Dir2/temp.txt
# Dir0/Dir1/Dir2/ is directory that wanted.
rsync -aq /Users/temp/ rsync://remote
2:然后可以指定传输文件/目录的远程目录
tempfile|dir=/Users/XX/data|/Users/XX/data/
rsync -avc /Users/XX/data rsync://remote/Dir0/Dir1/Dir2
# Tips: [SRC] with/without '/' is different
rsync source.pdf user1@192.168.56.100:~/not-created/target.pdf
如果完全指定了目标文件,~/not-created
则不会创建目录。
rsync source.pdf user1@192.168.56.100:~/will-be-created/
但是目标只指定了一个目录,该目录~/will-be-created
被创建。/
必须遵循让 rsync 知道will-be-created
是一个目录。
这会在目标中创建目录树/usr/local/bin
,然后递归同步所有包含的文件和文件夹:
rsync --archive --include="/usr" --include="/usr/local" --include="/usr/local/bin" --include="/usr/local/bin/**" --exclude="*" user@remote:/ /home/user
与 相比mkdir -p
,目录树甚至具有与源相同的权限。
如果您使用的版本或 rsync 没有“mkpath”,那么 --files-from 可以提供帮助。假设您需要在目标目录中创建“mysubdir”
创建“filelist.txt”以包含 mysubdir/dummy
mkdir -p source_dir/mysubdir/
touch source_dir/mysubdir/dummy
rsync --files-from='filelist.txt' source_dir target_dir
rsync 会将 mysubdir/dummy 复制到 target_dir,在此过程中创建 mysubdir。在 Raspberry Pi OS (debian) 上使用 rsync 3.1.3 进行测试。