Linux Mysql + shell + ftp 数据库备份

#!/bin/bash
#
# 使用MysqlDump 快速导出数据,并通过FTP上传到文件服务器
# 本地备份文件保存20天
# FTP服务器备份数据保存60天
# Tips: 
#    mysqldump 如果没有 --opt 选项,则在导出的时候会把数据全部加入内存,
#        小心爆表,默认是有此选项的
#    需要添加:本地Mysql用户;FTP服务器用户与其相关目录
USERNAME=mysqlbackup
PASSWORD=mysqlbackup
DATE=`date +%Y-%m-%d`
OLDDATE=`date +%Y-%m-%d -d '-20 days'`
FTPOLDDATE=`date +%Y-%m-%d -d '-60 days'`
MYSQL=/usr/local/mysql/bin/mysql
MYSQLDUMP=/usr/local/mysql/bin/mysqldump
MYSQLADMIN=/usr/local/mysql/bin/mysqladmin
SOCKET=/tmp/mysql.sock
BACKDIR=/data/backup/db
DBNAMES=('mysql' 'test' 'repoot')

[ -d ${BACKDIR} ] || mkdir -p ${BACKDIR}
[ -d ${BACKDIR}/${DATE} ] || mkdir -p ${BACKDIR}/${DATE}
[ ! -d ${BACKDIR}/${OLDDATE} ] || rm -rf ${BACKDIR}/${OLDDATE}


for DBNAME in ${DBNAMES}[@]
do
    ${MYSQLDUMPP} --opt -u${USERNAME} -p${PASSWORD} -S${SOCKET} ${DBNAME} | gzip > ${BACKDIR}/${DATE}/${DBNAME}-backup-${DATE}.sql.gz
    echo "${DBNAME} has been backup successful !"
    /bin/sleep 5
done


HOST=192.168.0.230
FTP_USERNAME=dbmysql
FTP_PASSWORD=dbmysql

cd ${BACKDIR}/${DATE}
ftp -i -n -v << !
open ${HOST}
user ${FTP_USERNAME} ${FTP_PASSWORD}
bin
cd ${FTPOLDDATE}
mdelete *
cd ..
rmdir ${FTPOLDDATE}
mkdir ${DATE}
cd ${DATE}
mput *
bye
!