across/ftp_upload.sh

101 lines
2.2 KiB
Bash
Raw Normal View History

2016-11-29 16:59:32 +09:00
#!/usr/bin/env bash
#
2016-11-29 18:48:21 +09:00
# Upload file(s) to FTP server
2016-11-29 16:59:32 +09:00
#
# Copyright (C) 2016 Teddysun <i@teddysun.com>
#
# Argument example:
# 1) ./ftp_upload.sh filename
# 2) ./ftp_upload.sh filename1 filename2 filename3 ...
2016-11-29 18:48:21 +09:00
# 3) ./ftp_upload.sh *.extension
2016-11-29 16:59:32 +09:00
#
########## 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
2016-11-29 17:01:23 +09:00
check_command() {
2016-11-29 16:59:32 +09:00
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
2016-11-29 18:48:21 +09:00
ls ${FTP_OUT_FILE} 2>/dev/null
2016-11-29 16:59:32 +09:00
[ $? -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} <<EOF
user $FTP_USER $FTP_PASS
binary
lcd $LOCALDIR
cd $FTP_DIR
mput $FTP_OUT_FILE
quit
EOF
log "Tranfer completed"
}
# Main progress
STARTTIME=$(date +%s)
[ $# -eq 0 ] && log "Error: argument can not be empty!" && exit 1
2016-11-29 17:01:23 +09:00
check_command
2016-11-29 16:59:32 +09:00
for i in $@
do
ftp_upload $i
done
ENDTIME=$(date +%s)
DURATION=$((ENDTIME - STARTTIME))
log "All done"
log "Transfer completed in ${DURATION} seconds"