Skip to content

Instantly share code, notes, and snippets.

@linosteenkamp
Last active April 6, 2020 15:22
Show Gist options
  • Save linosteenkamp/4c94a75fd1a31e92b95296312764fed0 to your computer and use it in GitHub Desktop.
Save linosteenkamp/4c94a75fd1a31e92b95296312764fed0 to your computer and use it in GitHub Desktop.
#! /bin/bash
BACKUP_DESTINATION="/Users/linosteenkamp/Database/Backups"
MYSQL_CONNECTION="aax-k8s-mysql-develop"
display_usage() {
if ! command -v mysql_config_editor > /dev/null; then
Echo "MySQL not Installed!"
Echo "Please install MySQL before using this script"
exit 1
fi
if [ -z "$MYSQL_CONNECTION" ] || ! mysql_config_editor print --login-path="$MYSQL_CONNECTION" > /dev/null; then
Echo "Configure your MySQL connection with the mysql_config_editor"
echo "Then set the MYSQL_CONNECTION variable at the sart of this script to the configured login path"
exit 1
fi
echo "You Need to pass the Database Name or wildcard of the databases you wish to backup"
echo -e "\nUsage: ./backup.sh \"Dev_\" \n"
echo -e "This will backup all databases with \"Dev_\" in the database name"
}
if [ $# -eq 0 ] || [ -z "$MYSQL_CONNECTION" ]
then
display_usage
fi
if ! [ -d "$BACKUP_DESTINATION" ]
then
mkdir -p "$BACKUP_DESTINATION"
fi
dbs=$(mysqlshow --login-path="$MYSQL_CONNECTION" | grep $1 | awk '{print $2}')
for db in $dbs;
do
echo "$db backing up ....."
mysqldump --login-path=$MYSQL_CONNECTION $db | gzip -c > "$BACKUP_DESTINATION/$db.sql.dump.gz"
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment