Update comments

Fixed issue: Do not exit script when upload file to google drive or FTP server failed
This commit is contained in:
Teddysun 2020-04-17 13:57:49 +09:00
parent cf77196d73
commit 3d023e58b7

View File

@ -60,7 +60,7 @@ BACKUP[0]=""
# Number of days to store daily local backups (default 7 days) # Number of days to store daily local backups (default 7 days)
LOCALAGEDAILIES="7" LOCALAGEDAILIES="7"
# Delete Googole Drive's & FTP server's remote file flag (true: delete, false: not delete) # Delete remote file from Googole Drive or FTP server flag (true: delete, false: not delete)
DELETE_REMOTE_FILE_FLG=false DELETE_REMOTE_FILE_FLG=false
# Rclone remote name # Rclone remote name
@ -69,10 +69,10 @@ RCLONE_NAME=""
# Rclone remote folder name (default "") # Rclone remote folder name (default "")
RCLONE_FOLDER="" RCLONE_FOLDER=""
# Upload to FTP server flag (true: upload, false: not upload) # Upload local file to FTP server flag (true: upload, false: not upload)
FTP_FLG=false FTP_FLG=false
# Upload to RCLONE server flag (true: upload, false: not upload) # Upload local file to Google Drive flag (true: upload, false: not upload)
RCLONE_FLG=false RCLONE_FLG=false
# FTP server # FTP server
@ -162,7 +162,6 @@ EOF
log "MySQL root password is incorrect. Please check it and try again" log "MySQL root password is incorrect. Please check it and try again"
exit 1 exit 1
fi fi
if [ "${MYSQL_DATABASE_NAME[*]}" == "" ]; then if [ "${MYSQL_DATABASE_NAME[*]}" == "" ]; then
mysqldump -u root -p"${MYSQL_ROOT_PASSWORD}" --all-databases > "${SQLFILE}" 2>/dev/null mysqldump -u root -p"${MYSQL_ROOT_PASSWORD}" --all-databases > "${SQLFILE}" 2>/dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -173,8 +172,7 @@ EOF
#Add MySQL backup dump file to BACKUP list #Add MySQL backup dump file to BACKUP list
BACKUP=(${BACKUP[*]} ${SQLFILE}) BACKUP=(${BACKUP[*]} ${SQLFILE})
else else
for db in ${MYSQL_DATABASE_NAME[*]} for db in ${MYSQL_DATABASE_NAME[*]}; do
do
unset DBFILE unset DBFILE
DBFILE="${TEMPDIR}${db}_${BACKUPDATE}.sql" DBFILE="${TEMPDIR}${db}_${BACKUPDATE}.sql"
mysqldump -u root -p"${MYSQL_ROOT_PASSWORD}" ${db} > "${DBFILE}" 2>/dev/null mysqldump -u root -p"${MYSQL_ROOT_PASSWORD}" ${db} > "${DBFILE}" 2>/dev/null
@ -214,8 +212,7 @@ start_backup() {
fi fi
# Delete MySQL temporary dump file # Delete MySQL temporary dump file
for sql in $(ls ${TEMPDIR}*.sql) for sql in $(ls ${TEMPDIR}*.sql); do
do
log "Delete MySQL temporary dump file: ${sql}" log "Delete MySQL temporary dump file: ${sql}"
rm -f ${sql} rm -f ${sql}
done done
@ -233,7 +230,7 @@ start_backup() {
# https://rclone.org/downloads/ # https://rclone.org/downloads/
rclone_upload() { rclone_upload() {
if ${RCLONE_FLG} && ${RCLONE_COMMAND}; then if ${RCLONE_FLG} && ${RCLONE_COMMAND}; then
[ -z "${RCLONE_NAME}" ] && log "Error: RCLONE_NAME can not be empty!" && exit 1 [ -z "${RCLONE_NAME}" ] && log "Error: RCLONE_NAME can not be empty!" && return 1
if [ -n "${RCLONE_FOLDER}" ]; then if [ -n "${RCLONE_FOLDER}" ]; then
rclone ls ${RCLONE_NAME}:${RCLONE_FOLDER} 2>&1 > /dev/null rclone ls ${RCLONE_NAME}:${RCLONE_FOLDER} 2>&1 > /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -241,26 +238,25 @@ rclone_upload() {
rclone mkdir ${RCLONE_NAME}:${RCLONE_FOLDER} rclone mkdir ${RCLONE_NAME}:${RCLONE_FOLDER}
fi fi
fi fi
log "Tranferring backup file to Google Drive" log "Tranferring backup file: ${OUT_FILE} to Google Drive"
rclone copy ${OUT_FILE} ${RCLONE_NAME}:${RCLONE_FOLDER} >> ${LOGFILE} rclone copy ${OUT_FILE} ${RCLONE_NAME}:${RCLONE_FOLDER} >> ${LOGFILE}
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
log "Error: Tranferring backup file to Google Drive failed" log "Error: Tranferring backup file: ${OUT_FILE} to Google Drive failed"
exit 1 return 1
fi fi
log "Tranferring backup file to Google Drive completed" log "Tranferring backup file: ${OUT_FILE} to Google Drive completed"
fi fi
} }
# Tranferring backup file to FTP server # Tranferring backup file to FTP server
ftp_upload() { ftp_upload() {
if ${FTP_FLG}; then if ${FTP_FLG}; then
[ -z "${FTP_HOST}" ] && log "Error: FTP_HOST can not be empty!" && exit 1 [ -z "${FTP_HOST}" ] && log "Error: FTP_HOST can not be empty!" && return 1
[ -z "${FTP_USER}" ] && log "Error: FTP_USER can not be empty!" && exit 1 [ -z "${FTP_USER}" ] && log "Error: FTP_USER can not be empty!" && return 1
[ -z "${FTP_PASS}" ] && log "Error: FTP_PASS can not be empty!" && exit 1 [ -z "${FTP_PASS}" ] && log "Error: FTP_PASS can not be empty!" && return 1
[ -z "${FTP_DIR}" ] && log "Error: FTP_DIR can not be empty!" && exit 1 [ -z "${FTP_DIR}" ] && log "Error: FTP_DIR can not be empty!" && return 1
local FTP_OUT_FILE=$(basename ${OUT_FILE}) local FTP_OUT_FILE=$(basename ${OUT_FILE})
log "Tranferring backup file to FTP server" log "Tranferring backup file: ${FTP_OUT_FILE} to FTP server"
ftp -in ${FTP_HOST} 2>&1 >> ${LOGFILE} <<EOF ftp -in ${FTP_HOST} 2>&1 >> ${LOGFILE} <<EOF
user $FTP_USER $FTP_PASS user $FTP_USER $FTP_PASS
binary binary
@ -269,7 +265,11 @@ cd $FTP_DIR
put $FTP_OUT_FILE put $FTP_OUT_FILE
quit quit
EOF EOF
log "Tranferring backup file to FTP server completed" if [ $? -ne 0 ]; then
log "Error: Tranferring backup file: ${FTP_OUT_FILE} to FTP server failed"
return 1
fi
log "Tranferring backup file: ${FTP_OUT_FILE} to FTP server completed"
fi fi
} }
@ -277,19 +277,16 @@ EOF
get_file_date() { get_file_date() {
#Approximate a 30-day month and 365-day year #Approximate a 30-day month and 365-day year
DAYS=$(( $((10#${YEAR}*365)) + $((10#${MONTH}*30)) + $((10#${DAY})) )) DAYS=$(( $((10#${YEAR}*365)) + $((10#${MONTH}*30)) + $((10#${DAY})) ))
unset FILEYEAR FILEMONTH FILEDAY FILEDAYS FILEAGE unset FILEYEAR FILEMONTH FILEDAY FILEDAYS FILEAGE
FILEYEAR=$(echo "$1" | cut -d_ -f2 | cut -c 1-4) FILEYEAR=$(echo "$1" | cut -d_ -f2 | cut -c 1-4)
FILEMONTH=$(echo "$1" | cut -d_ -f2 | cut -c 5-6) FILEMONTH=$(echo "$1" | cut -d_ -f2 | cut -c 5-6)
FILEDAY=$(echo "$1" | cut -d_ -f2 | cut -c 7-8) FILEDAY=$(echo "$1" | cut -d_ -f2 | cut -c 7-8)
if [[ "${FILEYEAR}" && "${FILEMONTH}" && "${FILEDAY}" ]]; then if [[ "${FILEYEAR}" && "${FILEMONTH}" && "${FILEDAY}" ]]; then
#Approximate a 30-day month and 365-day year #Approximate a 30-day month and 365-day year
FILEDAYS=$(( $((10#${FILEYEAR}*365)) + $((10#${FILEMONTH}*30)) + $((10#${FILEDAY})) )) FILEDAYS=$(( $((10#${FILEYEAR}*365)) + $((10#${FILEMONTH}*30)) + $((10#${FILEDAY})) ))
FILEAGE=$(( 10#${DAYS} - 10#${FILEDAYS} )) FILEAGE=$(( 10#${DAYS} - 10#${FILEDAYS} ))
return 0 return 0
fi fi
return 1 return 1
} }
@ -301,12 +298,12 @@ delete_gdrive_file() {
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
rclone delete ${RCLONE_NAME}:${RCLONE_FOLDER}/${FILENAME} >> ${LOGFILE} rclone delete ${RCLONE_NAME}:${RCLONE_FOLDER}/${FILENAME} >> ${LOGFILE}
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
log "Google Drive's old backup file name: ${FILENAME} has been deleted" log "Google Drive's old backup file: ${FILENAME} has been deleted"
else else
log "Failed to delete Google Drive's old backup file name: ${FILENAME}" log "Failed to delete Google Drive's old backup file: ${FILENAME}"
fi fi
else else
log "Google Drive's old backup file name: ${FILENAME} is not exist" log "Google Drive's old backup file: ${FILENAME} is not exist"
fi fi
fi fi
} }
@ -321,22 +318,23 @@ cd $FTP_DIR
del $FILENAME del $FILENAME
quit quit
EOF EOF
log "FTP server's old backup file name: ${FILENAME} has been deleted" if [ $? -eq 0 ]; then
log "FTP server's old backup file: ${FILENAME} has been deleted"
else
log "Failed to delete FTP server's old backup file: ${FILENAME}"
fi
fi fi
} }
# Clean up old file # Clean up old file
clean_up_files() { clean_up_files() {
cd ${LOCALDIR} || exit cd ${LOCALDIR} || exit
if ${ENCRYPTFLG}; then if ${ENCRYPTFLG}; then
LS=($(ls *.enc)) LS=($(ls *.enc))
else else
LS=($(ls *.tgz)) LS=($(ls *.tgz))
fi fi
for f in ${LS[@]}; do
for f in ${LS[@]}
do
get_file_date ${f} get_file_date ${f}
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
if [[ ${FILEAGE} -gt ${LOCALAGEDAILIES} ]]; then if [[ ${FILEAGE} -gt ${LOCALAGEDAILIES} ]]; then
@ -353,12 +351,8 @@ clean_up_files() {
STARTTIME=$(date +%s) STARTTIME=$(date +%s)
# Check if the backup folders exist and are writeable # Check if the backup folders exist and are writeable
if [ ! -d "${LOCALDIR}" ]; then [ ! -d "${LOCALDIR}" ] && mkdir -p ${LOCALDIR}
mkdir -p ${LOCALDIR} [ ! -d "${TEMPDIR}" ] && mkdir -p ${TEMPDIR}
fi
if [ ! -d "${TEMPDIR}" ]; then
mkdir -p ${TEMPDIR}
fi
log "Backup progress start" log "Backup progress start"
check_commands check_commands
@ -371,8 +365,8 @@ rclone_upload
ftp_upload ftp_upload
log "Upload progress complete" log "Upload progress complete"
log "Cleaning up"
clean_up_files clean_up_files
ENDTIME=$(date +%s) ENDTIME=$(date +%s)
DURATION=$((ENDTIME - STARTTIME)) DURATION=$((ENDTIME - STARTTIME))
log "All done" log "All done"