java lftp_lftp script that will download files

First: Create a script. You can call it whatever you want. I will call it downloader.sh.

#!/bin/bash

PROTOCOL="ftp"

URL="server.example.com"

LOCALDIR="/home/user/downloads"

REMOTEDIR="dir/remote/server/"

USER="user"

PASS="password"

REGEX="*.txt"

LOG="/home/user/script.log"

cd $LOCALDIR

if [ ! $? -eq 0 ]; then

echo "$(date "+%d/%m/%Y-%T") Cant cd to $LOCALDIR. Please make sure this local directory is valid" >> $LOG

fi

lftp $PROTOCOL://$URL <

user $USER "$PASS"

cd $REMOTEDIR

mget -E $REGEX

DOWNLOAD

if [ ! $? -eq 0 ]; then

echo "$(date "+%d/%m/%Y-%T") Cant download files. Make sure the credentials and server information are correct" >> $LOG

fi

Second: Add it to crontab. If you want to execute it every exact 15 minutes inside an hour:

45,30,15,00 * * * * /home/user/downloader.sh >/dev/null 2>&1

If you want to execute it each 15 minutes no matter what is the starting minute:

*/15 * * * * /home/user/downloader.sh >/dev/null 2>&1

Explaining the variables:

PROTOCOL - What protocol to use. lftp supports a good range of them: ftp, ftps, http, https, hftp, fish, sftp and file. https and ftps require lftp to be compiled with OpenSSL or GNU TLS support.

URL- Name or IP of the server. You can even add :PORT at the end if your server doesn't use the default port of the protocol being used.

LOCALDIR - Where to save the files.

REMOTEDIR - Where to cd on the remote server to get the files.

USER and PASSWORD - ftp credentials.

REGEX - Regular expression to filter files to download. It can be useful if you want to download only files of a determined extension, for example. Use * if you want to download everything.

LOG - Logfile location.

Explaining some code logic:

1. - if

if [ ! $? -eq 0 ]; then

fi

The $? variable is a special bash variable that means "status code of last command". Bash always return zero on successful command executions so, comparing -eq (equal to) with the starting !(negative) on an if should be enough to see if cd and lftp had issues during execution. If you want a better log of what happened, you will have to crawl through those commands' documentation.

2. - heredocs

lftp $PROTOCOL://$URL <

DOWNLOAD

bash heredocs. It's a way to say "feed this command with this input list". I've named the limit string DOWNLOAD so, everything between <

3. - lftp commands

user $USER "$PASS"

cd $REMOTEDIR

mget -E $REGEX

These are internal commands of lftp that means respectively, auth the user with $USER login and "$PASS" password, change to $REMOTEDIR and bulk download anything with the $REGEXkeywords. You can learn them by simply typing lftp, and as soon as an lftp shell is opened, type ? and press Enter or ? lftp-command-you-want and press Enter. Example:

[root@host ~]# lftp

lftp :~> ?

! (commands) alias [ []]

attach [PID] bookmark [SUBCMD] cache [SUBCMD]

cat [-b] cd chmod [OPTS] mode file...

close [-a] [re]cls [opts] [path/][pattern] debug [|off] [-o ]

du [options] exit [|bg] get [OPTS] [-o ]

glob [OPTS] help [] history -w file|-r file|-c|-l [cnt]

jobs [-v] [] kill all| lcd

lftp [OPTS] ln [-s] ls []

mget [OPTS] mirror [OPTS] [remote [local]] mkdir [-p]

module name [args] more mput [OPTS]

mrm mv [re]nlist []

open [OPTS] pget [OPTS] [-o ] put [OPTS] [-o ]

pwd [-p] queue [OPTS] [] quote

repeat [OPTS] [delay] [command] rm [-r] [-f] rmdir [-f]

scache [] set [OPT] [ []] site

source torrent [-O

wait [] zcat zmore

lftp :~> ? mget

Usage: mget [OPTS]

Gets selected files with expanded wildcards

-c continue, resume transfer

-d create directories the same as in file names and get the

files into them instead of current directory

-E delete remote files after successful transfer

-a use ascii mode (binary is the default)

-O specifies base directory or URL where files should be placed

The knowledge related to know that mget would be the right command inside lftp came from reading manpages and searching for keywords like "bulk", "multi" or "mass", and knowing that the ftp(1) command also have the mget command so, probably lftp would have an equivalent.

Manpage: lftp(1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值