30 4 * * * /etc/cron.d/db.sh


#!/bin/sh
###################
# mysql backup tools #
# code by colorweb   #
###################
_USERNAME="root" # DB root 계정
_PASSWORD="password" # DB root 패스워드
_BACKUP_DIR="/backup/db/" # 백업 위치
c_time="3" # 보관주기
/usr/bin/mysqladmin -u $_USERNAME -p$_PASSWORD reload >& /dev/null
mkdir -p $_BACKUP_DIR$(date +%Y%m%d)
now_db=`echo "show databases" | /usr/bin/mysql -u $_USERNAME -p$_PASSWORD`
num=1
for dir in $now_db ; do
if [ $dir != "Database" ]; then
   /usr/bin/mysqldump -u $_USERNAME -p$_PASSWORD \
   --databases $dir > $_BACKUP_DIR$(date +%Y%m%d)/$dir-$(date +%Y%m%d).sql
   echo "Database - $dir - Backup Complete!!"
   num=$(($num+1))
fi

  # 3일 지난 데이터 삭제
  find $_BACKUP_DIR* -ctime $c_time -exec rm -rf {} \;
done