/ batch

Backup your MySQL server nightly - using e-mail and ftp

Wanted to create backups at certain intervals for a very important server. And what’s more  I wanted to make sure that the backups were in couple of places. Including my e-mail. So, I bash script was created!

#!/bin/bash
### MySQL Server Login Info ###
MUSER="some_mysql_user"
MPASS="some_mysql_password"
MHOST="localhost"
MYSQL="mysql"
MYSQLDUMP="mysqldump"
BAK="/var/www/backup/mysql/"
GZIP="$(which gzip)"
### FTP SERVER Login info ###
FTPU="some_ftp_user"
FTPP="some_ftp_password"
FTPS="some_ftp_server_information"
NOW=$(date +"%d-%m-%Y")
#E-mail info
EMAIL="some@email.com"

[ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/*

mysqldump -u root -p$MPASS -h $MHOST --all-databases | gzip -9 > /var/www/backup/backup_$NOW.sql.gz

lftp -u $FTPU,$FTPP -e "mput /var/www/backup/backup_$NOW.sql.gz; quit" $FTPS
mutt -s "Full database backup" $EMAIL < message2.txt  -a /var/www/backup/backup_$NOW.sql.gz

Now you may notice you will need to install couple of packages to your server. These are ;

  • Mutt : Setting mutt is very easy. Simply search gmail mutt configuration in google. Who knows. May be you will find me in the process.
  • lftp : You don’t need to set this. And the packages for this is readily available for ubuntu and fedora distros.

Now all you need is to save this little script and call it from cron in periods of your wish.

For those wondering what message2.txt contains. It is a daily usage report that is created by a python script I wrote. However you can simply create a simple txt file of your choosing. That way mutt will include that text in to your e-mail.

John Roach

John Roach

I am John Roach, a devops architect. Find more information here: https://johnroach.io/about/

Read More
Backup your MySQL server nightly - using e-mail and ftp
Share this