#!/bin/bash
#author: songjianhao
#date: 2015-11-19
#usage: backup mysql db exam for xiu_sheng_chu exam system
#update 2015-12-7: add function: caculate size of sql file and to record log file
MYSQLDUMP=/usr/bin/mysqldump
LLH="/bin/ls -lh"
TAR=/bin/tar
RM=/bin/rm
FIND=/bin/find
DATE=/bin/date
TIME=/usr/bin/time
BAKDIR=/db_backup
ECHO=/bin/echo
USER=root
PASS=iwing02
DB_NAME=exam
BKFILE=EXAM
if [ ! -d $BAKDIR ]
then
mkdir $BAKDIR
fi
#back up mysql and caculate backup file size
$ECHO "mysql backup start: `$DATE`" >> /var/log/mysql_bak_time
$MYSQLDUMP -u$USER -p$PASS $DB_NAME --lock-tables > $BAKDIR/$BKFILE-$($DATE +%F).sql
SIZE=`$LLH $BAKDIR/$BKFILE-$($DATE +%F).sql |awk '{print $5}'`
#zip mysql backup file
cd $BAKDIR && $TAR -zcf $BKFILE-$($DATE +%F).sql.tar.gz $BKFILE-$($DATE +%F).sql
#record end time to log
$ECHO "mysql backup end: `$DATE`-SqlFile:$SIZE" >> /var/log/mysql_bak_time
$ECHO "=========================================" >>/var/log/mysql_bak_time
#Delete after 100 days of backup files
$FIND $BAKDIR -mtime +100 -name \*.sql.tar.gz -exec rm -rf {} \;
#Delete SQL FILE
$RM -rf $BAKDIR/*.sql
阅读(1186) | 评论(0) | 转发(0) |