I actually had a lot of trouble with this but in the end this finally worked:
#!/bin/bash
declare HOST='192.168.0.20 50015'
declare USER=admin
declare PASSWORD=MyPassword123
ftp -nvp $HOST <<EOF
ascii
user $USER $PASSWORD
prompt
cd /custom/remote/directory
prompt
lcd /custom/local/directory
prompt
mget *.mkv
prompt
mdelete *.mkv
prompt
bye
EOF
The above will connect to a camera FTP server on port 50015 then change the remote directory to where the recordings are located. Then it will set my local directory to a location my PLEX server is scanning and the files will be copied to the folder.
To get the script to execute properly I had to run this command on the file (Press Ctrl+V Ctrl+M to insert that ^M
.):
sed -i -e 's/^M$//' scriptname.sh
I also have a cleanup step to delete footage older than 14 days:
#!/bin/bash
find /Custom/Local/Directory/* -mtime +14 -exec rm {} \;
The above will find files older than 14 days and delete them.
To setup a schedule run CronTab:
Crontab -e
For syntax help checkout:
https://ss64.com/bash/crontab.html
References:
FTP file transfer with an automated bash script – Linux.com
Bash script and /bin/bash^M: bad interpreter: No such file or directory – Stack Overflow
https://access.redhat.com/discussions/6487921
Full Guide – Automate FTP Transfers in Linux Shell Scripting (eduonix.com)
c++ – FTP Server Ports in Active Mode and Passive Mode – Stack Overflow
VSFTPD – How to configure a different home folder for each user (ryadel.com)
ftp username and pasword automated in shell script – Stack Overflow
How to Setup vsftpd FTP Server on Debian 10? (linuxhint.com)
Delete Files Older Than x Days on Linux (howtogeek.com)