程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 備份MySQL數據庫的Bash腳本

備份MySQL數據庫的Bash腳本

編輯:MySQL綜合教程

  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命令將腳本可執行化。】

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved