日期:2014-05-16 浏览次数:20541 次
#!/bin/bash ############################################### # NAME: mysql_tool.sh # AUTH: GuZhiQiang # DATE: 2011-12-13 ############################################### #Setting #设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式 #默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy #运行方式:./mysql_tool.sh database tar DBName=$1 DBUser=root DBPasswd=*** BackupPath=/data/sqls/ LogFile=/data/db.log DBPath=/var/lib/mysql/ BackupMethod=$2 #BackupMethod=mysqldump #BackupMethod=mysqlhotcopy #BackupMethod=tar #Setting End NewFile="$BackupPath""$DBName"_$(date +%y%m%d).tgz DumpFile="$DBName"_$(date +%y%m%d).sql OldFile="$BackupPath""$DBName"_$(date +%y%m%d --date='5 days ago').tgz CHECK_FOR_START () { if [ $(id -u) != "0" ]; then echo "Error: You must be root to run this script, please use root to install!" exit 1 fi if [ "$DBName" != "" ] && [ "$BackupMethod" != "" ];then LOG_HEAD_INFO DELETE_OLDFILE BACKUP_START else LOG_HEAD_INFO echo "Command error!!!" >> $LogFile echo "Input[#]:./mysqlbak_4399.sh {database} {mysqldump|mysqlhotcopy|tar} " >> $LogFile fi } LOG_HEAD_INFO () { echo "-------------------------------------------" >> $LogFile echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile echo "--------------------------" >> $LogFile } DELETE_OLDFILE() { #删除5天前的旧文件 if [ -f $OldFile ] then rm -f $OldFile >> $LogFile 2>&1 echo "[$OldFile]Delete Old File Success!" >> $LogFile else echo "[$OldFile]No Old Backup File!" >> $LogFile fi } BACKUP_START() { if [ -f $NewFile ] then echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile else ##使用何种备份方式 case $BackupMethod in mysqldump) if [ -z $DBPasswd ] then mysqldump -u $DBUser --opt $DBName > $DumpFile else mysqldump -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile fi tar czvf $NewFile $DumpFile >> $LogFile 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile rm -f $DumpFile ;; mysqlhotcopy) rm -f $DumpFile mkdir $DumpFile if [ -z $DBPasswd ] then mysqlhotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1 else mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1 fi tar czvf $NewFile $DumpFile >> $LogFile 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile rm -f $DumpFile ;; tar) /etc/init.d/mysqld stop >/dev/null 2>&1 tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1 /etc/init.d/mysqld start >/dev/null 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile ;; esac fi } CHECK_FOR_START && echo "-------------------------------------------" >> $LogFile?