Skip to content

Instantly share code, notes, and snippets.

@jpomykala
Last active November 10, 2019 13:32
Show Gist options
  • Save jpomykala/047ad7b2ab8d51ccbe2fce408473bde5 to your computer and use it in GitHub Desktop.
Save jpomykala/047ad7b2ab8d51ccbe2fce408473bde5 to your computer and use it in GitHub Desktop.
mysql-backup.sh
#!/bin/bash
# I installed MariaDB 10 from Package Center
export PATH=/bin:/usr/bin:/usr/local/bin:/volume1/@appstore/MariaDB10/usr/local/mariadb10/bin
# Put backups in the same place where the script is placed
DB_BACKUP_PATH=`dirname "$0"`
backup-mysql() {
MYSQL_HOST=$1
MYSQL_PORT=$2
MYSQL_USER=$3
MYSQL_PASSWORD=$4
DATABASE_NAME=$5
echo "${DATABASE_NAME} - backup started"
TODAY=`date +"%Y-%m-%d__%H-%M-%S"` # format: 2019-10-12__13-53-23
mkdir -p ${DB_BACKUP_PATH}
# docs: https://dev.mysql.com/doc/refman/8.0/en/mysqldump.htm
mysqldump -h ${MYSQL_HOST} \
-P ${MYSQL_PORT} \
-u ${MYSQL_USER} \
-p${MYSQL_PASSWORD} \
--quick \
${DATABASE_NAME} > ${DB_BACKUP_PATH}/${TODAY}__${DATABASE_NAME}.sql
if [ $? -eq 0 ]; then
echo "${DATABASE_NAME} - backup successfully completed"
else
echo "Error found during backup"
exit 1
fi
}
backup-mysql 'db_host' 'db_port' 'db_usename' 'db_password' 'db_name'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment