目标:使用mysqldump自动导出数据文件(全库),再通过FTP实现异地备份。

如下:

#!/bin/bash
#Program:
#       The program will export database to a director.
#History:
#2014/03/10     Chris   First release



# Exporting MYSQL databases
######################################################################################
PATH=${PATH}
export PATH

dbun='root'
dbpass='Antec456'
dt='/bin/date +%Y%m%d%H%M'
bakdir='/srv/mysql-backup'
file="ADB$($dt).sql"
dbfile="$bakdir/$file"


/usr/bin/mysqldump -u$dbun -p$dbpass -A > $dbfile

if [ $? == '0' ]; then
        echo -e "MYSQL databases has been exported to: $dbfile "
        /usr/bin/logger "MYSQL databases has been exported to: $dbfile"
else
        echo "Error: No export MYSQL database files!"
        /usr/bin/logger "Error: No export MYSQL database files!"
        exit 1
fi


# Upload the MYSQL databases file
######################################################################################
ftpserver='202.43.144.82'
ldir='WebServer_Backup'
ftpuser='Design'
ftppass='believeit'
ftplog='/tmp/ftplog'

if [ -f $dbfile ]; then
(
        /usr/bin/ftp -ivn $ftpserver <<EOF
        user $ftpuser $ftppass
        lcd $bakdir
        cd $ldir
        put $file
        bye
EOF
) 2> $ftplog
        if [ -s $ftplog ]; then
                echo -e "The MYSQL databases file upload failed! Failed Info: \"$(cat $ftplog)\""
                /usr/bin/logger "The MYSQL databases file upload failed! Failed Info: \"$(cat $ftplog)\""
                rm -f $ftplog
                exit 3
        else
                /usr/bin/logger "The MYSQL database file upload to $ftpserver:$ldir$file successfully!"
                echo -e "The MYSQL database file upload to $ftpserver:$ldir$file successfully!\n"
                exit 0
        fi

else
        /usr/bin/logger "The MYSQL databases file not exist!"
        echo "The MYSQL databases file not exist!"
        exit 2
fi