备份MySQL数据库的Bash脚本分享

2023-05-20

这篇文章主要讲解了“备份MySQL数据库的Bash脚本分享”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“备份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--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).

感谢各位的阅读,以上就是“备份MySQL数据库的Bash脚本分享”的内容了,经过本文的学习后,相信大家对备份MySQL数据库的Bash脚本分享这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是本站,小编将为大家推送更多相关知识点的文章,欢迎关注!

《备份MySQL数据库的Bash脚本分享.doc》

下载本文的Word格式文档,以方便收藏与打印。