#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2010-2022. All rights reserved.
# date: 2021-12-22
# version: 1.0
dn_role=""
min_disk=""
old_version=""
new_version=""
old_cfg=""
big_cfg=""
binfile=""
upgrade_path=$(
cd $(dirname $0)
pwd
)
source ${upgrade_path}/upgrade_errorcode.sh
function check_and_init() {
check_config
check_user
check_cmd_conflict
}
function check_config() {
#check and init GAUSS_LOG_FILE
if [[ X"$GAUSS_LOG_PATH" = X ]]; then
GAUSS_LOG_FILE="$upgrade_path"/upgrade.log
else
check_config_path "$GAUSS_LOG_PATH"
GAUSS_LOG_FILE="$GAUSS_LOG_PATH"/upgrade.log
fi
log_dir=$(dirname "$GAUSS_LOG_FILE")
if [[ ! -d "$log_dir" ]]; then
if ! mkdir -p -m 700 "$log_dir"; then
echo "mkdir -p -m 700 ${log_dir} failed"
exit ${err_check_init}
fi
fi
if touch "$GAUSS_LOG_FILE" && chmod 600 "$GAUSS_LOG_FILE"; then
echo "check log file is ok" >>"$GAUSS_LOG_FILE"
else
echo "touch $GAUSS_LOG_FILE && chmod 600 $GAUSS_LOG_FILE failed"
exit ${err_check_init}
fi
check_env
#check GAUSS_LISTEN_PORT
if [[ X"$GAUSS_LISTEN_PORT" = X ]]; then
GAUSS_LISTEN_PORT=$(grep -E "^[ ]{0,}port" "$PGDATA"/postgresql.conf | sed 's/\s*//g' | awk -F# '{print $1}' | awk -F= '{print $2}')
fi
if [[ X"$GAUSS_LISTEN_PORT" = X ]]; then
GAUSS_LISTEN_PORT=5432
debug "The value of port cannot be obtained from configuration, use default value 5432"
fi
if ! echo "$GAUSS_LISTEN_PORT" | grep -Eqw "[0-9]{4,5}"; then
die "GAUSS_LISTEN_PORT may be not right" ${err_check_init}
fi
#check GAUSS_ADMIN_USER
if [[ X"$GAUSS_ADMIN_USER" = X ]]; then
GAUSS_ADMIN_USER=$(whoami)
else
check_config_user "$GAUSS_ADMIN_USER"
fi
#check GAUSS_UPGRADE_BASE_PATH and cd in
if [[ X"$GAUSS_UPGRADE_BASE_PATH" = X ]]; then
GAUSS_UPGRADE_BASE_PATH=${upgrade_path}
fi
check_config_path "$GAUSS_UPGRADE_BASE_PATH"
if [[ ! -d "$GAUSS_UPGRADE_BASE_PATH" ]]; then
die "$GAUSS_UPGRADE_BASE_PATH must exist" ${err_check_init}
fi
if ! cd "$GAUSS_UPGRADE_BASE_PATH"; then
die "Cannot access $GAUSS_UPGRADE_BASE_PATH" ${err_check_init}
fi
#check GAUSS_SQL_TAR_PATH
if [[ X"$GAUSS_SQL_TAR_PATH" = X ]]; then
GAUSS_SQL_TAR_PATH=${GAUSS_UPGRADE_BASE_PATH}
fi
check_config_path "$GAUSS_SQL_TAR_PATH"
#check GAUSS_BACKUP_BASE_PATH
if [[ X"$GAUSS_BACKUP_BASE_PATH" = X ]]; then
GAUSS_BACKUP_BASE_PATH=${GAUSS_UPGRADE_BASE_PATH}/backup
fi
check_config_path "$GAUSS_BACKUP_BASE_PATH"
if [[ ! -d "$GAUSS_BACKUP_BASE_PATH" ]]; then
if ! mkdir -p -m 700 "$GAUSS_BACKUP_BASE_PATH"; then
die "mkdir -p -m 700 $GAUSS_BACKUP_BASE_PATH failed" ${err_check_init}
fi
fi
#check GAUSS_TMP_PATH
if [[ X"$GAUSS_TMP_PATH" = X ]]; then
GAUSS_TMP_PATH=${GAUSS_UPGRADE_BASE_PATH}/tmp
fi
check_config_path "$GAUSS_TMP_PATH"
if [[ ! -d "$GAUSS_TMP_PATH" ]]; then
if ! mkdir -p -m 700 "$GAUSS_TMP_PATH"; then
die "mkdir -p -m 700 $GAUSS_TMP_PATH failed" ${err_check_init}
fi
fi
#check GAUSS_UPGRADE_BIN_PATH
if [[ X"$GAUSS_UPGRADE_BIN_PATH" != X ]]; then
check_config_path "$GAUSS_UPGRADE_BIN_PATH"
fi
#check GAUSS_UPGRADE_SYNC_CONFIG_LIST
check_config_sync_path
}
function usage() {
echo "
Usage: $0 [OPTION]
Arguments:
-h|--help show this help, then exit
-t upgrade_pre,upgrade_bin,upgrade_post,rollback_pre,rollback_bin,rollback_post,upgrade_commit
query_start_mode,switch_over
--min_disk reserved upgrade disk space in MB, default 2048
-m|--mode normal、primary、standby and cascade_standby
"
}
function debug() {
local current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "[$current_time]" "$1" >>"${GAUSS_LOG_FILE}"
}
function log() {
local current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "[$current_time]" "$1" >>"${GAUSS_LOG_FILE}"
echo "$1"
}
function die() {
local current_time=$(date +"%Y-%m-%d %H:%M:%S")
if [[ -f "${GAUSS_LOG_FILE}" ]];then
echo "[$current_time]" "$1" >>"${GAUSS_LOG_FILE}"
fi
echo -e "\033[31mError: $1\033[0m"
exit $2
}
function parse_cmd_line() {
if [[ $# -gt 6 ]];then
log "please input right parameter, the following command may help you"
log "sh upgrade_GAUSSV5.sh --help or sh upgrade_GAUSSV5.sh -h"
exit ${err_parameter}
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
usage
exit 0
;;
-m | --mode)
if [[ "$2" = "" ]]; then
die "the parameter -m|--mode cannot be empty." ${err_parameter}
fi
dn_role=$2
dn_role_list="normal primary standby cascade_standby"
if ! echo "$dn_role_list"|grep -wq "$dn_role"; then
die "only these modes are supported: normal,primary,standby and cascade_standby" ${err_parameter}
fi
shift 2
;;
-t)
if [[ "$2" = "" ]]; then
die "the parameter -t cannot be empty." ${err_parameter}
fi
action=$2
action_list="upgrade_pre upgrade_bin upgrade_post rollback_pre rollback_bin rollback_post upgrade_commit query_start_mode switch_over"
if ! echo "$action_list"|grep -wq "$action"; then
die "only these actions are supported: upgrade_pre, upgrade_bin, upgrade_post, rollback_pre, \
rollback_bin, rollback_post, upgrade_commit and query_start_mode switch_over" ${err_parameter}
fi
shift 2
;;
--min_disk)
if [[ "$2" = "" ]]; then
die "the parameter --min_disk cannot be empty." ${err_parameter}
fi
min_disk=$2
if echo ${min_disk} | grep -q "[^0-9]";then
die "min_disk value must be int " ${err_parameter}
fi
if echo ${min_disk} | grep -q "^0";then
die "min_disk value cannot start with 0 " ${err_parameter}
fi
if [[ ${min_disk} -lt 2048 || ${min_disk} -gt 204800 ]]; then
die "min_disk value must be >= 2048 and <= 204800" ${err_parameter}
fi
shift 2
;;
*)
log "please input right parameter, the following command may help you"
log "sh upgrade_GAUSSV5.sh --help or sh upgrade_GAUSSV5.sh -h"
exit ${err_parameter}
;;
esac
done
log "Parse cmd line successfully."
}
function check_user() {
user=$(whoami)
if [[ X"$user" == X"root" ]]; then
die "Can not exec the script with root!" ${err_check_init}
fi
}
function check_env() {
if [[ "$GAUSSHOME" == "" ]]; then
die "GAUSSHOME cannot be null!" ${err_check_init}
fi
if [[ "$GAUSSDATA" == "" ]] && [[ "$PGDATA" == "" ]]; then
die "GAUSSDATA or PGDATA cannot be all null!" ${err_check_init}
fi
if [[ "$PGDATA" == "" ]]; then
PGDATA=${GAUSSDATA}
fi
if [[ "$GAUSSDATA" == "" ]]; then
GAUSSDATA=${PGDATA}
fi
check_config_path "$GAUSSHOME"
check_config_path "$GAUSSDATA"
check_config_path "$PGDATA"
log "Current env value: GAUSSHOME is $GAUSSHOME, PGDATA is $PGDATA."
}
function check_config_path() {
local temp_value="$1"
if [[ "$temp_value" == *[\(\)\{\}\[\]\<\>\"\'\`\\\ \*\!\|\;\&\$\~\?]* ]];then
die "$temp_value may contain illegal characters" ${err_check_init}
fi
if echo "$temp_value"|grep -Eq "^/{1,}$"; then
die "path cannot be / " ${err_check_init}
fi
}
function check_config_sync_path() {
if [[ "abc$GAUSS_UPGRADE_SYNC_CONFIG_LIST" == "abc" ]]; then
debug "GAUSS_UPGRADE_SYNC_CONFIG_LIST is null"
fi
if [[ "$GAUSS_UPGRADE_SYNC_CONFIG_LIST" == *[\(\)\{\}\[\]\<\>\"\'\`\\\ \*\!\|\;\&\$\~\?]* ]];then
die "$GAUSS_UPGRADE_SYNC_CONFIG_LIST may contain illegal characters" ${err_check_init}
fi
local array=(${GAUSS_UPGRADE_SYNC_CONFIG_LIST//,/ })
for var in ${array[@]}
do
if echo "$var"|grep -Eq "^/"; then
die "path in ${GAUSS_UPGRADE_SYNC_CONFIG_LIST} must not be start with /" ${err_check_init}
fi
done
}
function check_config_user() {
local temp_value="$1"
if [[ "$temp_value" == *[\(\)\{\}\[\]\<\>\"\'\`\\\ \*\!\|\