備份MySQL資料庫的Bash指令碼

來源:互聯網
上載者:User

  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

  Don't forget to make the script executable using the chmod a+x mysqlbackupscript.sh command.

  【別忘了使用chmod a+x mysqlbackupscript.sh命令將指令碼可執行化。】




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.