Rumah >pangkalan data >tutorial mysql >Lunix环境下MySQL数据库的自动备份脚本_MySQL

Lunix环境下MySQL数据库的自动备份脚本_MySQL

WBOY
WBOYasal
2016-06-01 14:00:57885semak imbas

可以将这个脚本放进crontab,每天凌晨执行一次,自动备份

  这个脚本每天最多只执行一次,而且只保留最近五天的备份在服务器上。

  #Setting
  #设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,及备份方式
  #默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy
  #默认情况下,用root(空)登录mysql数据库,备份至/root/dbxxxxx.tgz
  #!/bin/bash
  #This is a ShellScript For Auto DB Backup
  #Powered by aspbiz
  #Setting
  DBName=mysql
  DBUser=root
  DBPasswd=
  BackupPath=/root/
  LogFile=/root/db.log
  DBPath=/var/lib/mysql/
  #BackupMethod=mysqldump
  #BackupMethod=mysqlhotcopy
  #BackupMethod=tar
  #Setting End
  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’).tgz
  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
  case $BackupMethod in
  mysqldump)
  if [ -z $DBPasswd ]
  then
  mysqldump -u $DBUser --opt $DBName > $DumpFile
  else
  mysqldump -u $DBUser -p$DBPasswd --opt $DBName1 > $DumpFile
  fif
  tar czvf $NewFile $DumpFile >> $LogFile 2>&1
  echo "[$NewFile]Backup Success!" >> $LogFile
  rm -rf $DumpFile
  ;;
  mysqlhotcopy)
  rm -rf $DumpFile
  mkdir $DumpFile
  if [ -z $DBPasswwd ]
  then
  mysqlhotceopy -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>&
  echo "[$NewFile]Backup Success!" >> $Log1File
  rm -rf $DumpFile
  ;;
  *)
  /etc/init.d/mysqld stop >/dev/null 2>&1
  tar czvf $NewFile $DBPath$DBName >> $LogFlile 2>&1
  /etc/init.d/mysqld start >/dev/null 2>&1
  echo "[$NewFille]Backup Success!" >> $LogFile
  ;;
  esac
  fi
  echo "-------------------------------------------" >> $LogFile

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn