mysql 远程自动备份脚本

#!/bin/bash
#the script is backup all databases for mysql server,first grant all on *.* to adminbak@'172.16.2.222' identified by "backup";
#Setting  
DBHost=172.16.2.218
DBUser=adminbak 
DBPasswd=backup
BackupPath=/backup/mysqlbak/ 
LogFile=/backup/mysqlbak/db.log 
NewFile="$BackupPath"db$(date +%y%m%d).sql.tar.gz
DumpFile="$BackupPath"db$(date +%y%m%d) 
OldFile="$BackupPath"db$(date +%y%m%d --date='3 days ago').sql.tar.gz 
echo "-------------------------start backup---------------------" >> $LogFile 
echo "start time is $(date +"%y-%m-%d %H:%M:%S")" >> $LogFile 
#Delete Old File 
if [ -f $OldFile ] 
then 
   rm -f $OldFile >> $LogFile
   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 
      mysqldump  -h$DBHost -u$DBUser -p$DBPasswd --all-databases > $DumpFile 
      tar czvf $NewFile $DumpFile >/dev/null 2>&1  
      echo "[$NewFile]Backup Success!" >> $LogFile 
      rm -rf $DumpFile 
fi 
echo "end  time is $(date +"%y-%m-%d %H:%M:%S")" >> $LogFile 
echo "--------------------------end backup----------------------" >> $LogFile

你可能感兴趣的:(mysql,数据库,shell脚本,MysqlDump,远程备份)