-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathftp_upload.sh
More file actions
117 lines (96 loc) · 2.75 KB
/
ftp_upload.sh
File metadata and controls
117 lines (96 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env bash
#
# Upload file(s) to FTP server
#
# Copyright (C) 2016 - 2026 Teddysun <i@teddysun.com>
#
# Argument example:
# 1) ./ftp_upload.sh filename
# 2) ./ftp_upload.sh filename1 filename2 filename3 ...
# 3) ./ftp_upload.sh "*.extension"
# 4) ./ftp_upload.sh "*.extension1" "*.extension2"
#
########## START OF CONFIG ##########
# Local directory (current folder)
LOCALDIR=$(pwd)
# File to log the outcome of uploads
readonly LOGFILE="/var/log/ftp_upload.log"
# FTP server
# Enter the Hostname or IP address below
readonly FTP_HOST=""
# FTP username
# Enter the FTP username below
readonly FTP_USER=""
# FTP password
# Enter the username's password below
readonly FTP_PASS=""
# FTP server remote folder
# Enter the FTP remote folder below
# For example: public_html
readonly 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_command() {
if [ ! "$(command -v "ftp")" ]; then
log "The ftp command is not installed, please install it and try again"
exit 1
fi
}
# Transferring file(s) to FTP server
ftp_upload() {
cd "${LOCALDIR}" || exit 2
[ -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
# Check if files exist (handle wildcards)
local file_count=0
for f in "$@"; do
if [[ "${f}" == *"*"* ]]; then
# Wildcard pattern
if ! ls "${f}" > /dev/null 2>&1; then
log "Error: [${f}] no matching files found!"
exit 3
fi
file_count=$((file_count + 1))
else
if [ ! -f "${f}" ]; then
log "Error: [${f}] not found!"
exit 4
fi
file_count=$((file_count + 1))
fi
done
if [ ${file_count} -eq 0 ]; then
log "Error: no valid file(s) found!"
exit 5
fi
local FTP_OUT_FILE=("$@")
log "Transferring file(s) list below to FTP server:"
for file in "${FTP_OUT_FILE[@]}"
do
log "${file}"
done
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 "Transfer to FTP server completed"
}
# Main process
STARTTIME=$(date +%s)
[ $# -eq 0 ] && log "Error: argument(s) cannot be empty!" && exit 1
check_command
ftp_upload "$@"
ENDTIME=$(date +%s)
DURATION=$((ENDTIME - STARTTIME))
log "All done"
log "Transfer completed in ${DURATION} seconds"