Skip to main content

Syncing Files with FTP

I came across a problem when doing migration last time, the server grew too big that I cannot just simply compress the files and move it to another server, it was more than 20GB files. So, I came across an FTP client called LFTP that will synchronize files and folders over FTP. The script below is the script I used to sync the files, let's call it sync.sh:

  1. #!/bin/bash
    HOST='<ftp host>'
    USER='<ftp user>'
    PASS='<password>'
    RCD='<remote directory to sync>'
    lftp -e "
    open $HOST
    user $USER $PASS
    mirror --verbose --continue $RCD
    bye
    "

To sync only certain folders, use the following sciprts:

  1. #!/bin/bash
    HOST='<ftp host>'
    USER='<ftp user>'
    PASS='<password>'
    RCD='<remote directory to sync>'
    lftp -e "
    open $HOST
    user $USER $PASS
    mirror --verbose --continue --exclude '.*' --exclude '.*/' --include '<folder1>' --include '<folder2>' $RCD
    bye
    "

To have sync overnight even when logged out, use the command nohup bash sync.sh > sync.log.