From 103958d102b033f7dd81f9795dc64bb26459b359 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Tue, 29 Nov 2016 16:59:32 +0900 Subject: [PATCH] Add ftp_upload.sh --- ftp_upload.sh | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 ftp_upload.sh diff --git a/ftp_upload.sh b/ftp_upload.sh new file mode 100644 index 0000000..d2f56d8 --- /dev/null +++ b/ftp_upload.sh @@ -0,0 +1,105 @@ +#!/usr/bin/env bash +# +# Auto upload file(s) to FTP server +# +# Copyright (C) 2016 Teddysun +# +# Argument example: +# 1) ./ftp_upload.sh filename +# 2) ./ftp_upload.sh filename1 filename2 filename3 ... +# 3) ./ftp_upload.sh *.gz +# + +########## START OF CONFIG ########## + +# Local directory (current folder) +LOCALDIR=$( pwd ) + +# File to log the outcome of backups +LOGFILE="/var/log/ftp_upload.log" + +# FTP server +# Enter the Hostname or IP address below +FTP_HOST="" + +# FTP username +# Enter the FTP username below +FTP_USER="" + +# FTP password +# Enter the username's password below +FTP_PASS="" + +# FTP server remote folder +# Enter the FTP remote folder below +# For example: public_html +FTP_DIR="" + +########## END OF CONFIG ########## + + +log() { + echo "$(date "+%Y-%m-%d %H:%M:%S")" "$1" + echo -e "$(date "+%Y-%m-%d %H:%M:%S")" "$1" >> ${LOGFILE} +} + +# Check ftp command +check_commands() { + # Iterate over the list of binaries, and if one isn't found, abort + if [ ! "$(command -v "ftp")" ]; then + log "ftp command is not installed, please install it and try again" + exit 1 + fi +} + +# Tranferring backup file to FTP server +ftp_upload() { + cd ${LOCALDIR} || exit + + [ -z ${FTP_HOST} ] && log "Error: FTP_HOST can not be empty!" && exit 1 + [ -z ${FTP_USER} ] && log "Error: FTP_USER can not be empty!" && exit 1 + [ -z ${FTP_PASS} ] && log "Error: FTP_PASS can not be empty!" && exit 1 + [ -z ${FTP_DIR} ] && log "Error: FTP_DIR can not be empty!" && exit 1 + + local FTP_OUT_FILE="$1" + echo "${FTP_OUT_FILE}" | grep "*" 2>&1 > /dev/null + if [ $? -eq 0 ]; then + ls ${FTP_OUT_FILE} 2>&1 > /dev/null + [ $? -ne 0 ] && log "Error: [${FTP_OUT_FILE}] file(s) not exists!" && exit 1 + else + [ ! -f ${FTP_OUT_FILE} ] && log "Error: [${FTP_OUT_FILE}] not exists!" && exit 1 + fi + + log "Tranferring [${FTP_OUT_FILE}] to FTP server" + ftp -in ${FTP_HOST} 2>&1 >> ${LOGFILE} <