rsync从远端到远端
环境:rsync中间机,源机host1,目标机host2
方案1. 先挂载
对于rsync来说,先挂载再拷贝有点多余,反正也是一个方案
注:sshfs需要从epel源中下载(当然nfs,ftp,等挂载也行)
例:
$ mkdir host1 host2 $ sshfs user1@host1:/path1 ./host1 $ sshfs user2@host2:/path2 ./host2 $ rsync -a ./host1/ ./host2/
方案2:一个挂载,一个rsync
算是一个,常用方案
将远端或目标端挂载到中间机,上再进行rsync拷贝
mount -t nfs host2_ip:/tmp/share /mnt/rsync rsync -avzr --partial --progress --checksum --log-file=/var/log/rsync.log root@host1_ip:/share /mnt/rsync
方案3:远端到远端
利用ssh隧道技术,让rsync通过隧道直接连接
ssh -R 50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path'
脚本示例1
SOURCE_USER=user1 SOURCE_HOST=hostname1 SOURCE_PATH=path1 TARGET_USER=user2 TARGET_HOST=host2 TARGET_PATH=path2 ssh -l $TARGET_USER -A -R localhost:22000:$TARGET_HOST:22 \ $SOURCE_USER@$SOURCE_HOST "rsync -e 'ssh -p 22000' -vuar $SOURCE_PATH \ $TARGET_USER@localhost:$TARGET_PATH"
脚本示例2
echo -n "Test: ssh to $SOURCE_HOST: " ssh $SOURCE_HOST echo PASSED| grep PASSED || exit 3 echo -n "Verifying path in $SOURCE_HOST " ssh $SOURCE_HOST stat $SOURCE_PATH | grep "File:" || exit 5 echo -n "Verifying path in $TARGET_HOST " ssh $TARGET_HOST stat $TARGET_PATH | grep "File:" || exit 5 echo "configuring ssh from $SOURCE_HOST to $TARGET_HOST via locahost" ssh $SOURCE_HOST "echo \"Host tmpsshrs; ControlMaster auto; ControlPath /tmp/%u_%r@%h:%p; hostname localhost; port $FREE_PORT; user $TARGET_USER\" | tr ';' '\n' > /tmp/tmpsshrs" # The ssh options that will setup the tunnel TUNNEL="-R localhost:$FREE_PORT:$TARGET_ADDR_PORT" echo echo -n "Test: ssh to $SOURCE_HOST then to $TARGET_HOST: " if ! ssh -A $TUNNEL $SOURCE_HOST "ssh -A -F /tmp/tmpsshrs tmpsshrs echo PASSED" | grep PASSED ; then echo echo "Direct authentication failed, will use plan #B:" echo "Please open another terminal, execute the following command" echo "and leave the session running until rsync finishes" echo "(if you're asked for password use the one for $TARGET_USER@$TARGET_HOST)" echo " ssh -t -A $TUNNEL $SOURCE_HOST ssh -F /tmp/tmpsshrs tmpsshrs" read -p "Press [Enter] when done..." fi echo "Starting rsync" ssh -A $TUNNEL $SOURCE_HOST "rsync -e 'ssh -F /tmp/tmpsshrs' $RSYNC_OPTS $SOURCE_PATH tmpsshrs:$TARGET_PATH" echo echo "Cleaning up" ssh $SOURCE_HOST "rm /tmp/tmpsshrs"