Skip to content

Instantly share code, notes, and snippets.

@paseto
Created March 24, 2016 11:16
Show Gist options
  • Save paseto/bdf07e73d32c0004756c to your computer and use it in GitHub Desktop.
Save paseto/bdf07e73d32c0004756c to your computer and use it in GitHub Desktop.
Backup databases bash script with email option
#!/bin/bash
#
# File: uploader_email.sh
#
# Author: Giovani Paseto
#
# Created on Mar 23, 2015, 18:36:24
#
# Require: bzip, mutt
#
# Config file
VERSION="0.1"
CONFIG_FILE=~/.uploader_email
echo "Init setup"
if [[ -e $CONFIG_FILE ]]; then
source "$CONFIG_FILE" 2>/dev/null || {
sed -i'' 's/:/=/' "$CONFIG_FILE" && source "$CONFIG_FILE" 2>/dev/null
}
if [[ $FOLDER_NAME == "" || $MYSQL_U == "" || $MYSQL_P == "" || $MYSQL_DB == "" ]]; then
echo -e "Ops, there's something wrong with you config file, please run again."
unlink $CONFIG_FILE
exit 1
fi
else
while (true); do
echo -ne "\n ----- Uploader v$VERSION - [SETUP] ----- \n"
echo -ne "\n Please follow instructions above:\n"
echo -n " Please choose a folder name to store zipped db [UPLOADER]:"
read FOLDER_NAME
if [[ $FOLDER_NAME == "" ]]; then
FOLDER_NAME="UPLOADER"
fi
echo -n " MySQL username:"
read MYSQL_U
echo -n " MySQL password:"
read MYSQL_P
echo -n " MySQL Database names (,) comma separated:"
read MYSQL_DB
# Save config file
echo -ne "OK\n"
echo "FOLDER_NAME=$FOLDER_NAME" > "$CONFIG_FILE"
echo "MYSQL_U=$MYSQL_U" >> "$CONFIG_FILE"
echo "MYSQL_P=$MYSQL_P" >> "$CONFIG_FILE"
echo "MYSQL_DB=$MYSQL_DB" >> "$CONFIG_FILE"
echo -ne "\n Done!\n"
break
done;
fi
# Params
DATE=$(date +"%d/%m/%Y")
TIME=$(date +"%H:%M")
#Create Database backup
DATABASES=$MYSQL_DB;
array=$(echo $DATABASES | tr "," "\n");
for x in $array
do
CDB="$(echo -e "${x}" | tr -d '[[:space:]]')"
DATABASE_FOLDER="${FOLDER_NAME}/${CDB}/"
COMPRENSSED_FILE="${DATE}${TIME}"
DB_FILE="${FOLDER_NAME}/${CDB}.sql"
echo -e "-------------------------------------"
echo -e "Generating database backup [$CDB]... \c"
mysqldump --opt --single-transaction --quick --password=$MYSQL_P -u $MYSQL_U $CDB > "${DB_FILE}"
echo "done!"
echo -e "BZIP2... $DB_FILE"
# 7z a "$DB_FILE.7z" "$DB_FILE"
#gzip "$DB_FILE"
bzip2 "$DB_FILE"
echo -e "Sending email... \c"
echo "Attached the backup file $DATE $TIME " | mutt -a "${DB_FILE}.bz2" -s "Backup DB ${CDB}" -- sgw.v3x@gmail.com
echo "done!"
rm "${DB_FILE}.bz2"
done
echo "Done $DATE $TIME"
#mutt config put at /home/.muttrc
#touch .muttrc
#set ssl_starttls=yes
#set ssl_force_tls=yes
#set imap_user = 'sgw.v3x@gmail.com'
#set imap_pass = 'pwd'
#set from='sgw.v3x@gmail.com'
#set realname='Your Name'
#set folder = imaps://imap.gmail.com/
#set spoolfile = imaps://imap.gmail.com/INBOX
#set postponed="imaps://imap.gmail.com/[Gmail]/Drafts"
#set header_cache = "~/.mutt/cache/headers"
#set message_cachedir = "~/.mutt/cache/bodies"
#set certificate_file = "~/.mutt/certificates"
#set smtp_url = 'smtps://sgw.v3x@gmail.com:pwd@smtp.gmail.com:465/'
#set move = no
#set imap_keepalive = 900
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment