自动备份mysql数据库 shell脚本

jopen 10年前

最近使用了testlink和mantis这2个测试相关的系统,而他们又全部是基于mysql+php开发的,因此定期备份数据库也是很有必要的,下面是自己写的一个简单的shell脚本。我的mysql是通过xampp这个套件安装的,因为这个套件可以一起安装testlink和mantis 需要的php+apache环境。

#!/bin/bash  #this script will backup the mysql databases and source code automatically.  #Date: 2013-11-21  #Author: Cullen    #variable list  DbTestCase=testlink #testlink的数据库名称  DbBug=bugtracker   #mantis在mysql里的数据库名称  DbUser=root  DbPwd=admin  BackupPath=/root/mysql_backup/  LogFile=/root/mysql_backup/log_file    #check the backup file exists or not  if [ ! -d $BackupPath ]; then   mkdir $BackupPath  fi    for DbName in $DbTestCase $DbBug   do   NewFile="$BackupPath""$DbName"$(date +%y%m%d).tar.gz   DumpFile="$BackupPath""$DbName"$(date ++%y%m%d).sql   OldFile="$BackupPath""$DbName"$(date +%y%m%d --date='1 weeks ago').tar.gz     echo "-----------------------------------------"   echo $(date +"%y-%m-%d %H:%M:%S")   echo "-----------------------------------------"     #create new backup file weekly   if [ -f $NewFile ]; then    echo "New backup file have exists!"   else    /opt/lampp/bin/mysqldump -uroot -padmin $DbName > $DumpFile    tar czvf $NewFile $DumpFile    rm -rf $DumpFile    echo "[$NewFile] backup completely!" >> $LogFile   fi     #remove the obsolete file   if [ -f $OldFile ]; then    rm -f $OldFile    echo "delete the old file: [$OldFile]"   fi   done
可以将这个脚本加入到cron例行程序里,就可以自动备份数据库了。

来自:http://blog.csdn.net/wangyiyan315/article/details/16944479