Skip to content

Instantly share code, notes, and snippets.

@aaclarker
Created February 4, 2026 18:10
Show Gist options
  • Select an option

  • Save aaclarker/157530c94c0c46824a44d50a3e6d99ae to your computer and use it in GitHub Desktop.

Select an option

Save aaclarker/157530c94c0c46824a44d50a3e6d99ae to your computer and use it in GitHub Desktop.
LFTP Sync script
#!/usr/bin/env bash
# Setup host config in ~/.ssh/config
# Set the SYNCWATCH_* environment variables
# Run frequently, such as via cron `*/15 * * * * syncwatch.sh`
if [ -f ~/.config/plexbox ]; then
# shellcheck disable=SC1090
source ~/.config/plexbox
fi
if [ -z "$SYNCWATCH_HOST" ] || [ -z "$SYNCWATCH_LFTP_LOG_FILE" ] || [ -z "$SYNCWATCH_REMOTE_DIR" ] || [ -z "$SYNCWATCH_LOCAL_DIR" ]; then
echo "Missing required configuration"
exit 1
fi
trap "rm -f /tmp/syncwatch.lock" SIGINT SIGTERM
if [ -e /tmp/syncwatch.lock ]
then
echo "Syncwatch is running already."
exit 1
else
touch /tmp/syncwatch.lock
lftp "sftp://$SYNCWATCH_HOST" << EOF
set mirror:use-pget-n 10
set mirror:parallel-transfer-count 2
set net:connection-limit 10
set net:max-retries 3
set net:reconnect-interval-base 5
set net:reconnect-interval-multiplier 1.5
set sftp:auto-confirm yes
set sftp:max-packets-in-flight 32
set xfer:use-temp-file yes
set xfer:temp-file-name *.lftp
set xfer:log 1
set xfer:clobber yes
set xfer:buffer-size 4M
mirror --Remove-source-files --parallel=3 --log=$SYNCWATCH_LFTP_LOG_FILE $SYNCWATCH_REMOTE_DIR $SYNCWATCH_LOCAL_DIR
quit
EOF
# Remove empty directories left behind on remote host
ssh "$SYNCWATCH_HOST" "find $SYNCWATCH_REMOTE_DIR -mindepth 1 -type d -empty -delete" 2>/dev/null
rm -f /tmp/syncwatch.lock
trap - SIGINT SIGTERM
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment