Get the App
SLTechnology News&Howtos  ›  Database  › 

Bash script sharing for backing up MySQL database

Shulou Source: shulou.com Published: 2022-05-31 17:46:05 11月13日 Update

This article mainly explains the "backup MySQL database Bash script sharing", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "backup MySQL database Bash script sharing" bar!

#! / bin/bashNOW= `date + "% Ymuri% 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). Thank you for reading, the above is the content of "Bash script sharing for backing up MySQL database". After the study of this article, I believe you have a deeper understanding of the problem of Bash script sharing for backing up MySQL database, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Backup data database script learning content that is ideas situations articles more knowledge knowledge points articles follow questions e.g. practice push research Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS MySQL Linux MariaDB