linux下Mysql自動備份腳本

linux下Mysql自動備份腳本

Mysql自動備份腳本
找到一個Mysql自動備份腳本你們須要的話能夠本身根據需求參考修改一下
該腳本功能介紹:能夠將這個腳本放進crontab,天天凌晨執行一次,自動備份,支持多種備份方式,自動翻轉。
該腳本天天最多隻執行一次,並且只保留最近五天的備份在服務器上。
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Setting
#設置數據庫名,數據庫登陸名,密碼,備份路徑,日誌路徑,數據文件位置,以及備份方式
#默認狀況下備份方式是tar,還能夠是mysqldump,mysqldotcopy
#默認狀況下,用root(空)登陸mysql數據庫,備份至/root/dbxxxxx.tgz
DBName=mysql
DBUser=root
DBPasswd=
BackupPath=/root/
LogFile=/root/db.log
DBPath=/var/lib/mysql/
#BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#BackupMethod=tar
#Setting Endmysql


NewFile="$BackupPath"db$(date +%y%m%d).tgz
DumpFile="$BackupPath"db$(date +%y%m%d)
OldFile="$BackupPath"db$(date +%y%m%d --date='5 days ago').tgzlinux

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
fisql

if [ -f $NewFile ]
then
echo "[$NewFile]The Backup File is exists,Can't Backup!" >;>; $LogFile
else
case $BackupMethod in
mysqldump)
if [ -z $DBPasswd ]
then
mysqldump -u $DBUser --opt $DBName >; $DumpFile
else
mysqldump -u $DBUser -p$DBPasswd --opt $DBName >; $DumpFile
fi
tar czvf $NewFile $DumpFile >;>; $LogFile 2>;&1
echo "[$NewFile]Backup Success!" >;>; $LogFile
rm -rf $DumpFile
;;
mysqlhotcopy)
rm -rf $DumpFile
mkdir $DumpFile
if [ -z $DBPasswd ]
then
mysqlhotcopy -u $DBUser $DBName $DumpFile >;>; $LogFile 2>;&1
else
mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >;>;$LogFile 2>;&1
fi
tar czvf $NewFile $DumpFile >;>; $LogFile 2>;&1
echo "[$NewFile]Backup Success!" >;>; $LogFile
rm -rf $DumpFile
;;
*)
/etc/init.d/mysqld stop >;/dev/null 2>;&1
tar czvf $NewFile $DBPath$DBName >;>; $LogFile 2>;&1
/etc/init.d/mysqld start >;/dev/null 2>;&1
echo "[$NewFile]Backup Success!" >;>; $LogFile
;;
esac
fi數據庫

echo "-------------------------------------------" >;>; $LogFilebash

相關文章
相關標籤/搜索