备份MySQL数据库的简单Bash脚本

xyxzfj 发布于 2011/01/18 16:22
阅读 719
收藏 1
Back up MySQL Databases with a Simple Bash Script
Jan 12, 2011 5:59pm GMT
Dmitri Popov

If you host your own blog or any Web-based application running on the stack, you should have a backup system in place for keeping data stored in MySQL databases safe. There are several solutions that can help you with that, but nothing beats a simple Bash script I stumbled upon in a blog post comment. Here is the script in all its beauty:
【如果你管理有运行在在 Apache/MySQL/PHP栈上自己的博客或基于Web的应用,你应当有一个备份系统,以保证MySQL数据库中数据的安全。当然有一些好办法,但 没一个比得上一个简单的Bash脚本。那是我再一则博客评论上偶然发现的。一下就是美尽在其中的那一脚本:】

#!/bin/bashNOW=`date +"%Y-%m"`;BACKUPDIR="location/of/your/backup/dir/$NOW";### Server Setup ####* MySQL login user name *#MUSER="user";#* MySQL login PASSWORD name *#MPASS="pass";#* MySQL login HOST name *#MHOST="your-mysql-ip";MPORT="your-mysql-port";# DO NOT BACKUP these databasesIGNOREDB="information_schemamysqltest"#* MySQL binaries *#MYSQL=`which mysql`;MYSQLDUMP=`which mysqldump`;GZIP=`which gzip`;# assuming that /nas is mounted via /etc/fstabif [ ! -d $BACKUPDIR ]; then mkdir -p $BACKUPDIRelse :fi# get all database listingDBS="$(mysql -u $MUSER -p$MPASS -h $MHOST -P $MPORT -Bse 'show databases')"# SET DATE AND TIME FOR THE FILENOW=`date +"d%dh%Hm%Ms%S"`; # day-hour-minute-sec format# start to dump database one by onefor db in $DBSdo DUMP="yes"; if [ "$IGNOREDB" != "" ]; then for i in $IGNOREDB # Store all value of $IGNOREDB ON i do if [ "$db" == "$i" ]; then # If result of $DBS(db) is equal to $IGNOREDB(i) then DUMP="NO"; # SET value of DUMP to "no" #echo "$i database is being ignored!"; fi done fi if [ "$DUMP" == "yes" ]; then # If value of DUMP is "yes" then backup database FILE="$BACKUPDIR/$NOW-$db.gz"; echo "BACKING UP $db"; $MYSQLDUMP --add-drop-database --opt --lock-all-tables -u $MUSER -p$MPASS -h $MHOST -P $MPORT $db | gzip > $FILE fidone

The best part is that you only need to specify a handful of parameters to make the script work. This includes BACKUPDIR (the destination for storing backups), MUSER (MySQL user), MPASS (MySQL user password), MHOST (the IP address of the MySQL server, e.g. localhost), and MPORT (the port the MySQL database is running on, default is 3306).
【它的最大优点在于,在运行脚本之前,你只需要定义一些参数。这包括BACKUPDIR(存储本分的目录),MUSER(MySQL用户),MPASS(MySQL用户密码),MHOST(MySQL服务器IP地址,如: localhost),和MPORT(MySQL数据库工作的端口,默认是3306)。】

You can run the script manually, or you can set up a cron job which will perform backups on a regular basis. To do this, run the crontab -e command and add the following line (replace the sample path with the actual path and backup script name):
【你可以手动运行这一脚本,或者设置一个计划作业(a cron job)以使备份按计划自动进行。设置计划作业的方法是,运行crontab -e命令,并加入以下代码(请用实际路径和备份脚本名替换示例路径):】

@daily /path/to/mysqlbackupscript.sh
Don't forget to make the script executable using the chmod a+x mysqlbackupscript.sh command.
【别忘了使用 chmod a+x mysqlbackupscript.sh命令将脚本可执行化。】

英文原文:http://www.linuxpromagazine.com/Online/Blogs/Productivity-Sauce/Back-up-MySQL-Databases-with-a-Simple-Bash-Script
转载请注明:Linux人社区 英文资讯翻译专版 编译
http://www.linux-ren.org/modules/newbb/viewtopic.php?topic_id=58717&post_id=322236#forumpost322236
加载中
返回顶部
顶部