ssh 循环文件并在本地传输

ssh 循环文件并在本地传输

我通过 ssh 登录到服务器 1,在服务器 1 上,我想获取位于服务器 2 上的 git 存储库中的文件,并将它们保存在服务器 1 上以对它们进行某些操作(保存到数据库的脚本)

git 存储库的结构相当深。我无法直接克隆存储库,因为空间不足。因此,我当然想循环遍历 Server2 上的所有文件,并将每个文件移动到 server1,执行我的功能以保存到数据库,然后删除该文件。

我遇到问题的部分是递归循环通过 ssh 下载文件。

编辑时:我正在两个 redhat 服务器之间进行通信。

答案1

这不是很优雅,但希望这个想法能够被理解:

ssh localhost -p4444 \
 "find ~/tmp/  -iname \*png -exec \
    sh -c 'echo mkdir -p moo-diles/\`dirname {}\`\;\
           rsync -e \"ssh -p 4444\" localhost:{} moo-files/{}\;\
           rm -i {}' \; "

(...)

mkdir -p moo-diles//home/jaroslav/tmp/eterm/usr/share/Eterm/pix/tile; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/eterm/usr/share/Eterm/pix/tile/veins_2.png moo-files//home/jaroslav/tmp/eterm/usr/share/Eterm/pix/tile/veins_2.png; rm -i /home/jaroslav/tmp/eterm/usr/share/Eterm/pix/tile/veins_2.png
mkdir -p moo-diles//home/jaroslav/tmp/eterm/usr/share/Eterm/pix; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/eterm/usr/share/Eterm/pix/menu2.png moo-files//home/jaroslav/tmp/eterm/usr/share/Eterm/pix/menu2.png; rm -i /home/jaroslav/tmp/eterm/usr/share/Eterm/pix/menu2.png
mkdir -p moo-diles//home/jaroslav/tmp/eterm/usr/share/Eterm/pix; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/eterm/usr/share/Eterm/pix/bar_vertical_2.png moo-files//home/jaroslav/tmp/eterm/usr/share/Eterm/pix/bar_vertical_2.png; rm -i /home/jaroslav/tmp/eterm/usr/share/Eterm/pix/bar_vertical_2.png
mkdir -p moo-diles//home/jaroslav/tmp/eterm/usr/share/Eterm/pix; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/eterm/usr/share/Eterm/pix/button_arrow_down_2.png moo-files//home/jaroslav/tmp/eterm/usr/share/Eterm/pix/button_arrow_down_2.png; rm -i /home/jaroslav/tmp/eterm/usr/share/Eterm/pix/button_arrow_down_2.png
mkdir -p moo-diles//home/jaroslav/tmp/eterm/usr/share/Eterm/pix; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/eterm/usr/share/Eterm/pix/button_arrow_up_3.png moo-files//home/jaroslav/tmp/eterm/usr/share/Eterm/pix/button_arrow_up_3.png; rm -i /home/jaroslav/tmp/eterm/usr/share/Eterm/pix/button_arrow_up_3.png
mkdir -p moo-diles//home/jaroslav/tmp/eterm/usr/share/Eterm/pix; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/eterm/usr/share/Eterm/pix/bar_vertical_3.png moo-files//home/jaroslav/tmp/eterm/usr/share/Eterm/pix/bar_vertical_3.png; rm -i /home/jaroslav/tmp/eterm/usr/share/Eterm/pix/bar_vertical_3.png
mkdir -p moo-diles//home/jaroslav/tmp/eterm/usr/share/Eterm/pix; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/eterm/usr/share/Eterm/pix/button_arrow_down_1.png moo-files//home/jaroslav/tmp/eterm/usr/share/Eterm/pix/button_arrow_down_1.png; rm -i /home/jaroslav/tmp/eterm/usr/share/Eterm/pix/button_arrow_down_1.png
mkdir -p moo-diles//home/jaroslav/tmp/transcode; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/transcode/track14.png moo-files//home/jaroslav/tmp/transcode/track14.png; rm -i /home/jaroslav/tmp/transcode/track14.png
mkdir -p moo-diles//home/jaroslav/tmp/transcode; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/transcode/closeup.png moo-files//home/jaroslav/tmp/transcode/closeup.png; rm -i /home/jaroslav/tmp/transcode/closeup.png
mkdir -p moo-diles//home/jaroslav/tmp/transcode; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/transcode/track15.png moo-files//home/jaroslav/tmp/transcode/track15.png; rm -i /home/jaroslav/tmp/transcode/track15.png
mkdir -p moo-diles//home/jaroslav/tmp/transcode; rsync -e ssh -p 4444 localhost:/home/jaroslav/tmp/transcode/track10.png moo-files//home/jaroslav/tmp/transcode/track10.png; rm -i /home/jaroslav/tmp/transcode/track10.png

(...)

这里所做的可以称为“元编程”,因为对于每个找到的文件,我们只需回显更多要执行的命令。这些命令必须在稍后执行才能处理每个文件。明智的做法是将传入的命令sh -c '<here>'放在服务器上的脚本中。这样,您就不会在尝试正确退出并阅读长达三行的命令时失去理智。

相关内容