This page looks best with JavaScript enabled

FTP script

FTP script

  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
#!/bin/bash
NOW=$(date '+%Y/%m/%d %A %H:%M:%S')
PURPOSE="PURPOSE"

##### FTP VAR #####

FTP_INFO="user@ip"

##### TARGET_DIR VAR #####

TARGET_DIR="TARGET_DIR/"

##### SOURCE DIR VAR #####

SRC_DIR="SRC_DIR/"

#########
# check #
#########
TARGET_DIR="TARGET_DIR/"
mkdir -p $TARGET_DIR
SRC_DIR="SRC_DIR/"
mkdir -p $SRC_DIR

##### LOG VAR #####

LOG_DIR=${SRC_DIR}
LOG_FN_ONCE=${LOG_DIR}${PURPOSE}".log"
LOG_FN_ALL=${LOG_DIR}${PURPOSE}".log_all"

##### TIME VAR #####

LAST_MONTH=$(date '+%Y-%m-%d' -d -1month)

##### FILENAME VAR #####

FN_PREFIX="FN_PREFIX"
FN_SUFFIX=".CSV"
FN_OLDER_DEL="${FN_PREFIX}${LAST_MONTH}_000000${FN_SUFFIX}"
FN_LATEST=`find ${SRC_DIR} -type f -name "${FN_PREFIX}*_*${FN_SUFFIX}" | sort | tail -n 1`

####################

##### FUNCTION #####

####################
function DoAndLog() {
    echo + $1 >> $LOG_FN_ONCE; $1 >> $LOG_FN_ONCE; echo "" >> $LOG_FN_ONCE
}
function LogIt() {
    echo -e $1 >> $LOG_FN_ONCE; echo "" >> $LOG_FN_ONCE
}
function LogDrawLine() {
    printf '#%.0s' {1..100} >> $LOG_FN_ONCE; echo "" >> $LOG_FN_ONCE
}
function DoFTP() {
    sftp $FTP_INFO << EOF >> $LOG_FN_ONCE
    lcd $SRC_DIR
    cd $TARGET_DIR
    mput $1
    bye
EOF
}

###########################

########## START ##########

###########################

##### log start #####

echo "" > $LOG_FN_ONCE; LogDrawLine
LogIt "JOB NAME: ${PURPOSE}"
LogIt "SCRIPT start at: ${NOW}"
LogIt "TARGET_DIR: ${TARGET_DIR}"

##### ftp #####

LogIt "FTP connect at: ${FTP_INFO} at: $(date '+%Y/%m/%d %A %H:%M:%S')"
# check
# DoFTP "${FN_LATEST}"
LogIt "FTP disconnect at: $(date '+%Y/%m/%d %A %H:%M:%S')"

##### REMOVE OLD OLD FILE #####

DoAndLog "ls -la ${SRC_DIR}"
for fn in ${SRC_DIR}${FN_PREFIX}*_*${FN_SUFFIX}; do
    if [[ "${fn}" < "${SRC_DIR}${FN_OLDER_DEL}" ]]; then
        DoAndLog "rm -f ${fn}"; fi; done
DoAndLog "ls -la ${SRC_DIR}"

### log end #####

LogIt "SCRIPT finish at: $(date '+%Y/%m/%d %A %H:%M:%S')"
LogDrawLine

##### write to .log_all #####

cat -n $LOG_FN_ONCE >> $LOG_FN_ALL

##### keep 500 lines in .log_all #####

TMP_FILE=$(date '+%Y-%m%d-%H%M-%S').tmpfile
tail -n 500 $LOG_FN_ALL > $TMP_FILE
cat $TMP_FILE > $LOG_FN_ALL
rm -rf $TMP_FILE