多数据库备份脚本


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



扫描二维码,在手机上阅读

餐厅禁自带酒水消费者可起诉,拿起法律武器,保障权益

beta版究竟是什么意思,详解

评 论
avatar
测试
  • unknownBrowser
  • Other
啊啊啊啊啊啊啊啊啊啊
2014-03-02 15:23
avatar
  • unknownBrowser
  • Other
真不是一般的乱啊
2014-02-22 15:12
avatar
  • unknownBrowser
  • Other
这一堆乱糟糟的代码啊
2014-02-22 05:52