简体   繁体   English

从远程服务器到本地服务器的Sftp文件

[英]Sftp files from Remote server to local server

Sorry if it's too much simple question. 抱歉,这是一个非常简单的问题。 But I am Java developer, no idea of shell scripting. 但是我是Java开发人员,不了解Shell脚本。 I googled, but couldn't find exactly what I am looking for. 我用谷歌搜索,但是找不到我想要的东西。

My requirement 我的要求

  1. Connect to remote server using Sftp [authentication based on pub/pri keys]. 使用Sftp [基于pub / pri密钥的身份验证]连接到远程服务器。 A variable to point to private key file 指向私钥文件的变量
  2. Transfer files with specific extension [.log] to local server folder. 将具有特定扩展名[.log]的文件传输到本地服务器文件夹。 Variable to set remote server path and local folder 设置远程服务器路径和本地文件夹的变量
  3. Rename the transferred file in remote server 重命名远程服务器中传输的文件
  4. Log all the transferred files in a .txt file 将所有传输的文件记录在.txt文件中

Can any one give me shell script for this? 有人可以给我这个shell脚本吗?

This is so far I framed from suggestions. 到目前为止,我尚未提出建议。 Still some questions left on my side ;) 还有一些问题留在我的身边;)

export PRIVKEY=${private_key_path}
export RMTHOST=user@remotehost
export RMTDIR=/logs/*.log
export LOCDIR=/downloaded/logs/
export LOG=sucess.txt

scp -i $PRIVKEY $RMTHOST:$RMTDIR $LOCDIR
for i in 'ls -1 $LOCDIR/*.log'
do
   echo $i >> $LOG
done

ssh $RMTHOST -c "for i in `ls -1 $RMTDIR; do mv /logs/$i /logs/$i.transferred; done"

What about this approach? 那这种方法呢?

  1. Connect to remote server using Sftp [authentication based on pub/pri keys]. 使用Sftp [基于pub / pri密钥的身份验证]连接到远程服务器。 A variable to point to private key file 指向私钥文件的变量
  2. Transfer files with specific extension [.log] to local server folder. 将具有特定扩展名[.log]的文件传输到本地服务器文件夹。 Variable to set remote server path and local folder 设置远程服务器路径和本地文件夹的变量
scp your_user@server:/dir/of/file/*.log /your/local/dir
  1. Log all the transferred files in a .txt file 将所有传输的文件记录在.txt文件中
for file in /your/local/dir/*.log
do
   echo "$file" >> $your_txt
done
  1. Rename the transferred file in remote server 重命名远程服务器中传输的文件
ssh your_user@server -c "for file in /dir/of/file/*.log; do mv /dir/of/file/"$file" /dir/of/file/new_name_based_on"$file"; done"

Use scp(secure copy) command to transfer file. 使用scp(安全复制)命令传输文件。 You may also want to add the -C switch, which compresses the file. 您可能还需要添加-C开关,该开关可压缩文件。 That can speed things up a bit. 这样可以加快速度。 ie copy file1 from server1 to server2, 即将文件1从服务器1复制到服务器2,

On server1: 在server1上:

#!/bin/sh
scp -C /home/user/file1 root@server2.com:/home/user

Edit: 编辑:

#!/bin/sh
scp -i {path/to/pub/pri/key/file} /home/user/file1 root@server2.com:/home/user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM