Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1237670
  • 博文数量: 220
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1769
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-13 16:19
个人简介

努力, 努力, 再努力

文章分类

全部博文(220)

文章存档

2018年(8)

2017年(46)

2016年(75)

2015年(92)

我的朋友

分类: Mysql/postgreSQL

2015-11-19 20:38:55

#!/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) |
给主人留下些什么吧!~~