To back up your MySQL database bash script

Source: Internet
Author: User
Tags add date chmod mysql mysql database mysql login backup

If you host your own blog or no web-based application running on the stack, should have a backup system Keeping data stored in MySQL databases safe. There are several solutions that can help for you, and but nothing beats a simple Bash script I stumbled upon in a blog Post comment. Here are the script in all its beauty:

"If you manage to have your own blog or web-based application running on the apache/mysql/php stack, you should have a backup system to secure the data in the MySQL database." Of course there are some good options, but none is comparable to a simple bash script. That's what I stumbled upon in a blog comment. It's the one that's beautiful. The script: "

  #!/bin/bashnow= ' date + '%y-%m '; Backupdir= "location/of/your/backup/dir/$NOW"; ### Server Setup ####* mysql Login user name * #MUSER = "user"; #* mysql login P Assword 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 D Atabases ') "# 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 ' I do if [' $db ' = ' $i ']; Then # If result of $DBS (db) are equal to $IGNOREDB (i) then dump= "NO"; # SET value of DUMP to "no" #echo $i the database is BeinG ignored! "; Fi done fi If ["$DUMP" = = "yes"]; Then # If value 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 ' You ' need to specify a handful ' parameters to ' script work. This is 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's MySQL database is running on, default is 3306).

"The great thing about it is that you just need to define some parameters before you run the script." This includes backupdir (storing this part of the directory), Muser (MySQL user), mpass (mysql user password), mhost (MySQL server IP address, such as: localhost), and mport (MySQL database working port, The default is 3306). 】

You can run the script manually, or your can set up a cron job which'll perform backups on a regular basis. To does this, run the Crontab-ecommand and add the following line (replace the sample path with the actual path and backup Script name):

"You can run this script manually, or set up a scheduled job (a cron job) to automate the backup as scheduled. To set up a scheduled job, run the CRONTAB-E command and add the following code (replace the sample path with the actual path and the backup script name): "

@daily/path/to/mysqlbackupscript.sh

Don ' t forget to make the script executable using the chmod a+x mysqlbackupscript.sh command.

"Don't forget to use the chmod a+x mysqlbackupscript.sh command to execute the script. 】




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.