


備份MySQL數(shù)據(jù)庫的Bash腳本
【如果你管理有運行在在Apache/MySQL/PHP棧上自己的博客或基于Web的應用,你應當有一個備份系統(tǒng),以保證MySQL數(shù)據(jù)庫中數(shù)據(jù)的安全。當然有一些好辦法,但沒一個比得上一個簡單的Bash腳本。那是我再一則博客評論上偶然發(fā)現(xiàn)的。一下就是美盡在其中的那一腳本:】
#!/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).
【它的最大優(yōu)點在于,在運行腳本之前,你只需要定義一些參數(shù)。這包括BACKUPDIR(存儲本分的目錄),MUSER(MySQL用戶),MPASS(MySQL用戶密碼),MHOST(MySQL服務(wù)器IP地址,如:localhost),和MPORT(MySQL數(shù)據(jù)庫工作的端口,默認是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):
【你可以手動運行這一腳本,或者設(shè)置一個計劃作業(yè)(a cron job)以使備份按計劃自動進行。設(shè)置計劃作業(yè)的方法是,運行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命令將腳本可執(zhí)行化。】
關(guān)健詞:Bash,腳本
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動項
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機IP講解
- CentOS7使用hostapd實現(xiàn)無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯
- 解決Centos7雙系統(tǒng)后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認iptable規(guī)則詳解