Reputation: 15
I'm trying to use SFTP to copy some files from one server to another, this task should run everyweek. The script I use :
HOST='sftp://my.server.com'
USER='user1'
PASSWORD='passwd'
DIR=$HOSTNAME
REMOTE_DIR='/home/remote'
LOCAL_DIR='/home/local'
# LFTP via SFTP connexion
lftp -u "$USER","$PASSWORD" $HOST <<EOF
# changing directory
cd "$REMOTE_DIR"
$(if [ ! -d "$DIR" ]; then
mkdir $DIR
fi)
put -O "$REMOTE_DIR"/$DIR "$LOCAL_DIR"/uploaded.txt
EOF
My issue is that put is executed without taking in consideration the result of if statment.
PS : The error message I got is the following :
put: Access failed: No such file (/home/backups/myhost/upload.txt)
Upvotes: 0
Views: 2185
Reputation: 23814
LFTP has no if
statement!
What you are doing here?
lftp -u "$USER","$PASSWORD" $HOST <<EOF cd "$REMOTE_DIR" $(if [ ! -d "$DIR" ]; then mkdir $DIR fi) put -O "$REMOTE_DIR"/$DIR "$LOCAL_DIR"/uploaded.txt EOF
You call a sub command in a here document. The sub command is executed locally before lftp
is started and its output is pasted in the here document, which gets passed to lftp
. This works just, because mkdir
has no output. You do not call mkdir
on the ftp server. You call the mkdir
of your local shell. Effectively it is the same as if you put the if
statement before the lftp
execution.
if [ ! -d "$DIR" ]; then
mkdir $DIR
fi
lftp -u "$USER","$PASSWORD" $HOST <<EOF
cd "$REMOTE_DIR"
put -O "$REMOTE_DIR"/$DIR "$LOCAL_DIR"/uploaded.txt
EOF
What you are trying to do, does not work. You have to think about a different solution.
Right now I have no FTP server to test it, but it might be possible to use the -f
option of LFTP's mkdir
. I assume that it may work like the -f
option of the Unix rm
command. Try this:
lftp -u "$USER","$PASSWORD" $HOST <<EOF
cd "$REMOTE_DIR"
mkdir -f "$DIR"
put -O "$REMOTE_DIR"/$DIR "$LOCAL_DIR"/uploaded.txt
EOF
Update: It works as supposed. The creation of a directory, which exist already, throws no error, if you use the option -f
:
lftp anonymous@localhost:/pub> mkdir -f dir
mkdir ok, `dir' created
lftp anonymous@localhost:/pub> mkdir -f dir
lftp anonymous@localhost:/pub> ls
drwx------ 2 116 122 4096 Aug 10 12:04 dir
Maybe you lftp
client is outdated. I tested it with Debian 9.
Upvotes: 1