备份MySQL数据库的Bash脚本分享
这篇文章主要讲解了“备份MySQL数据库的Bash脚本分享”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“备份MySQL数据库的Bash脚本分享”吧!
#!/bin/bashNOW=`date+"%Y-%m"`;BACKUPDIR="location/of/your/backup/dir/$NOW";###ServerSetup####*MySQLloginusername*#MUSER="user";#*MySQLloginPASSWORDname*#MPASS="pass";#*MySQLloginHOSTname*#MHOST="your--ip";MPORT="your-mysql-port";#DONOTBACKUPthesedatabasesIGNOREDB="information_schemamysqltest"#*MySQLbinaries*#MYSQL=`whichmysql`;MYSQLDUMP=`whichmysqldump`;GZIP=`whichgzip`;#assumingthat/nasismountedvia/etc/fstabif[!-d$BACKUPDIR];thenmkdir-p$BACKUPDIRelse:fi#getalldatabaselistingDBS="$(mysql-u$MUSER-p$MPASS-h$MHOST-P$MPORT-Bseshowdatabases)"#SETDATEANDTIMEFORTHEFILENOW=`date+"d%dh%Hm%Ms%S"`;#day-hour-minute-secformat#starttodumpdatabaseonebyonefordbin$DBSdoDUMP="yes";if["$IGNOREDB"!=""];thenforiin$IGNOREDB#Storeallvalueof$IGNOREDBONidoif["$db"=="$i"];then#Ifresultof$DBS(db)isequalto$IGNOREDB(i)thenDUMP="NO";#SETvalueofDUMPto"no"#echo"$idatabaseisbeingignored!";fidonefiif["$DUMP"=="yes"];then#IfvalueofDUMPis"yes"thenbackupdatabaseFILE="$BACKUPDIR/$NOW-$db.gz";echo"BACKINGUP$db";$MYSQLDUMP--add-drop-database--opt--lock-all-tables-u$MUSER-p$MPASS-h$MHOST-P$MPORT$db|gzip>$FILEfidone Thebestpartisthatyouonlyneedtospecifyahandfulofparameterstomakethescriptwork.ThisincludesBACKUPDIR(thedestinationforstoringbackups),MUSER(MySQLuser),MPASS(MySQLuserpassword),MHOST(theIPaddressoftheMySQLserver,e.g.localhost),andMPORT(theporttheMySQLdatabaseisrunningon,defaultis3306).
感谢各位的阅读,以上就是“备份MySQL数据库的Bash脚本分享”的内容了,经过本文的学习后,相信大家对备份MySQL数据库的Bash脚本分享这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。