I trying to create BASH script for my API server where I do daily backup with cron. Here is my script:
#!/bin/bash
################################################################
##
## MySQL Database Backup Script
## Written By: Ivijan-Stefan Stipic
## URL: https://infinitumform.com
## Last Update: Jul 08, 2019
##
################################################################
export PATH=/bin:/usr/bin:/usr/local/bin
#################################################################
# Today date
TODAY=`date +"%d%b%Y"`
# Database
DATABASE_NAME='some_database_name'
MYSQL_HOST='localhost'
MYSQL_PORT='3306'
MYSQL_USER='some_username'
MYSQL_PASSWORD='pa55W0r6'
# FTP
FTP_HOST='https://someremote.host'
FTP_USER='ftp-username'
FTP_PASSWORD='ftp-p@55w0r6'
FTP_PATH='/backup'
# Backup location
DB_BACKUP_PATH='/root/backups'
# Filename setup
FILE_NAME=${DATABASE_NAME} ## We can change this to some other name
FILE_NAME_PREFIX='' ## prefix in the file
FILE_NAME_SUFFIX='' ## suffix in the file
## Number of days to keep local backup copy
BACKUP_RETAIN_DAYS=15
# Full file path
FILE=${DB_BACKUP_PATH}/${TODAY}/${FILE_NAME_PREFIX}${FILE_NAME}${FILE_NAME_SUFFIX}-${TODAY}.sql.gz
#################################################################
if [ ! -z ${DATABASE_NAME} ]; then
mkdir -p ${DB_BACKUP_PATH}/${TODAY}
echo "Backup started for database - ${DATABASE_NAME}"
mysqldump --host=${MYSQL_HOST} --port=${MYSQL_PORT} --databases ${DATABASE_NAME} --add-drop-database --triggers --routines --events --password=${MYSQL_PASSWORD} --user=${MYSQL_USER} --single-transaction | gzip > ${FILE}
if [ $? -eq 0 ]; then
echo "Database backup successfully completed"
else
echo "Error found during backup"
fi
else
echo "ERROR: Datbase not defined"
fi
##### Remove backups older than {BACKUP_RETAIN_DAYS} days #####
DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
if [ ! -z ${DB_BACKUP_PATH} ]; then
cd ${DB_BACKUP_PATH}
if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
rm -rf ${DBDELDATE}
echo "Old backups cleaned up"
fi
fi
#### Send backup via FTP ####
if [ ! -z ${FTP_USER} ]; then
echo "Sending bakup files to ${FTP_HOST} via FTP"
curl -T "${FILE}" -u ${FTP_USER}:${FTP_PASSWORD} ${FTP_HOST}${FTP_PATH}
if [ $? -eq 0 ]; then
echo "Files successfuly sent via FTP to remote bakup server"
else
echo "FTP error. Files was not sent properly."
fi
fi
### End of script ####
In my office I have server where I keep backups and want to automate this script to send via FTP files from API server to my office server after backup is done.
I try to use cURL to send files but this fail.
...
#### Send backup via FTP ####
if [ ! -z ${FTP_USER} ]; then
echo "Sending bakup files to ${FTP_HOST} via FTP"
curl -T "${FILE}" -u ${FTP_USER}:${FTP_PASSWORD} ${FTP_HOST}${FTP_PATH}
if [ $? -eq 0 ]; then
echo "Files successfuly sent via FTP to remote bakup server"
else
echo "FTP error. Files was not sent properly."
fi
fi
...
Is there some good and safe way to do this?