web-dev-qa-db-ja.com

bashスクリプトでlftpを使用してファイルを転送する

サーバーA test-lxとサーバーB test2-lxがあります。ファイルをサーバーAからサーバーBに転送します。ファイルを転送するときは、存在しない場合にのみディレクトリを作成する必要があります。 lftp接続中にディレクトリが存在するかどうかを確認できますか?これを2行で行う代わりに、1つのコマンドで複数のファイルを出力する方法を教えてください。 find -maxdepth 1 -name DirNameを使用するオプションはありますか

これが私のコードです:

lftp -u drop-up,1Q2w3e4R   ftp://ta1bbn01:21 << EOF

cd $desFolder
mkdir test
cd test
put $srcFil
put $srcFile

bye 
EOF
7
Alex Brodov

Ftpによる簡単な方法:

#!/bin/bash

ftp -inv ip << EOF
user username password

cd /home/xxx/xxx/what/you/want/
put what_you_want_to_upload

bye
EOF

Lftpの場合:

#!/bin/bash

lftp -u username,password ip << EOF

cd /home/xxx/xxx/what/you/want/
put what_you_want_to_upload

bye
EOF

Lftpマニュアルから:

-u <user>[,<pass>]  use the user/password for authentication

Mkdirを使用してディレクトリを作成できます。また、次のようにputコマンドを数回使用できます。

put what_you_want_to_upload
put what_you_want_to_upload2
put what_you_want_to_upload3

そして、byeとの接続を閉じることができます


フォルダーが存在するか、または次のようになっていないかを確認できます。

#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test1231")

if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exist"
fi

Lftpマニュアルから:

-c <cmd>            execute the commands and exit

そして、いくつかのファイルを置くために別の接続を開くことができます。


フォルダーが存在するかどうかを確認する方法がわかりませんが、1つの接続でこれを行うことができます。多分あなたはより良い解決策を見つけることができます:

#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test2")

if [ "$checkfolder" == "" ];
then

lftp -u user,pass ip << EOF

mkdir test2
cd test2
put testfile.txt
bye
EOF

else

echo "The directory already exists - exiting"

fi
16
phe

Pheと同じ基本的なコーディングアウトラインを使用しましたが、フォルダーが空の場合、ls/foldernameを使用すると「フォルダーが存在しません」と出力されることがわかりました。これを解決するために私は使用します

#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls | grep /test1231")

if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exists"
fi

これは、フォルダがルートディレクトリにある場合にのみ機能することに注意してください。フォルダ内のサブディレクトリの場合、次のように機能するはずです。

#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; find | grep home/test1/test1231")

if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exists"
fi
1
edf242

まず、クレデンシャルレコードを〜/ .netrcファイルに次のように準備します。

machine site-url-here
login user-login-here
password user-password-here

そのため、スクリプトでこれを使用するためにコマンドラインでパスワードを公開する必要はありません。

次に電話してください:

lftp -e "lftp-command-here" ftps://user-login-here@site-url-here/initial-folder-here/`

私の場合、mget -c * lftpコマンドJava AzureインフラストラクチャでLinuxアプリインスタンスを実行するSpring Bootアプリケーション。すべてのログを取得します。もちろん、コマンドをセミコロンで区切って配置できます。

0
andrej