From d78abf73fce67257af1f651953c27e8aada8269b Mon Sep 17 00:00:00 2001 From: Ken Fong Date: Wed, 2 Jan 2019 14:10:27 +0800 Subject: [PATCH] initial commit --- mysqldump-aes.sh | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 mysqldump-aes.sh diff --git a/mysqldump-aes.sh b/mysqldump-aes.sh new file mode 100644 index 0000000..9fb167f --- /dev/null +++ b/mysqldump-aes.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +LOGFILE="/var/log/mysqldumps.log" +function log { + echo "$(date) - $1" >> $LOGFILE +} + +MYSQLBIN=/usr/bin +MYSQLBACKUPPATH="/db-dumps" +MYSQLUSER="root" +MYSQLPASS="3/FFzU|-F+Xu" +RETENTION=14 +FAILFLAG=0 +AESPASS=/usr/local/soap/bin/aes-passphrase + +# Specify connection method +#MYSQLCONN="" +MYSQLCONN="-S /u03/mysql/mysql.sock" + +log "========== Starting Database Dumps ==========" >> $LOGFILE + +# Script begins +DATABASES=`$MYSQLBIN/mysql -u${MYSQLUSER} -p${MYSQLPASS} ${MYSQLCONN} -Nse "show databases;"` +if [ $? -gt 0 ]; then + log "========== Error: Could not execute 'show databases' ==========" + FAILFLAG=1 +fi + +# clean up stale archives +find $MYSQLBACKUPPATH -name "MYB*" -type d -mtime +$RETENTION | xargs rm -Rf + +# create dated directory +DATESTAMP=$(date +%Y%m%d) +DSTDIR=${MYSQLBACKUPPATH}/MYB-${DATESTAMP} +mkdir -vp $DSTDIR + +# backup dataabse individually +# possibly use background and wait to do this in parallel +# but need to check each bg process's exit code! + +for DB in $DATABASES; do + case ${DB} in + information_schema|performance_schema) + continue + ;; + esac + + log "Backup up ${DB}" + $MYSQLBIN/mysqldump --opt --routines -u${MYSQLUSER} -p${MYSQLPASS} ${MYSQLCONN} ${DB} > ${DSTDIR}/${DB}.sql 2>> $LOGFILE +if [ $? -gt 0 ]; then + log "Backup of ${DB} failed" + FAILFLAG=1 +fi +gzip -fc ${DSTDIR}/${DB}.sql | openssl enc -aes-256-cbc -salt -out ${DSTDIR}/${DB}.sql.gz.aes -pass file:/usr/local/soap/bin/aes-passphrase +rm -f ${DSTDIR}/${DB}.sql +done + +if [ $FAILFLAG -gt 0 ]; then + log "========== Errors Encountered During Data Dump ==========" +else + log "========== All Databases Dumped Successfully ==========" +fi + +exit 0 +