Linux Shell Script to backup MySQL database to remote server

Database is the most important file other than web file, hence the backup is very important for the web developer and web owner.

By referring to the current post, you may backup your database automatically from your Linux box.

###START###

#!/bin/bash

# Specify the temporary backup directory
BKUPDIR=”/home/mickgenie/tmp”

# Database Name
dbname=”dbname_here”
dbuser=”user_here”
dbpasswd=”password_here”

# store the current date
date=`date ‘+%Y-%m-%d’`

# Specify Ftp details
ftpserver=”FtpServerIP”
ftpuser=”username”
ftppass=”password”

# Dump the mysql database with the current date and compress it.
/usr/bin/mysqldump -u$dbuser -p$dbpasswd $dbname | gzip > $BKUPDIR/$date.$dbname.sql.gz

# Change directory to the backup directory
cd $BKUPDIR

# Upload the backup
ftp -n $ftpserver <<!EOF!
user $ftpuser $ftppass
binary
prompt
mput *.sql.gz
quit
!EOF!

# Remove the local backup file
/bin/rm -f /$BKUPDIR/$date.$dbname.sql.gz

###END###

With the above script, you may save it as file.sh and just schedule a cronjob to run it as your needed time.