1 min read

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!

### MySQL Server Login Info ###
GZIP="$(which gzip)"
### FTP SERVER Login info ###
NOW=$(date +"%d-%m-%Y")
#E-mail info

[ ! -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.