VPS

Importing files over FTP

You can connect to a remote machine over FTP to download all of its contents, for example to migrate a website or retrieve a backup to your VPS.

To do so we use lftp and its mirror command, which recursively replicates the remote content into a local directory.

If you do not have lftp installed: apt install lftp -y (Debian/Ubuntu) or yum install lftp -y (RHEL/CentOS).

This is the script (replace the host, login, password and destination values with your own):

#!/bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"

FTP_HOST='Remote.Server.IP'
FTP_USER='Login'
FTP_PASS='Pass'

BackupDestination='/home/backup'

lftp -e "set ftp:ssl-allow no" << EOF
open ${FTP_HOST}
user ${FTP_USER} ${FTP_PASS}
mirror -e . ${BackupDestination}
bye
EOF

Notes:

  • mirror -e deletes files in the destination that no longer exist at the source (exact mirror). Remove the -e flag if you prefer to keep what has already been downloaded.
  • set ftp:ssl-allow no disables TLS; remove it if the remote server supports FTPS so the transfer is encrypted.
  • You can save the script (e.g. import-ftp.sh), make it executable with chmod +x import-ftp.sh and run it inside screen or tmux if the download will take a while.