跳至主要内容

Upload the Entire Directory to an FTP server with curl

· 閱讀時間約 2 分鐘

A while ago I'm working on a project which involves Citrix NetScaler. The Client is required to automate the configuration and back up files to an internal FTP server. Citrix NetScaler cannot regularly upload the backup files to an external store (when no additional software is used). Hence, I decided to write a simple shell script and use curl to upload the entire directory to the FTP server. The script can be used in any Linux machine (or even Windows with WSL) with curl installed.

Here is the script.

REMOTEHOST="x.x.x.x"
USER="xxx"
PASSWD="xxx"
LOCALDIR="/var/ns_sys_backup"
REMOTEDIR="/NetworkBackup/Citrix/${HOSTNAME}"

find $LOCALDIR -type f -exec curl --user $USER:$PASSWD --ftp-create-dirs --upload-file {} ftp://$REMOTEHOST/$REMOTEDIR/ \;

The above script will use the find command to list all the files from the source directory and use curl to upload the files one by one to the destination FTP server. I even included the hostname in the target path. One key part of the script is the use of the --ftp-create-dirs switch with curl as it will create the folder at the destination if not exist. It's a very handy open as it no longer requires me to include the part to first create the directory before uploading files in the script.

Then I use crontab to schedule it to run every night. That is and it's a very simple solution (luckily crontab and curl commands are available in Citrix NetScaler CLI’s shell).