Skip to content

Instantly share code, notes, and snippets.

@allaniftrue
Created February 27, 2018 14:24
Show Gist options
  • Save allaniftrue/9488b154b018579453b40d4dcf68fbfa to your computer and use it in GitHub Desktop.
Save allaniftrue/9488b154b018579453b40d4dcf68fbfa to your computer and use it in GitHub Desktop.
A simple mysql backup script with send
#!/bin/bash
# This is where the backup gets emailed too
[email protected]
DATE=`date`
DATABASEUSER=root
DATABASEPASS=secret
DATABASENAME=test
PREFIX=topup_
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 storage purpose
/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 /usr/home/freebsd/backup/mysql/*.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