크론탭설정
50 * * * * /db_backup/db_dump.sh >> /db_backup/db_dump.log
쉘스크립트 코드
#!/bin/bash
dump_date=$(date +%Y-%m-%d)
dump_time=$(date +%H:%M:%S)
backup_root=/db_backup/
mkdir -p $backup_root/$dump_date
backup_dir=$backup_root/$dump_date
r_date=$(date +%Y-%m-%d --date '7 days ago')
db_list=`mysql -u 계정명 -p비밀번호 -h디비접속주소 --port 디비접속포트 -e "show databases" | tail -n+2`;
echo '--------------------------------------------------------------------';
echo $dump_date'_'$dump_time;
cd $backup_root
for db_name in $db_list; do
if [ "$db_name" != "performance_schema" -a "$db_name" != "test" -a "$db_name" != "information_schema" ]; then
echo $db_name;
/usr/bin/mysqldump -u 계정명 -p비밀번호 -h디비접속주소 --port 디비접속포트 --single-transaction $db_name | /bin/bzip2 > $backup_dir/$db_name'_'$dump_date'_'$dump_time.sql.bz2
fi
done
rm -rf $backup_root/$r_date