可以支持多个数据库同时备份,并自动删除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