Dump all databases

Simple script with minimal settings to be set. Will dump databases and save them in a location of your choosing after cleaning backups that are deemed to old (age of your choosing). It’s been written with mariadb as primary target. Mariadb binaries complain now when called on with their mysql-counterpart names. So mysql is now mariadb. mysqldump is now mariadb-dump

#!/bin/bash

# SETTING ENVIRONMENT - free choices
TARGETDIR=/backup
MAXAGE=7
GZIPCOMPRESSION=8
# SETTING ENVIRONMENT - default
TIMESTAMP=`date +%Y%m%d`
# SETTING ENVIRONMENT - mariadb binaries complain when called on with mysql-name
MYSQLBIN=/usr/bin/mariadb
MYSQLDUMPBIN=/usr/bin/mariadb-dump

# PREPERATION - Create backup target
mkdir -p $TARGETDIR/$TIMESTAMP
# PREPERATION - Clean backup target
find $TARGETDIR -ctime +$MAXAGE -exec rm -rf {} \;


# ACTION write database backups
# Loop through all database names
for DATABASE in `echo "show databases;" | $MYSQLBIN | grep -v Database | grep -v information_schema | grep -v mysql | grep -v sys | grep -v performance_schema` ; do
	echo "Dumping "$DATABASE" to "$TARGETDIR/$TIMESTAMP/$DATABASE".sql.gz"
	$MYSQLDUMPBIN $DATABASE | gzip -$GZIPCOMPRESSION > $TARGETDIR/$TIMESTAMP/$DATABASE.sql.gz
	ls -l $TARGETDIR/$TIMESTAMP/$DATABASE.sql.gz
done

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

four × 1 =