#! /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