备份MySQL数据库的Bash脚本

来源:未知 责任编辑:智问网络 发表时间:2013-11-04 19:54 点击:

  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 -ecommand 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

  Dont forget to make the script executable using the chmod a+x mysqlbackupscript.sh command.

  【别忘了使用chmod a+x mysqlbackupscript.sh命令将脚本可执行化。】

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • mysql-mmm
    • mysqldump命令——MySQL数据库备份还原
    • Oracle数据导入MySQL的快捷工具:MySQL Migration Toolkit
    • 简简单单储存过程——循环一个select结果集
    • MySQL数据库十大优化技巧
    • Mysql主主复制架构配置
    • Mysql安装笔记
    • MySQL Stmt预处理提高效率问题的小研究
    • Mysql的Procedure 参数为NULL问题分析
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1