Skip to content

Instantly share code, notes, and snippets.

@allaniftrue
Last active January 29, 2018 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save allaniftrue/0ad52f931f317872b6a070425ea061bd to your computer and use it in GitHub Desktop.
Save allaniftrue/0ad52f931f317872b6a070425ea061bd to your computer and use it in GitHub Desktop.
FreeBSD script to automate MySQL backup and send file to email
#!/bin/bash
# Credentials
DATE=`date`
DATABASEUSER=root
DATABASEPASS=secret
DATABASENAME=test
BACKUPMAIL=backup@wordpress.com
# For filename purpose
PREFIX=test_
NOW="$(date +"%d-%m-%Y")"
PATH=/usr/home/freebsd/backup/mysql/
cd ${PATH}
# Dump DB
/usr/local/bin/mysqldump --opt -u${DATABASEUSER} -p${DATABASEPASS} ${DATABASENAME} > ${DATABASENAME}.sql
# Archive for long term storage
/usr/bin/tar -zcvf ${PREFIX}${NOW}.tgz *.sql
# Zip send to me
/usr/bin/gzip -c ${DATABASENAME}.sql | /usr/bin/uuencode ${DATABASENAME}.sql.gz | /usr/bin/mail -s "MySQL DB ${DATABASENAME} for $DATE" ${BACKUPMAIL}
#Remove SQL files
/bin/rm ${PATH}*.sql
# Delete files older than 30 days
/usr/bin/find ${PATH}* -mtime +30 -exec /bin/rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment