##### Database backup script ##############################
# Parameter description ########################
# Parameter 1: Database Name
# Parameter 2: Database Server
# Parameter 3: User
# Parameter 4: Password
################################
# Database backup path
Bakdir =/opt/data/modules/mysql_back /;
CD $ bakdir;
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 = $ bakdir $ db' _ '$ day
[-A $ db_dir] | mkdir $ db_dir; echo "mkdir $ db_dir"
# Getting the table list in the database
Echo 'list = ('echo "show tables;" | mysql-h $ DBH-U $ dbu-p $ DBP $ db ')'
List = ('echo "show tables;" | 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
Mysqldump $ DBH $ dbu $ DBP $ db $ table | gzip-C> $ db_dir/mongodb.w.table.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
# Deleting backups 10 days ago
Del_day = 'date-d' 10 days ago '+ % Y % m % d ';
Del_dir = $ bakdir $ db '_' $ del_day;
Echo "delete dir:". $ del_dir;
Rm-RF $ del_dir;
##### Database recovery script ##############################
#! /Bin/bash
# Parameter 1: database backup path
# Parameter 2: Database Name
# Check Param
If [$ #! = 2]
Then
Echo "Param error! ";
Exit;
Else
If [! -D "$1"]
Then
Echo "dir: $1 is not exist! ";
Exit;
Fi
Fi
[$1] & rootpath = $1;
[$2] & DB = $2;
CD $ rootpath;
# Insert DB
For I in $ rootpath /*
Do
If [-F "$ I"]
Then
Echo $ I;
Gunzip $ I;
/Opt/modules/MySQL/bin/MySQL-uroot-p223238 $ db <symbol I +#.gz };
Gzip $ {I % .*};
Fi
Done