File: //opt/scripts/db-backup.sh
#!/bin/bash
# Define backup variables
DB_USER="db-backups"
DB_PASSWORD="!...@(~Aws/Backups/Rockzz~)@...!"
DB_HOST="localhost"
date_format=`date +%d_%B_%Y_at_%H_%M_%S -d '-5 hours'`
db_dir="/tmp/databases/$date_format"
sync_dir="/tmp/databases"
dest_backup_file="/tmp/databases-VariosDEV-$date_format.tgz"
log_file="/var/log/s3"
s3_bucket="s3://b001-si/db_backups/"
email_notification="helpdesk@aliansap.com.co"
email_copy_notification="andres.garcia@allup.com.co, francisco.restrepo@allup.com.co"
# Delete previous backups from machine
sudo rm -rf /tmp/databases*
# create log file
if [ ! -e $log_file ]; then
touch $log_file
fi
# Create database backups dir
if [ ! -d $db_dir ]; then
mkdir -p $db_dir
fi
# Get a list of all databases -- Except mysql and schema dbs
databases=`mysql -u $DB_USER -h $DB_HOST -p$DB_PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
# Dump all databases
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
echo "Dumping database: $db"
mysqldump -u $DB_USER -h $DB_HOST -p$DB_PASSWORD --databases $db > $db_dir/$db-$date_format.sql
fi
done
# Generate compressed file of backup dir
echo "Compressing databases before upload.."
tar -zcvf $dest_backup_file -C $db_dir . | tee -a $log_file
# Copy compressed file to s3
echo "Backing up databases to s3.."
aws s3 cp $dest_backup_file ${s3_bucket} | tee -a $log_file
# Send email when successful - Optional
# Uncomment this section if you don't need email notification upon completion
echo "Backup successful"
rm -rf $db_dir
echo "" > /tmp/success
echo "DB backups to s3 successful" >> /tmp/success
echo "" >> /tmp/success
aws s3 ls ${s3_bucket} | tee -a /tmp/success
# mail -s "Database backups to s3" -c $email_copy_notification $email_notification < /tmp/success
# echo "Mail sended"
exit 0