Tag Archives: shell

Bash Shell File to backup Mysql Server and Save it over FTP

today i was trying to move mysql server from one to another , so i had to use this bash shell file , and just wanna share it to public
1-requirement :
Good FTP Client like lftp , you can download it by : yum install lftp (redhat or any RPM dependent distro ) or apt-get install lftp (debian or ubuntu )

2-create file name “mysql.backup.sh”

and write down

#!/bin/bash
### MySQL Server Login Info ###
MUSER="root"
MPASS=""
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
BAK="/backup/mysql"
GZIP="$(which gzip)"
### FTP SERVER Login info ###
FTPU="ftpusernane"
FTPP="ftppassword"
FTPS="targetServer"
NOW=$(date +"%d-%m-%Y")
[ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/*
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BAK/$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
lftp -u $FTPU,$FTPP -e "mkdir /mysql/$NOW;cd /mysql/$NOW; mput /backup/mysql/*; quit" $FTPS

3- run this command  chmod +x mysql.backup.sh to give the file execute permission

4- one more tip , you can add it to crontab ……………….. so it can run automatically  as you want

easy , isn’t it ??