Plan to back up mysql database _ MySQL

Source: Internet
Author: User
1: mysql is the most widely used Database. if we back up mysql data correctly in daily use, let's do this, implement it through scripts #################################### ######################################## ####### 1: mysql is the most widely used Database. if mysql data is correctly backed up in daily use, we will do this in the next step.

######################################## ######################################## ######################################## ###########
#! /Bin/bash
# Backup My databases
# By luox at 2008-10-24
Source/home/cacti/. bash_profile
PATH =/home/cacti/local/mysql5.0.22/bin:/home/cacti/local/php5.2.6/bin:/home/cacti/local/apache2.2.9/bin: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# Date + % Y-% m-% d
# Backup bbs database
Mysqldump-S/home/cacti/tmp/mysql. sock-u bbsuser-p 'pddwshdzww.zscb' -- default-character-set = gbk -- opt -- extended-insert = false -- hex-blob bbsuser>/home/cacti/backup/bbsuser_bak _ 'date + % Y-% m-% d '. SQL

# Compress the bak files
Bzip2/home/cacti/backup/bbsuser_bak _ 'date + % Y-% m-% d'. SQL


# Remove 4 week old bak files
Find/home/cacti/backup/-name '*. bz2'-ctime + 28-exec rm {}\;

######################################## ######################################## ######################################## ##########
The above script can be used to back up data. a brief introduction is given to the use of the mysqldump command (in bold above, bbsuser refers to the database name)
-S specifies the sock file used by the database. a system can run mysql with multiple ports. if you want to connect to the database, you need to use this parameter to specify the sock file.
-U specifies the user. here, if we back up the bbsuser database, we only need to use the user bbsuser. Of course, the root user can do the same, but it is better to write the script with a smaller permission.
-P: specify the password and use ''to enclose the password.
-- Default-character-set = is used to set the format of the database. this is related to the use of the default language in the database. you can connect to the database and use show variables; to view the current situation. generally, utf8 gbk is used.
-- Opt backup may be the most common method, because of the advantages of backup speed
-- Extended-insert = false is used to export multiple Insert rows. Otherwise, an error may occur because the Insert statement is too long.
-- Using hex-blob to export binary string fields in hexadecimal format
-A, -- all-databases Dump all the databases. This will be same as -- databases with all databases selected. can be used to back up all databases
PS: date + % Y-% m-% d can be used to display the date of the current day. we also add this parameter during backup to mark the date of the backup file. If you reference date in mysqldump, use 'date + % Y-% m-% d'

2: bzip2 can compress the file to bz2 and delete the original file,

3: Use find to search for the data, and then delete the data based on the time. if it is more than 28 days (4 weeks), the data is deleted.

4: You can run the script in Crontab to run the script once a day to back up the data.
######################################## ######################################## #
# Backup my database
5 0 ***/home/cacti/backup/backupmysql. sh>/dev/null 2> & 1
######################################## ######################################## #

3: database import:
/Home/cacti/local/mysql5.0.22/bin/mysql-S/home/cacti/tmp/mysql. sock-ubbsuser-p 'pddwshdzw1_zscb' bbsuser

This article is from the "gzmaster" blog, please be sure to keep this source http://gzmaster.blog.bitscn.com/299556/108896

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.