问题描述
我不确定这是否可行。我已经摆弄MWE已有一段时间了。
我在相对模式(-R)中使用rsync将远程目录从服务器复制到本地计算机上。远程目录可能具有符号链接。有时,符号链接指向相对目录的范围内,有时指向外部。
当链接指向相对目录内部时,我只想复制链接(因为文件已经同步,因此这些链接应该可以解析)。
当链接指向相对目录之外时,我想复制文件本身,因为否则链接可能无法解析。
我已经设置了MWE,以测试执行此操作的各种方式:
TEST_BASE=rsync_test
REMOTE_dpath=$HOME/tmp/rsync-test/remote
LOCAL_dpath=$HOME/tmp/rsync-test/local
REMOTE_URI=$REMOTE_dpath
REMOTE_MOUNT=$REMOTE_dpath
reset_rsync_test_remote()
{
# Clean
if [ -d "$REMOTE_dpath" ]; then
rm -rf $REMOTE_dpath
fi
mkdir -p $REMOTE_dpath
# Setup remote data
mkdir -p $REMOTE_dpath/$TEST_BASE/root/dir_L0_X0_A
mkdir -p $REMOTE_dpath/$TEST_BASE/root/dir_L0_X0_A/dir_L1_X0_B
mkdir -p $REMOTE_dpath/$TEST_BASE/root/dir_L0_X1_C
mkdir -p $REMOTE_dpath/$TEST_BASE/root/inside_dir
mkdir -p $REMOTE_dpath/$TEST_BASE/root/links
mkdir -p $REMOTE_dpath/$TEST_BASE/outside_dir/
touch $REMOTE_dpath/$TEST_BASE/root/file_L0_X0_a.txt
touch $REMOTE_dpath/$TEST_BASE/root/dir_L0_X0_A/file_L1_X0_b.txt
touch $REMOTE_dpath/$TEST_BASE/root/dir_L0_X1_C/file_L1_X0_c.txt
touch $REMOTE_dpath/$TEST_BASE/root/inside_dir/inside_file.txt
touch $REMOTE_dpath/$TEST_BASE/outside_dir/outside_file.txt
# Create links to inside and outside the sync root
ln -s $REMOTE_dpath/$TEST_BASE/root/inside_dir/inside_file.txt $REMOTE_dpath/$TEST_BASE/root/links/inside_flink.txt
ln -s $REMOTE_dpath/$TEST_BASE/outside_dir/outside_file.txt $REMOTE_dpath/$TEST_BASE/root/links/outside_flink.txt
ln -s $REMOTE_dpath/$TEST_BASE/outside_dir $REMOTE_dpath/$TEST_BASE/root/links/outside_dlink
ln -s $REMOTE_dpath/$TEST_BASE/root/inside_dir $REMOTE_dpath/$TEST_BASE/root/links/inside_dlink
ln -sr $REMOTE_dpath/$TEST_BASE/root/inside_dir/inside_file.txt $REMOTE_dpath/$TEST_BASE/root/links/rel_inside_flink.txt
ln -sr $REMOTE_dpath/$TEST_BASE/outside_dir/outside_file.txt $REMOTE_dpath/$TEST_BASE/root/links/rel_outside_flink.txt
ln -sr $REMOTE_dpath/$TEST_BASE/outside_dir $REMOTE_dpath/$TEST_BASE/root/links/rel_outside_dlink
ln -sr $REMOTE_dpath/$TEST_BASE/root/inside_dir $REMOTE_dpath/$TEST_BASE/root/links/rel_inside_dlink
tree $REMOTE_dpath/
}
reset_rsync_test_local(){
# Setup home data
echo "LOCAL_dpath = $LOCAL_dpath"
if [ -d "$LOCAL_dpath" ]; then
rm -rf $LOCAL_dpath
fi
mkdir -p $LOCAL_dpath
mkdir -p $LOCAL_dpath/$TEST_BASE
# Make an existing link on the destination that we will sync to
mkdir -p $LOCAL_dpath/link-dest1
mkdir -p $LOCAL_dpath/link-dest2
ln -s $LOCAL_dpath/link-dest1 $LOCAL_dpath/rsync_test-link
ln -s $LOCAL_dpath/link-dest2 $LOCAL_dpath/rsync_test-link/root
tree $LOCAL_dpath
}
reset_rsync_test_remote
这将设置我的假“远程”目录,如下所示:
我们要同步的相对目录是“ root”。
请注意底部的链接。其中一些指向“根”内部(在这种情况下,其名称为inside_
),某些指向“根”外部(在此情况下,其名称为outside_
)。那么其中一半是绝对链接,另一半是相对链接(rel_
前缀)。列举了我在这里关注的所有8种可能性。
我将“根”目录重新同步到“本地”计算机上符号链接中的现有符号链接,以模拟我的正常用例。这没什么大不了的,只是意味着我们在运行rsync时必须指定(-K)。
到目前为止,我有两种方法可以大致完成我想做的事情,但是每种方法都有缺陷。在第一种方法中,我使用(-L)来简单解析所有指向其硬文件的链接。
# Method 1 with -KL
# The -K is important when syncing to a destination dir that is a symlink
# The -L will resolve any symlinks
# this grabs everything however,all files will be copied over as hard files
reset_rsync_test_local
rsync -avPRKL $REMOTE_URI/$TEST_BASE/./root $LOCAL_dpath/rsync_test-link
ls -al $LOCAL_dpath/
ls -al $LOCAL_dpath/rsync_test-link/
tree $LOCAL_dpath/link-dest2
方法2越来越近,因为它至少链接了rel_inside_flink
,但没有链接rel_inside_dlink
,并且无法解析rel_outside_dlink
。
# Method 2: with -Kk --copy-unsafe-links
# Alternatively using -k --copy-unsafe-links will get almost everything
# links inside the relative directory are copied as links,links outside
# the relative dir are copied as files,except relative outside files for
# whatever reason.
reset_rsync_test_local
rsync -avPRKk --copy-unsafe-links $REMOTE_URI/$TEST_BASE/./root $LOCAL_dpath/rsync_test-link
ls -al $LOCAL_dpath/
ls -al $LOCAL_dpath/rsync_test-link/
tree $LOCAL_dpath/link-dest2
我想要的是有关如何完成以下任一操作的信息:
比我的情况更好: 我想要的是任何rel_inside链接都可以直接复制为链接,而所有其他链接都可以解析并复制为文件。
在绝对理想的情况下:它将指向相对目录内的绝对链接转换为目标位置上的相对链接或绝对链接(例如inside_dlink
会将其自身转换为../inside_dir
或{{ 1}})。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)