MySQL自动备份脚本(linux) 晴

jed , 2006-12-5 11:10 , 服务器技术 , 评论(0) , 阅读(3999) , Via 本站原创 | |
  之前,有用户向我们提出过疑问:如何才能实现MySQL的自动备份,当时,我们的回复是编一个自动脚本。
     现在我们把这段代码贴出来,希望对各位有所帮助。可以将这个脚本放进crontab,每天凌晨执行一次,自动备份。

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

  代码:

<br/><br/>#!/bin/bash <br/>#This is a ShellScript For Auto DB Backup <br/>#Powered by aspbiz <br/>#2004-09 <br/><br/>#Setting <br/>#设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式 <br/>#默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy <br/>#默认情况下,用root(空)登录mysql数据库,备份至/root/dbxxxxx.tgz <br/>DBName=mysql <br/>DBUser=root <br/>DBPasswd= <br/>BackupPath=/root/ <br/>LogFile=/root/db.log <br/>DBPath=/var/lib/mysql/ <br/>#BackupMethod=mysqldump <br/>#BackupMethod=mysqlhotcopy <br/>#BackupMethod=tar <br/>#Setting End <br/><br/><br/>NewFile=&quot;$BackupPath&quot;db$(date +%y%m%d).tgz <br/>DumpFile=&quot;$BackupPath&quot;db$(date +%y%m%d) <br/>OldFile=&quot;$BackupPath&quot;db$(date +%y%m%d --date=&#039;5 days ago&#039;).tgz <br/><br/>echo &quot;-------------------------------------------&quot; &gt;&gt; $LogFile <br/>echo $(date +&quot;%y-%m-%d %H:%M:%S&quot;) &gt;&gt; $LogFile <br/>echo &quot;--------------------------&quot; &gt;&gt; $LogFile <br/>#Delete Old File <br/>if [ -f $OldFile ] <br/>then <br/> &nbsp; rm -f $OldFile &gt;&gt; $LogFile 2&gt;&amp;1 <br/> &nbsp; echo &quot;[$OldFile]Delete Old File Success!&quot; &gt;&gt; $LogFile <br/>else <br/> &nbsp; echo &quot;[$OldFile]No Old Backup File!&quot; &gt;&gt; $LogFile <br/>fi <br/><br/>if [ -f $NewFile ] <br/>then <br/> &nbsp; echo &quot;[$NewFile]The Backup File is exists,Can&#039;t Backup!&quot; &gt;&gt; $LogFile <br/>else <br/> &nbsp; case $BackupMethod in <br/> &nbsp; mysqldump) <br/> &nbsp; &nbsp; &nbsp;if [ -z $DBPasswd ] <br/> &nbsp; &nbsp; &nbsp;then <br/> &nbsp; &nbsp; &nbsp; &nbsp; mysqldump -u $DBUser --opt $DBName &gt; $DumpFile <br/> &nbsp; &nbsp; &nbsp;else <br/> &nbsp; &nbsp; &nbsp; &nbsp; mysqldump -u $DBUser -p$DBPasswd --opt $DBName &gt; $DumpFile <br/> &nbsp; &nbsp; &nbsp;fi <br/> &nbsp; &nbsp; &nbsp;tar czvf $NewFile $DumpFile &gt;&gt; $LogFile 2&gt;&amp;1 <br/> &nbsp; &nbsp; &nbsp;echo &quot;[$NewFile]Backup Success!&quot; &gt;&gt; $LogFile <br/> &nbsp; &nbsp; &nbsp;rm -rf $DumpFile <br/> &nbsp; &nbsp; &nbsp;;; <br/> &nbsp; mysqlhotcopy) <br/> &nbsp; &nbsp; &nbsp;rm -rf $DumpFile <br/> &nbsp; &nbsp; &nbsp;mkdir $DumpFile <br/> &nbsp; &nbsp; &nbsp;if [ -z $DBPasswd ] <br/> &nbsp; &nbsp; &nbsp;then <br/> &nbsp; &nbsp; &nbsp; &nbsp; mysqlhotcopy -u $DBUser $DBName $DumpFile &gt;&gt; $LogFile 2&gt;&amp;1 <br/> &nbsp; &nbsp; &nbsp;else <br/> &nbsp; &nbsp; &nbsp; &nbsp; mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile &gt;&gt;$LogFile 2&gt;&amp;1 <br/> &nbsp; &nbsp; &nbsp;fi <br/> &nbsp; &nbsp; &nbsp;tar czvf $NewFile $DumpFile &gt;&gt; $LogFile 2&gt;&amp;1 <br/> &nbsp; &nbsp; &nbsp;echo &quot;[$NewFile]Backup Success!&quot; &gt;&gt; $LogFile <br/> &nbsp; &nbsp; &nbsp;rm -rf $DumpFile <br/> &nbsp; &nbsp; &nbsp;;; <br/> &nbsp; *) <br/> &nbsp; &nbsp; &nbsp;/etc/init.d/mysqld stop &gt;/dev/null 2&gt;&amp;1 <br/> &nbsp; &nbsp; &nbsp;tar czvf $NewFile $DBPath$DBName &gt;&gt; $LogFile 2&gt;&amp;1 <br/> &nbsp; &nbsp; &nbsp;/etc/init.d/mysqld start &gt;/dev/null 2&gt;&amp;1 <br/> &nbsp; &nbsp; &nbsp;echo &quot;[$NewFile]Backup Success!&quot; &gt;&gt; $LogFile <br/> &nbsp; &nbsp; &nbsp;;; <br/> &nbsp; esac <br/>fi <br/><br/>echo &quot;-------------------------------------------&quot; &gt;&gt; $LogFile <br/> <br/><br/>


发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]