title: BASH: Bakup MySQL database and send via FTP to remote server
body: <p>I trying to create BASH script for my API server where I do daily backup with cron. Here is my script:</p>
  <pre><code>#!/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 +&quot;%d%b%Y&quot;`
  
  # 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 &quot;Backup started for database - ${DATABASE_NAME}&quot;
  
      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 &gt; ${FILE}
  
      if [ $? -eq 0 ]; then
          echo &quot;Database backup successfully completed&quot;
      else
          echo &quot;Error found during backup&quot;
      fi
  
  else
      echo &quot;ERROR: Datbase not defined&quot;
  fi
  
  ##### Remove backups older than {BACKUP_RETAIN_DAYS} days  #####
  DBDELDATE=`date +&quot;%d%b%Y&quot; --date=&quot;${BACKUP_RETAIN_DAYS} days ago&quot;`
  if [ ! -z ${DB_BACKUP_PATH} ]; then
      cd ${DB_BACKUP_PATH}
      if [ ! -z ${DBDELDATE} ] &amp;&amp; [ -d ${DBDELDATE} ]; then
          rm -rf ${DBDELDATE}
          echo  &quot;Old backups cleaned up&quot;
      fi
  fi
  
  #### Send backup via FTP ####
  if [ ! -z ${FTP_USER} ]; then
      echo &quot;Sending bakup files to ${FTP_HOST} via FTP&quot;
      
      curl -T &quot;${FILE}&quot; -u ${FTP_USER}:${FTP_PASSWORD} ${FTP_HOST}${FTP_PATH}
      
      if [ $? -eq 0 ]; then
          echo &quot;Files successfuly sent via FTP to remote bakup server&quot;
      else
          echo &quot;FTP error. Files was not sent properly.&quot;
      fi
  fi
  ### End of script ####
  </code></pre>
  <p>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.</p>
  <p>I try to use cURL to send files but this fail.</p>
  <pre><code>...
  #### Send backup via FTP ####
  if [ ! -z ${FTP_USER} ]; then
      echo &quot;Sending bakup files to ${FTP_HOST} via FTP&quot;
      
      curl -T &quot;${FILE}&quot; -u ${FTP_USER}:${FTP_PASSWORD} ${FTP_HOST}${FTP_PATH}
      
      if [ $? -eq 0 ]; then
          echo &quot;Files successfuly sent via FTP to remote bakup server&quot;
      else
          echo &quot;FTP error. Files was not sent properly.&quot;
      fi
  fi
  ...
  </code></pre>
  <p>Is there some good and safe way to do this?</p>