Automating SFTP upload (mirror) using lftp
- 1 minutes read - 100 wordsUse case
My web host provider allows to manage files using SFTP. I use this to upload the files created by Hugo.
Script
However the sftp command-line utility does not support to delete directories recursively.
As an alternative lftp can be used. If you want to mirror $localBaseDir/$localDir to directory $remoteDir on host $host:
#!/bin/bash
set -e
set -u
user="user"
host="host"
localBaseDir="base"
localDir="local"
remoteDir="remote"
export LFTP_PASSWORD=$(cat ~/data/passwords/sftp.pw)
lftp --env-password sftp://$user@$host <<EOF
ls
lcd $localBaseDir
mirror -R $localDir $remoteDir
EOF
Password
The password is passed as an environment variable. That’s not perfect security-wise, but better than hard-coding it into the script.