db / 未分类 · 2012年10月29日

blog上mysql的简单备份

#!/bin/sh
USERNAME=root
PASSWORD=123456
DATE=`date +%Y-%m-%d`
OLDDATE=`date +%Y-%m-%d -d '-1 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 wordpress
#for DBNAME in mysql wordpress
do
${MYSQLDUMP} -u${USERNAME} -p${PASSWORD} ${DBNAME}> ${BACKDIR}/${DATE}/${DBNAME}-backup-${DATE}.sql
tar -cjvf ${BACKDIR}/${DATE}/${DBNAME}-backup-${DATE}.sql.tar.bz2 ${BACKDIR}/${DATE}/${DBNAME}-backup-${DATE}.sql && rm -rf ${BACKDIR}/${DATE}/${DBNAME}-backup-${DATE}.sql
#gzip > ${BACKDIR}/${DATE}/${DBNAME}-backup-${DATE}.sql.gz && rm -rf ${BACKDIR}/${DATE}/${DBNAME}-backup-${DATE}.sql
#logger "${DBNAME} has been backup successful - $DATE"
/bin/sleep 5
done

eg
[root@326219 db]# ls 2012-10-28/
wordpress-backup-2012-10-28.sql.tar.bz2