多数据库备份脚本

可以支持多个数据库同时备份,并自动删除5天前的备份。在linux中可以用crontab定时执行#!/bin/bash #This is a ShellScript For Auto DB Backup #Setting DBUser=username DBPasswd=passwd BackupPath=/home/backupdb/ LogFile=/home/backupdb/db.log #Setting End backup_db(){ local DBName=$1 local NewFile="$BackupPath"$(date %Y%m%d)$DBName.tgz local DumpFile="$BackupPath"$(date %Y%m%d)$DBName.sql local OldFile="$BackupPath"$(date %Y%m%d --date='5 days ago')$DBName.tgz echo "-------------------------------------------" >> $LogFile echo "-------------------------------------------" >> $LogFile echo $(date "%Y-%m-%d %H:%M:%S") >> $LogFile echo "--------------------------" >> $LogFile #Delete Old File 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 if [ -f $NewFile ]; then echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile else if [ -z $DBPasswd ]; then /usr/bin/mysqldump -u $DBUser $DBName > $DumpFile else /usr/bin/mysqldump -u $DBUser -p$DBPasswd $DBName > $DumpFile fi tar czvf $NewFile $DumpFile >> $LogFile 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile rm -rf $DumpFile fi } backup_db db1 backup_db db2 backup_db db3

相关推荐

发表评论

路人甲

网友评论(3)

啊啊啊啊啊啊啊啊啊啊
测试 7年前 (2014-03-02) 回复
真不是一般的乱啊
夜蝠翔空 7年前 (2014-02-22) 回复
这一堆乱糟糟的代码啊
Louis Han 7年前 (2014-02-22) 回复