# 建立备份用户.
mysql> grant select,lock tables,reload,super,file,show view on *.* to 'mysqlbackup'@'localhost' identified by 'mysql_ritto';
mysql> flush privileges;
# 脚本如下.
#!/bin/bash
USERNAME=mysqlbackup
PASSWORD=mysql_ritto
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
[ -d ${BACKDIR} ] || mkdir -p ${BACKDIR}
[ -d ${BACKDIR}/${DATE} ] || mkdir ${BACKDIR}/${DATE}
[ ! -d ${BACKDIR}/${OLDDATE} ] || rm -rf ${BACKDIR}/${OLDDATE}
for DBNAME in mysql db1 db2 db3
do
${MYSQLDUMP} --opt --master-data=2 --tz-utc=true -u${USERNAME} -p${PASSWORD} -S${SOCKET} ${DBNAME} | gzip > ${BACKDIR}/${DATE}/${DBNAME}-backup-${DATE}.s l.gz
logger "${DBNAME} has been backup successful - $DATE"
/bin/sleep 5
done
HOST=10.1.2.22
FTP_USERNAME=db1
FTP_PASSWORD=db1_ritto
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
!
# 备份后的目录结构是这个样子的.
[root@db1 db]# ls
2011-11-10 2011-11-12 2011-11-14 2011-11-16 2011-11-18 2011-11-20 2011-11-22 2011-11-24 2011-11-26 2011-11-28
2011-11-11 2011-11-13 2011-11-15 2011-11-17 2011-11-19 2011-11-21 2011-11-23 2011-11-25 2011-11-27 2011-11-29
[root@db1 db]# ls -lhtr 2011-11-29/
total 3.1G
-rw-r--r-- 1 root root 133K Nov 29 02:01 mysql-backup-2011-11-29.sql.gz
-rw-r--r-- 1 root root 1.5G Nov 29 02:06 db1-backup-2011-11-29.sql.gz
-rw-r--r-- 1 root root 35M Nov 29 02:06 db2-backup-2011-11-29.sql.gz