Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
small db backup
#!/bin/bash
current_hour=$(date +"%H")
current_day=$(date +"%d")
current_month=$(date +"%m")
dbuser=backup
dbpass=******
dbname=mydb
from=backup@domain.com
to=email_backup@domain.com
backup_dir=/srv/backup/
subject="Backup from ${dbname} - $(date)"
txtfile=$(mktemp) # A text message with a simple preface message
sqlfile=$(mktemp) # File to be attached and generally requiring encoding
filename="backup-${dbname}-$(date +"%Y%m%d%H%M%S").sql"
subject="Your attachment" # Change as needed
# mime="plain/text" # Adjust this to the proper mime-type of file
mime="application/x-gzip" # Adjust this to the proper mime-type of file
mysqldump -u${dbuser} -p${dbpass} ${dbname} > $sqlfile
cp $sqlfile ${backup_dir}backup-hourly-$current_hour.sql
if [ -d $backup_dir ]; then
if [ $current_hour = '00' ]; then
cp $sqlfile ${backup_dir}backup-daily-$current_day.sql
fi
if [ $current_day = '01' ]; then
cp $sqlfile $backup_dir$backup-$current_month.sql
fi
fi
encoding=base64
boundary="---my-unlikely-text-for-mime-boundary---$$--"
if [ $current_hour = '00' ]; then
(cat <<EOF
From: $from
To: $to
Subject: $subject
Date: $(LC_TIME=en date +"%a, %b %e %Y %T %z")
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="$boundary"
Content-Disposition: inline
--$boundary
Content-Type: text/plain; charset=utf8
Content-Disposition: inline
This email has attached the file
--$boundary
Content-Type: $mime;name="$filename"
Content-Disposition: attachment;filename="$filename"
Content-Transfer-Encoding: $encoding
EOF
base64 $sqlfile
echo ""
echo "--$boundary" ) | sendmail -t
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.