0

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78
  • Possible duplicate of [https://stackoverflow.com/questions/1583481/how-to-create-cron-job-to-backup-mysql-and-ftp-backup-to-my-backup-server](https://stackoverflow.com/questions/1583481/how-to-create-cron-job-to-backup-mysql-and-ftp-backup-to-my-backup-server) – user0 Jul 08 '19 at 11:23
  • Hm... is not in the basic but this post you share have point. Maby `ncftpput` can be solution. Need to try but will love to use something already built in. – Ivijan Stefan Stipić Jul 08 '19 at 11:32

1 Answers1

0

You can use ncftpput.

See my simple script about it, here: https://github.com/NabiKAZ/MySQL-Backup

Nabi K.A.Z.
  • 9,887
  • 6
  • 59
  • 81