mysql 定时数据备份

可以根据自己的需求自己改写:)

运行
bash mysql_backup.sh
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by liuzheng

#系统变量定义
DBName=Bulletin_production
DBUser=root
DBPasswd=
BackupPath=/home/worker/mysql_backup/

NewFile="$BackupPath"db$(date +%y%m%d).tar.gz
DumpFile="$BackupPath"db$(date +%y%m%d).sql
OldFile="$BackupPath"db$(date +%y%m%d --date='1 days ago').tar.gz


#创建备份文件
if [ ! -d $BackupPath ]; then
	mkdir $BackupPath
fi

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

#删除历史文件
if [ -f $OldFile ]; then
  rm -f $OldFile >> $LogFile
   echo "[$OldFile]Delete Old File Success!"
else
   echo "not exist old file!"
fi

#新文件
if [ -f $NewFile ]; then
	echo "[$NewFile] The Backup File is exists,Can't Backup! "
else
	mysqldump -u $DBUser  $DBName > $DumpFile
	tar czvf $NewFile $DumpFile 
	rm -rf $DumpFile	
	echo "[$NewFile]Backup Success!"
fi

相关推荐