Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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
put -O "$REMOTE_DIR"/$DIR "$LOCAL_DIR"/uploaded.txt
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)
–
–
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
lftp -u "$USER","$PASSWORD" $HOST <<EOF
cd "$REMOTE_DIR"
put -O "$REMOTE_DIR"/$DIR "$LOCAL_DIR"/uploaded.txt
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
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.
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.