mysqldump script again

I decided to look back at my mysqldump script and thought it would be better to improve upon the whole process.

Important notes: Mysqldump looks at a file that’s chmod 600 for credentials to do the backups and email for the failure of backups needs to be done.

#!/bin/bash
# Create system variables:
NFSMOUNT=/mnt/nfs/freenas/Backup/karlahlers/databases
DATE=`date +%Y%m%d`
COMPAREDATE=`date +%Y%m%d`
ALLDATABASES=KA_DATABASES
# Email variables:
TO="karl@karlahlers.com"
FROM="karl@karlahlers.com"
SUBJECT="MySQL database KA_"$DATE".sql has been created!"
NFS_MOUNT_FAIL="Failed to mount NFS"

# Function for MySQL Backup
MYSQL_BACKUP () {
  mysqldump --defaults-extra-file=/srv/scripts/.sqlpwd --all-databases > "$NFSMOUNT"/"$ALLDATABASES"_"$DATE".sql
  return 0
}

# Check if NFS folder is mounted and available if not try to remount it
if [ -d $NFSMOUNT ]
  then
    echo "Creating database "$ALLDATABASES"_"$DATE".sql"
      if [ -f "$ALLDATABASES"_"$DATE".sql ]
        then
          echo "Exiting"
          exit 0
      fi
    MYSQL_BACKUP
  else
    echo "Remounting NFS Share"
    systemctl restart mnt-nfs-freenas.mount
    sleep 3
      if [ ! -d $NFSMOUNT ]
        then
          echo "Restarting OpenVPN Tunnel"
          systemctl restart openvpn
          sleep 5
          ping -c 4 10.4.0.1 &> /dev/null
            if [ $? = 0 ]
              then
                MYSQL_BACKUP
              else
                /usr/sbin/sendmail -f $FROM -t $TO -u $NFS_MOUNT_FAIL -m "NFS share isn't mounted! Re-establishing VPN failed! Please backup manually!"
            fi
      fi
    MYSQL_BACKUP

fi

# Locate files older than 14 days and delete them
find $NFSMOUNT -type f -mtime +13 -exec rm {} \;
Tagged , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.