该脚本实现了以下几点功能:

1、在线备份mysql数据库;

2、将备份的日志存入日志文件;

3、压缩数据备份文件;

4、将压缩后的文件远程ftp到备份服务器;

5、删除本地备份超过7天的数据主要是防止因备份导致生产服务器磁盘空间的非正常增长带来性能瓶颈问题。

具体实现shell如下所示:

#!/bin/sh

#---------------------------------------------------------

# scriptname: erpbackup.sh

# version: 1.0 for linux

# method: backup mysqldb

# author: fengzhanhai

# history: create program V1.1 20110429 by Fengzhanhai

UserName='root'

AdminPwd='passw0rd'

DbName=erp

BakDir=/home/imu/db-backup/vpnback

LogFile=/home/imu/db-backup/vpnback/mysqlbak.log

DATE=`date +%Y%m%d%H%M`

DumpFile=vpndata$DATE.sql

GZDumpFile=$DumpFile.tgz

FtpServer=10.19.98.235

FtpTargetPath=/erp_backup/mysql

#main---------------------------------------------------------

echo $(date +"%y-%m-%d %H:%M:%S") backup begin >> $LogFile

cd $BakDir

#备份当天文件

/usr/local/mysql/bin/mysqldump -u$UserName -p$AdminPwd $DbName > $DumpFile

echo $(date +"%y-%m-%d %H:%M:%S") Dump Done >> $LogFile

#tar压缩文件

tar czvf $GZDumpFile $DumpFile >> $LogFile

#rm -f $DumpFile

echo $(date +"%y-%m-%d %H:%M:%S") $GZDumpFile compress done >> $LogFile

#上传到ftp服务器

ftp -v -n $FtpServer << END

user databack passw0rd

bin

cd $FtpTargetPath

put $GZDumpFile

bye

END

echo $(date +"%y-%m-%d %H:%M:%S") ftp $GZDumpFile compress done >> $LogFile

rm -f $GZDumpFile

#删除当前目录下7天前的文件备份文件

find $BakDir -name "vpndata*" -mtime +7 -exec  rm {} \;

echo $(date +"%y-%m-%d %H:%M:%S") delete old file done >> $LogFile

#验证结果

ls -al $DumpFile >> $LogFile