code-dumps/sh/mysqldump-aes.sh

66 lines
1.7 KiB
Bash

#!/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