Back up the Bash script of the MySQL database

Source: Internet
Author: User
Tags mysql login

If you host your own blog or any Web-based application running on the stack, you shoshould have a backup system in place for keeping data stored in MySQL databases safe. there are several solutions that can help you with that, but nothing beats a simple Bash script I stumbled upon in a blog post comment. here is the script in all its beauty:

[If you manage your own blog or Web-based applications running on the Apache/MySQL/PHP stack, you should have a backup system to ensure data security in the MySQL database. Of course there are some good methods, but none of them is similar to a simple Bash script. That was my occasional blog comment. Here is the script that is perfect :]

#! /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 my'; MYSQLDUMP = 'which mysqldump '; GZIP = 'which g Zip '; # 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-mongodb.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 des 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 ).

[Its biggest advantage is that you only need to define some parameters before running the script. This includes BACKUPDIR (directory for storing the current score), MUSER (MySQL user), MPASS (MySQL user password), MHOST (MySQL Server IP address, for example: localhost ), and MPORT (the port used by the MySQL database, which is 3306 by default ).]

You can run the script 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 script name ):

[You can manually run this script, or set a scheduled job (a cron job) to automatically perform the backup as planned. To schedule a job, run the crontab-e command and add the following code (replace the sample path with the actual path and backup script name ):]

@ Daily/path/to/mysqlbackupscript. sh

Dont forget to make the script executable using the chmod a + x mysqlbackupscript. sh command.

[Do not forget to run the chmod a + x mysqlbackupscript. sh command to make the script executable .]

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.