mysql备份还原

「1/1」shell

  • mysql备份还原

#引入函数库
BASE_DIRNAME=$(dirname $0)
DATEFORMAT=$(date -u “+%F%H%M%S”)

#备份
DB_HOST=”127.0.0.1″
DB_POST=”3306″
DB_NAME=”com1″
DB_USER_NAME=”root”
DB_USER_PASSWORD=”589cef9d8e3045c5″
DATA_BACKUP_ROOT=”${BASE_DIRNAME}/”
DATA_BACKUP_NAME=”${DB_NAME}-${DATEFORMAT}.sql”
DATA_BACKUP_FILE=${DATA_BACKUP_ROOT}${DATA_BACKUP_NAME}

#还原
DB_HOST_TO=”127.0.0.1″
DB_POST_TO=”3306″
DB_NAME_TO=”com2″
DB_USER_NAME_TO=${DB_USER_NAME}
DB_USER_PASSWORD_TO=${DB_USER_PASSWORD}

CMD_FROM=”-u${DB_USER_NAME} -p${DB_USER_PASSWORD} -h${DB_HOST} -P${DB_POST} ${DB_NAME}”
CMD_TO=”-u${DB_USER_NAME_TO} -p${DB_USER_PASSWORD_TO} -h${DB_HOST_TO} -P${DB_POST_TO} ${DB_NAME_TO}”

#echo ${CMD_FROM}
#echo ${CMD_TO}
#echo ${DATA_BACKUP_FILE}

#备份
echo “mysqldump ${CMD_FROM} > ${DATA_BACKUP_FILE}”
mysqldump ${CMD_FROM} > ${DATA_BACKUP_FILE}

#恢复
echo “mysql ${CMD_TO} < ${DATA_BACKUP_FILE}”
mysql ${CMD_TO} < ${DATA_BACKUP_FILE}

#删除
if [ “${DATA_BACKUP_FILE}” != “” ];
then
echo “rm -rf ${DATA_BACKUP_FILE}”
rm -rf ${DATA_BACKUP_FILE}
fi

发表评论