Home > Database > Mysql Tutorial > Linux下MySQL数据库备份脚本

Linux下MySQL数据库备份脚本

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:59:19
Original
836 people have browsed it

这个脚本是07年在网上查找并经过修改用到正式系统下的脚本,数据库名字让我改为了LinuxIDC.com1.首先建立sh文件,在/usr/bin/先建

这个脚本是07年在网上查找并经过修改用到正式系统下的脚本,数据库名字让我改为了LinuxIDC.com

1.首先建立sh文件,在/usr/bin/先建立一个名叫mysqlscript.sh的脚本文件,内容参见mysqlscript.sh

2.其次在根目录下打开 #crontab -e 输入以下内容 12 12 * * * /usr/bin/mysqlscript.sh

3.最后,如果发现不能定时执行脚本,可能的原因是mysqlscript.sh脚本文件的权限问题,然后更改为可执行文件#chmod 777 mysqlscript.sh;

4.上面的工作做完以后就应该没什么问题.
mysqlscript.sh的脚本内容如下:

#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by aspbiz
#2007-11-25

#Setting
#设置数据库名,数据库登录名,密码,备份路径,,日志路径,数据文件位置,以及备份方式
#默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy
#默认情况下,用root(空)登录mysql数据库,备份至/root/dbxxxxx.tgz
#数据库名
DBName=LinuxIDC.com
#数据库用户名
DBUser=root
#密码
DBPasswd=!qazdr%
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 $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 "-------------------------------------------" >> $LogFile

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template