2012. 2. 2. 10:37

MySql DB 별 백업 쉘스크립트(메모)

매일 디렉토리 생성 후 데이타베이스별로 백업 후 압축
15일이 지난 백업분 제거

#!/bin/sh

DBHOST="호스트"
DBUSER="계정"
DBPWD="비번"

BACKUPDIR="/root/backup/"
OPTIONS="--skip-comments --default-character-set=utf8 --routines --single-transaction --quick"

DATE=`date +%Y%m%d`
DELDATE=`date --date "15 day ago" +%Y%m%d`

rm -rf ${BACKUPDIR}${DELDATE}
mkdir ${BACKUPDIR}${DATE}

RESULT=`/usr/local/mysql/bin/mysql -u$DBUSER -p$DBPWD -h$DBHOST -e "show databases" | grep -v Database`

for DB in $RESULT; do

  FNAME=${BACKUPDIR}${DATE}/${DB}${DATE}.sql
  /usr/local/mysql/bin/mysqldump -u$DBUSER -p$DBPWD -h$DBHOST $OPTIONS $DB >> $FNAME
  echo "set foreign_key_checks=1;" >> $FNAME
  gzip --rsyncable $FNAME

done