#! /bin/bash
###################################
# backup database from mysql
###################################
###### VARIABLES & FUNCTIONS ######
# Database info
DB_HOST=""
DB_USER=""
DB_PWD=""
DB_NAME=""
# Others vars
FILE_CONF="/etc/system_conf.xml" #the configure xml file
BIN_DIR="/usr/bin" #the mysql bin path
BAK_DIR="/home/mysql/backup" #the backup file directory
MAX_FILES=10 #the maximum number of backup files
PARAM_VAL="" #the xml values
TABLE_PRE="tbl_" #the need table's prefix
DB_RET=""
CUR_TIME=`date +%F_%T`
# tables don't need backup
EXTABLES=(
"tbl_name_a" \
"tbl_name_b" \
"tbl_name_c"
)
# only all tables' data
DUMP_ARG="-n --hex-blob"
#which table's need backup
DUMPTABLES=""
# -------------------------------------------------
# get one param value from xml file:
# <param name="param01" value="1"/>
# param_name MUST be unique one
# return: PARAM_VAL_OLD
# -------------------------------------------------
get_xml_param_val()
{
local my_file=$1
local my_param=$2
PARAM_VAL=`cat $my_file | grep $my_param | awk '{print $3}' | cut -f 2 -d '"'`
}
get_dump_tables()
{
#get all tables in cur db
ALLTABLES=`mysql -h${DB_HOST} -u${DB_USER} -p${DB_PWD} -B ${DB_NAME} -e "show tables"|grep "^${TABLE_PRE}"`
if [ $? -ne 0 ]
then
echo "###### show all tables in ${DB_NAME} fail"
return 1
fi
if [ "${ALLTABLES}" = "" ]
then
echo "###### no tables in ${DB_NAME}"
return 1
fi
#get all tables need bk
for table in ${ALLTABLES}
do
#check whether table is in EXTABLES
index=0
tbs_num=${#EXTABLES[*]}
need_bk=1
while [ ${index} -lt ${tbs_num} ]
do
if [ -n "${EXTABLES[$index]}" ]
then
if [ "${table}" = "${EXTABLES[$index]}" ]
then
need_bk=0
break;
fi
fi
index=`expr ${index} + 1`
done
if [ ${need_bk} -eq 1 ]
then
DUMPTABLES=`echo ${DUMPTABLES} ${table}`
fi
done
return 0
}
rotate_bk_files()
{
#cp old.n to older.m
index=`expr ${MAX_FILES} - 1`
while [ ${index} -gt 0 ]
do
src=${BAK_DIR}/${DB_NAME}_backup.sql.$((${index} - 1))
dst=${BAK_DIR}/${DB_NAME}_backup.sql.${index}
if [ -e "${src}" ]
then
mv ${src} ${dst}
if [ $? -ne 0 ]
then
echo "###### rotate ${src} to ${dst} fail"
return 1
fi
fi
index=`expr ${index} - 1`
done
#cp new to old.0
src=${BAK_DIR}/${DB_NAME}_backup.sql
dst=${BAK_DIR}/${DB_NAME}_backup.sql.0
if [ -e "${src}" ]
then
mv ${src} ${dst}
if [ $? -ne 0 ]
then
echo "###### rotate ${src} to ${dst} fail"
return 1
fi
fi
return 0
}
###### main() ######
# check user role
ROLE=`id | awk '{print $1}' | sed -e 's/=/ /' -e 's/(/ /' -e 's/)/ /' |awk '{print $3}'`
if [ "${ROLE}" != "root" ]; then
echo "[ERROR]Operation not permitted! Please run cmd by root, or sudo cmd ..."
exit 1
fi
if [ ! -d ${BAK_DIR} ]; then
echo "create backup dir: ${BAK_DIR}"
mkdir -p ${BAK_DIR}
fi
### get db host
get_xml_param_val ${FILE_CONF} "mysql_url"
DB_HOST="${PARAM_VAL}"
if [ "${DB_HOST}" = "" ]; then
echo "[ERROR] can't get mysql_url from ${FILE_CONF}"
exit 1
fi
### get db user
get_xml_param_val ${FILE_CONF} "mysql_user"
DB_USER="${PARAM_VAL}"
if [ "${DB_USER}" = "" ]; then
echo "[ERROR] can't get mysql_user from ${FILE_CONF}"
exit 1
fi
### get db pwd
get_xml_param_val ${FILE_CONF} "mysql_pwd"
DB_PWD="${PARAM_VAL}"
if [ "${DB_PWD}" = "" ]; then
echo "[ERROR] can't get mysql_pwd from ${FILE_CONF}"
exit 1
fi
### get db name
get_xml_param_val ${FILE_CONF} "mysql_db"
DB_NAME="${PARAM_VAL}"
if [ "${DB_NAME}" = "" ]; then
echo "[ERROR] can't get mysql_db from ${FILE_CONF}"
exit 1
fi
# check database exist
DB_RET=`mysql -h${DB_HOST} -u${DB_USER} -p${DB_PWD} -e "show databases like '${DB_NAME}'"|egrep "^${DB_NAME}$"`
if [ "${DB_RET}" != "${DB_NAME}" ]; then
echo "****** db ${DB_NAME} backup fail!"
exit 1
fi
# get tables need backup
get_dump_tables
if [ $? -ne 0 ]; then
exit 1
fi
#todo backup
mysqldump -h${DB_HOST} -u${DB_USER} -p${DB_PWD} -B ${DB_NAME} ${DUMP_ARG} --tables ${DUMPTABLES} >${BAK_DIR}/${DB_NAME}_backup.sql
if [ $? -ne 0 ]; then
exit 1
fi
# chmod backup files
chmod 600 ${BAK_DIR}/${DB_NAME}_backup.sql
# rotate backup files
rotate_bk_files
if [ $? -ne 0 ]; then
exit 1
fi
ls -lhst --full-time ${BAK_DIR}
echo "****** Backup '${DB_NAME}' Success!"
exit 0
附录: