自动备份mysql数据库 shell脚本

来源:互联网 发布:程序员android工作总结 编辑:程序博客网 时间:2024/05/16 15:42

最近使用了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 listDbTestCase=testlink #testlink的数据库名称DbBug=bugtracker   #mantis在mysql里的数据库名称DbUser=rootDbPwd=adminBackupPath=/root/mysql_backup/LogFile=/root/mysql_backup/log_file#check the backup file exists or notif [ ! -d $BackupPath ]; thenmkdir $BackupPathfifor DbName in $DbTestCase $DbBug doNewFile="$BackupPath""$DbName"$(date +%y%m%d).tar.gzDumpFile="$BackupPath""$DbName"$(date ++%y%m%d).sqlOldFile="$BackupPath""$DbName"$(date +%y%m%d --date='1 weeks ago').tar.gzecho "-----------------------------------------"echo $(date +"%y-%m-%d %H:%M:%S")echo "-----------------------------------------"#create new backup file weeklyif [ -f $NewFile ]; thenecho "New backup file have exists!"else/opt/lampp/bin/mysqldump -uroot -padmin $DbName > $DumpFiletar czvf $NewFile $DumpFilerm -rf $DumpFileecho "[$NewFile] backup completely!" >> $LogFilefi#remove the obsolete fileif [ -f $OldFile ]; thenrm -f $OldFileecho "delete the old file: [$OldFile]"fi done
可以将这个脚本加入到cron例行程序里,就可以自动备份数据库了。

原创粉丝点击