MySQL back up shell by table

Source: Internet
Author: User

#! /Bin/bash

If [$1] # If no parameter exists
Then
Echo database $1;
Else
Echo 'backup database tables to gzip files sh bak_db_table.sh DB [db_host] [db_user] [db_passwd] '; exit;
Fi

# Parameter assignment
[$1] & DB = $1
[$2] & DBH = $2
[$3] & dbu = $3
[$4] & DBP = $4

# Configure database Parameters
[$ DBH] & DBH = "-h" $ DBH
[$ Dbu] & dbu = "-U" $ dbu
[$ DBP] & DBP = "-P" $ DBP

Day = 'date + % Y % m % d'

# Create a directory named after dB if it does not exist
Db_dir =/opt/db_bak/$ db' _ '$ day
 
[-A $ db_dir] | mkdir $ db_dir; echo "mkdir $ db_dir"

# Obtain the list of all tables in the database (crontab may not recognize the MySQL command, so use the absolute path)
List = ('echo show tables |/opt/modules/MySQL/bin/MySQL $ DBH $ dbu $ DBP $ db ')

# Getting the number of tables
Tbcount = $ ($ {# list [@]}-2 ))
If [$ tbcount =-2] # If the table does not exist or the database connection fails
Then
Echo empty tables in $ db or connent failed! ; Exit 1;
Fi
Echo table count: $ tbcount

# Back up and compress each table
# Decompress the package using gzip-d $ File
# Use MySQL $ database <$ bakfile for restoration
For (I = 1; I <($ tbcount + 1); I ++ ))
Do
Table =$ {list [$ I]}
Echo back table $ table
Echo $ DBH $ dbu $ DBP $ db $ table
Mysqldump $ DBH $ dbu $ DBP $ db $ table | gzip-C> $ db_dir/230.90.db.mongodb.mongotable.gz
# $? The Return Value of the previous command. 0 indicates success. 1 indicates failure, but mysqldump does not comply with this Convention.
Echo back table $?
Sleep 10
Done

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.