commit c5cf20f25e560741d65bce429fc93be71a675022 Author: root Date: Tue May 3 11:04:47 2022 +0200 script diff --git a/curlFTP.sh b/curlFTP.sh new file mode 100755 index 0000000..de9252d --- /dev/null +++ b/curlFTP.sh @@ -0,0 +1,57 @@ +#!/bin/bash +# fanta 03/May/2022 +# Script sencillo para subir un archivo a un server ftp +# Permite tambien borrar un archivo y descargarlo +# Ejemplos: +# +# Upload a file example: backup-to-ftp.sh -u /var/backups/vzdump-qemu-109-2022_05_03-01_47_35.vma.lzo +# Remove remote file example: backup-to-ftp.sh -r vzdump-qemu-109-2022_05_03-01_47_35.vma.lzo +# Download a file example: backup-to-ftp.sh -d vzdump-qemu-109-2022_05_03-01_47_35.vma.lzo + +user="" +passwd="" +ftpHost="" +mode="$1" # -d -u -r +file="$2" + +function main(){ + checkCurl + checkFTP + checkMode +} + +function checkCurl(){ + [ -z "$(whereis -b curl | awk {'print$2'})" ] && echo "curl - Maybe it is not installed on the system. Sorry but I can't continue." && exit +} + +function checkFTP(){ + ftpProtocol=$(curl -V | grep -i "Protocols" | tr " " "\n" | grep -w ftp) + if [ $ftpProtocol != "ftp" ]; then echo "Please compile curl with ftp protocol support"; exit; fi +} + +function checkMode(){ + [ -z "$mode" ] && echo -e "Please specifies an option: -d -u -r\n -d download a file\n -u upload a file\n -r remove a file" && exit + if [ $mode == "-u" ] ; then checkFile; fi + if [ $mode == "-u" ] ; then uploadFile ; fi + if [ $mode == "-r" ] ; then removeRemoteFile ; fi + if [ $mode == "-d" ] ; then downloadFile ; fi +} + +function checkFile(){ + [ -z "$file" ] && echo -e "Please specifies a filename." && exit + if [ -f "$file" ]; then echo "File: $file"; else echo "i can't find the file [ $file ] in that path"; exit; fi +} + +function uploadFile(){ + curl -T "$file" ftp://"$user":"$passwd"@"$ftpHost"/ +} + +function downloadFile(){ + curl --output $file ftp://"$user":"$passwd"@"$ftpHost"/"$file" +} + +function removeRemoteFile(){ + curl -u "$user:$passwd" --quote "DELE $file" ftp://"$ftpHost" +} + +main