備份MySQL數據庫的Bash腳本
【如果你管理有運行在在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 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 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 name):
【你可以手動運行這一腳本,或者設置一個計劃作業(a cron job)以使備份按計劃自動進行。設置計劃作業的方法是,運行crontab -e命令,并加入以下代碼(請用實際路徑和備份腳本名替換示例路徑):】
@daily /path/to/mysqlbackup.sh
Don't forget to make the executable using the chmod a+x mysqlbackup.sh command.
【別忘了使用chmod a+x mysqlbackup.sh命令將腳本可執行化。】
關健詞:Bash,腳本
新文章:
- CentOS7下圖形配置網絡的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統后丟失windows啟動項
- CentOS單網卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網打印機IP講解
- CentOS7使用hostapd實現無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網絡重啟出錯
- 解決Centos7雙系統后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統有什么不同呢
- Centos 6.6默認iptable規則詳解