From 91d04c64771832a0b8815ffbe1f0f9920320d94d Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Tue, 14 Feb 2017 19:41:00 -0500 Subject: Initial OpenECOMP policy/engine commit Change-Id: I7dbff37733b661643dd4d1caefa3d7dccc361b6e Signed-off-by: Pamela Dragosh --- .../src/files/install/mysql/bin/cleanup_policy.sh | 92 +++++++++++ .../base/src/files/install/mysql/bin/db_backup.sh | 84 ++++++++++ .../src/files/install/mysql/bin/db_backup_data.sh | 84 ++++++++++ .../files/install/mysql/bin/db_backup_remote.sh | 88 +++++++++++ .../base/src/files/install/mysql/bin/db_restore.sh | 97 ++++++++++++ .../src/files/install/mysql/bin/db_restore_data.sh | 103 ++++++++++++ .../base/src/files/install/mysql/bin/db_upgrade.sh | 150 ++++++++++++++++++ .../files/install/mysql/bin/db_upgrade_remote.sh | 173 +++++++++++++++++++++ 8 files changed, 871 insertions(+) create mode 100644 packages/base/src/files/install/mysql/bin/cleanup_policy.sh create mode 100644 packages/base/src/files/install/mysql/bin/db_backup.sh create mode 100644 packages/base/src/files/install/mysql/bin/db_backup_data.sh create mode 100644 packages/base/src/files/install/mysql/bin/db_backup_remote.sh create mode 100644 packages/base/src/files/install/mysql/bin/db_restore.sh create mode 100644 packages/base/src/files/install/mysql/bin/db_restore_data.sh create mode 100644 packages/base/src/files/install/mysql/bin/db_upgrade.sh create mode 100644 packages/base/src/files/install/mysql/bin/db_upgrade_remote.sh (limited to 'packages/base/src/files/install/mysql/bin') diff --git a/packages/base/src/files/install/mysql/bin/cleanup_policy.sh b/packages/base/src/files/install/mysql/bin/cleanup_policy.sh new file mode 100644 index 000000000..1eec53f96 --- /dev/null +++ b/packages/base/src/files/install/mysql/bin/cleanup_policy.sh @@ -0,0 +1,92 @@ +#!/bin/bash +### +# ============LICENSE_START======================================================= +# ECOMP Policy Engine +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +# cleanup_policy.sh: Run this script to delete policy record marked as 'deleted' +# +# Usage : cleanup_policy.sh db_user db_user_password retention_period +# Example: cleanup_policy.sh policy_user password 90 +# +# +# + +. $HOME/.profile + +DB_USER="" +DB_PASSWORD="" +RETENTION_PERIOD="" +DATE=`date +"%Y%m%d"` +DATETIME=`date +"%Y%m%d%H%M%S"` +LOG="" +ERR="" + +function cleanup_deleted_policy +{ + # 1 + echo "1- cleanup_deleted_policy [policyGroupEntity] ... `date`" | tee -a $LOG + ${MYSQL} -e "delete from xacml.policyGroupEntity where policyId in ( select policyId from xacml.policyEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)) and groupKey in ( select groupKey from xacml.groupEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)); " 2>&1 | tee -a $LOG + echo "--" | tee -a $LOG + + # 2 + echo "2- cleanup_deleted_policy [pdpEntity] ... `date`" | tee -a $LOG + ${MYSQL} -e "delete from xacml.pdpEntity where groupKey in ( select groupKey from xacml.groupEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)); " 2>&1 | tee -a $LOG + echo "--" | tee -a $LOG + + # 3 + echo "3- cleanup_deleted_policy [groupEntity] ... `date`" | tee -a $LOG + ${MYSQL} -e "delete from xacml.groupEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY); " 2>&1 | tee -a $LOG + echo "--" | tee -a $LOG + + # 4 + echo "4- cleanup_deleted_policy [policyEntity] ... `date`" | tee -a $LOG + ${MYSQL} -e "delete from xacml.policyEntity where configurationDataId in ( select configurationDataId from xacml.configurationDataEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)) and actionBodyId in ( select actionBodyId from xacml.actionBodyEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)); " 2>&1 | tee -a $LOG + echo "--" | tee -a $LOG + + # 5 + echo "5- cleanup_deleted_policy [configurationDataEntity] ... `date`" | tee -a $LOG + ${MYSQL} -e "delete from xacml.configurationDataEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY); " 2>&1 | tee -a $LOG + echo "--" | tee -a $LOG + + # 6 + echo "6- cleanup_deleted_policy [actionBodyEntity] ... `date`" | tee -a $LOG + ${MYSQL} mysql --verbose -u${DB_USER} -p${DB_PASSWORD} -e "delete from xacml.actionBodyEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY); " 2>&1 | tee -a $LOG + echo "--" | tee -a $LOG +} + +# MAIN +LOG=$POLICY_HOME/logs/cleanup_policy_$DATE.log +ERR=$POLICY_HOME/logs/cleanup_policy_$DATE.err +echo "cleanup_policy.sh started ... `date`" | tee -a $LOG +if [ $# -eq 3 ]; then + DB_USER="${1}" + DB_PASSWORD="${2}" + RETENTION_PERIOD="${3}" + echo "DB_USER: $DB_USER" | tee -a $LOG + + typeset -r MYSQL="mysql -u${DB_USER} -p${DB_PASSWORD} --verbose "; + + cleanup_deleted_policy + + echo "cleanup_policy.sh completed ... `date`" | tee -a $LOG +else + echo "Usage : cleanup_policy.sh db_user_id db_user_password retention_period" + echo "Example: cleanup_policy.sh policy_user password 90" +fi + diff --git a/packages/base/src/files/install/mysql/bin/db_backup.sh b/packages/base/src/files/install/mysql/bin/db_backup.sh new file mode 100644 index 000000000..896c4ff2d --- /dev/null +++ b/packages/base/src/files/install/mysql/bin/db_backup.sh @@ -0,0 +1,84 @@ +#!/bin/bash +### +# ============LICENSE_START======================================================= +# ECOMP Policy Engine +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +# +# db_backup.sh: Run this script to back database to a file +# +# Usage : db_backup.sh db_user db_user_password database +# Example: db_backup.sh policy_user password ecomp_sdk +# +# Note: mysqldump requires at least the SELECT privilege for dumped tables +# +# + +. $HOME/.profile + +DB_USER="" +DB_PASSWORD="" +DATABASE="" +DATE=`date +"%Y%m%d"` +DATETIME=`date +"%Y%m%d%H%M%S"` +DAILY_BACKUP_DIR="" +LOG="" +ERR="" + +function create_backup_dir +{ + if [ ! -d $DAILY_BACKUP_DIR ]; then + echo "Create DAILY_BACKUP_DIR [$DAILY_BACKUP_DIR] ..." + mkdir -p $DAILY_BACKUP_DIR 2>&1 + fi +} + +function backup_database +{ + echo "backup_database [$DATABASE] started ...@`date`" | tee -a $LOG + + BACKUP_FILE=$DAILY_BACKUP_DIR/backup_${DATABASE}_${DATETIME}.sql + echo $BACKUP_FILE + mysqldump --user=${DB_USER} --password=${DB_PASSWORD} --databases ${DATABASE} > $BACKUP_FILE + echo "" | tee -a $LOG + echo "database backup file --> $BACKUP_FILE" | tee -a $LOG + echo "" | tee -a $LOG + echo "backup_database [$DATABASE] completed ...@`date`" | tee -a $LOG +} + + +# MAIN +echo "db_backup.sh started ... `date`" | tee -a $LOG +if [ $# -eq 3 ]; then + DB_USER="${1}" + DB_PASSWORD="${2}" + DATABASE="${3}" + echo "DB_USER: $DB_USER" | tee -a $LOG + + DAILY_BACKUP_DIR=$POLICY_HOME/data/mysql/$DATE + LOG=$POLICY_HOME/logs/db_backup_$DATE.log + ERR=$POLICY_HOME/logs/db_backup_$DATE.err + create_backup_dir + + backup_database + echo "db_backup.sh completed ... `date`" | tee -a $LOG +else + echo "Usage : db_backup.sh db_user_id db_user_password database" + echo "Example: db_backup.sh policy_user password ecomp_sdk" +fi + diff --git a/packages/base/src/files/install/mysql/bin/db_backup_data.sh b/packages/base/src/files/install/mysql/bin/db_backup_data.sh new file mode 100644 index 000000000..39df9e272 --- /dev/null +++ b/packages/base/src/files/install/mysql/bin/db_backup_data.sh @@ -0,0 +1,84 @@ +#!/bin/bash +### +# ============LICENSE_START======================================================= +# ECOMP Policy Engine +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +# +# db_backup_data.sh: Run this script to backup database DATA only +# +# Usage : db_backup_data.sh db_user db_user_password database +# Example: db_backup_data.sh policy_user password xacml +# +# Note: mysqldump requires at least the SELECT privilege for dumped tables +# +# + +. $HOME/.profile + +DB_USER="" +DB_PASSWORD="" +DATABASE="" +DATE=`date +"%Y%m%d"` +DATETIME=`date +"%Y%m%d%H%M%S"` +DAILY_BACKUP_DIR="" +LOG="" +ERR="" + +function create_backup_dir +{ + if [ ! -d $DAILY_BACKUP_DIR ]; then + echo "Create DAILY_BACKUP_DIR [$DAILY_BACKUP_DIR] ..." + mkdir -p $DAILY_BACKUP_DIR 2>&1 + fi +} + +function backup_database +{ + echo "backup_database [$DATABASE] started ...@`date`" | tee -a $LOG + + BACKUP_FILE=$DAILY_BACKUP_DIR/backup_${DATABASE}_data_${DATETIME}.sql + echo $BACKUP_FILE + mysqldump --no-create-info --no-create-db --user=${DB_USER} --password=${DB_PASSWORD} --databases ${DATABASE} > $BACKUP_FILE + echo "" | tee -a $LOG + echo "database backup file --> $BACKUP_FILE" | tee -a $LOG + echo "" | tee -a $LOG + echo "backup_database [$DATABASE] completed ...@`date`" | tee -a $LOG +} + + +# MAIN +LOG=$POLICY_HOME/logs/db_backup_data_$DATE.log +ERR=$POLICY_HOME/logs/db_backup_data_$DATE.err +echo "db_backup_data.sh started ... `date`" | tee -a $LOG +if [ $# -eq 3 ]; then + DB_USER="${1}" + DB_PASSWORD="${2}" + DATABASE="${3}" + echo "DB_USER: $DB_USER" | tee -a $LOG + + DAILY_BACKUP_DIR=$POLICY_HOME/data/mysql/$DATE + create_backup_dir + + backup_database + echo "db_backup_data.sh completed ... `date`" | tee -a $LOG +else + echo "Usage : db_backup_data.sh db_user_id db_user_password database" + echo "Example: db_backup_data.sh policy_user password xacml" +fi + diff --git a/packages/base/src/files/install/mysql/bin/db_backup_remote.sh b/packages/base/src/files/install/mysql/bin/db_backup_remote.sh new file mode 100644 index 000000000..00ee95269 --- /dev/null +++ b/packages/base/src/files/install/mysql/bin/db_backup_remote.sh @@ -0,0 +1,88 @@ +#!/bin/bash +### +# ============LICENSE_START======================================================= +# ECOMP Policy Engine +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= + +### +# +# db_backup_remote.sh: Perform database backup from remote +# +# Usage : db_backup_remote.sh db_user db_user_password database db_hostname +# Example: db_backup_remote.sh policy_user password ecomp_sdk localhost.com +# +# Note: 1. mysqldump utility must be available in the env where this script intend to run +# 2. db_user requires at least the SELECT privilege for dumped tables +# +# + +. $HOME/.profile + +DB_USER="" +DB_PASSWORD="" +DATABASE="" +DB_HOSTNAME="" +DATE=`date +"%Y%m%d"` +DATETIME=`date +"%Y%m%d%H%M%S"` +DAILY_BACKUP_DIR="" +LOG="" +ERR="" + +function create_backup_dir +{ + if [ ! -d $DAILY_BACKUP_DIR ]; then + echo "Create DAILY_BACKUP_DIR [$DAILY_BACKUP_DIR] ..." + mkdir -p $DAILY_BACKUP_DIR 2>&1 + fi +} + +function backup_database +{ + echo "backup database [$DATABASE]@[${DB_HOSTNAME}] started ...@`date`" | tee -a $LOG + + BACKUP_FILE=$DAILY_BACKUP_DIR/backup_${DATABASE}_${DB_HOSTNAME}_${DATETIME}.sql + #echo $BACKUP_FILE + mysqldump --user=${DB_USER} --password=${DB_PASSWORD} --databases ${DATABASE} -h ${DB_HOSTNAME} > $BACKUP_FILE + echo "" | tee -a $LOG + echo "database backup file --> $BACKUP_FILE" | tee -a $LOG + echo "" | tee -a $LOG + echo "backup database [$DATABASE]@[${DB_HOSTNAME}] completed ...@`date`" | tee -a $LOG +} + + +# MAIN +if [ $# -eq 4 ]; then + DB_USER="${1}" + DB_PASSWORD="${2}" + DATABASE="${3}" + DB_HOSTNAME="${4}" + echo "db_backup_remote.sh for [$DATABASE]@[${DB_HOSTNAME}] started ... `date`" | tee -a $LOG + echo "DB_USER : $DB_USER" | tee -a $LOG + echo "DATABASE : $DATABASE" | tee -a $LOG + echo "DB_HOSTNAME: $DB_HOSTNAME" | tee -a $LOG + + DAILY_BACKUP_DIR=$POLICY_HOME/data/mysql/$DATE + LOG=$POLICY_HOME/logs/db_backup_remote_$DATE.log + ERR=$POLICY_HOME/logs/db_backup_remote_$DATE.err + create_backup_dir + + backup_database + echo "db_backup_remote.sh for [$DATABASE]@[${DB_HOSTNAME}] completed ... `date`" | tee -a $LOG +else + echo "Usage : db_backup_remote.sh db_user_id db_user_password database db_hostname" + echo "Example: db_backup_remote.sh policy_user password ecomp_sdk localhost.com" +fi diff --git a/packages/base/src/files/install/mysql/bin/db_restore.sh b/packages/base/src/files/install/mysql/bin/db_restore.sh new file mode 100644 index 000000000..b0e32dac3 --- /dev/null +++ b/packages/base/src/files/install/mysql/bin/db_restore.sh @@ -0,0 +1,97 @@ +#!/bin/bash +### +# ============LICENSE_START======================================================= +# ECOMP Policy Engine +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +# +# db_restore.sh: Restore database table(s) from database backup file +# +# Usage : db_restore.sh db_user db_password backup_file database table_name +# Example: db_restore.sh policy_user password /opt/app/policy/data/mysql/20150901/backup_ecomp_sdk_20150910102030.sql ecomp_sdk attribute +# db_restore.sh policy_user password /opt/app/policy/data/mysql/20150901/backup_ecomp_sdk_20150910102030.sql ecomp_sdk all +# +# Assumption: Database backup_file is created from mysqldump utility +# +# Note: use lower case table name +# +# + +DB_USER="" +DB_PASSWORD="" +BACKUP_FILE="" +DATABASE="" +TABLE="" +TEMP_FILE=/tmp/db_restore_$$.sql + +function restore_all +{ + echo "restore_all started ...@`date`" + echo "Before restore table ..." + echo "--" + mysql -u${DB_USER} -p${DB_PASSWORD} < $BACKUP_FILE + echo "--" + + echo "restore_all completed ...@`date`" +} + +function restore_table +{ + database="${1}" + table="${2}" + echo "restore_table [$database] [$table] started ...@`date`" + # extract sql statement from backup file + echo "use $database;" > $TEMP_FILE + echo "set foreign_key_checks=0; " >> $TEMP_FILE + sed -n -e '/DROP TABLE IF EXISTS `'$table'`;/,/UNLOCK TABLES;/p' $BACKUP_FILE >> $TEMP_FILE + echo "set foreign_key_checks=1; " >> $TEMP_FILE + echo "--" + cat $TEMP_FILE + echo "--" + echo "Before restore table ..." + mysql -u${DB_USER} -p${DB_PASSWORD} < $TEMP_FILE + echo "--" + echo "restore_table [$database] [$table] completed ...@`date`" +} + + +# MAIN +echo "db_restore.sh started ... `date`" +if [ $# -eq 5 ]; then + DB_USER="${1}" + DB_PASSWORD="${2}" + BACKUP_FILE="${3}" + typeset -l DATABASE="${4}" + typeset -l TABLE="${5}" + echo "DB_USER: $DB_USER" + if [ -f $BACKUP_FILE ]; then + if [ "${TABLE}" != "all" ]; then + restore_table ${DATABASE} ${TABLE} + else + restore_all + fi + else + echo "BACKUP FILE NOT FOUND: $BACKUP_FILE" + fi +else + echo "Usage : db_restore.sh db_user_id db_password backup_file database table_name" + echo "Example: db_restore.sh policy_user password /opt/app/policy/data/mysql/20150901/backup_ecomp_sdk_20150901102030.sql ecomp_sdk attribute" +fi + +rm -f $TEMP_FILE +echo "db_restore.sh completed ... `date`" diff --git a/packages/base/src/files/install/mysql/bin/db_restore_data.sh b/packages/base/src/files/install/mysql/bin/db_restore_data.sh new file mode 100644 index 000000000..926755996 --- /dev/null +++ b/packages/base/src/files/install/mysql/bin/db_restore_data.sh @@ -0,0 +1,103 @@ +#!/bin/bash +### +# ============LICENSE_START======================================================= +# ECOMP Policy Engine +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +# +# db_restore_data.sh: Restore only data for database table(s) from database backup file +# +# Usage : db_restore_data.sh db_user db_password backup_file database table_name +# Example: db_restore_data.sh policy_user password /opt/app/policy/data/mysql/20150901/backup_xacml_data_20150910102030.sql xacml attribute +# db_restore_data.sh policy_user password /opt/app/policy/data/mysql/20150901/backup_xacml_data_20150910102030.sql xacml all +# +# Assumption: +# 1. Database backup_file is created from db_backup_data.sh (contains only data) +# 2. Data in table(s) will be wiped out and loaded from backup file +# +# Note: use lower case table name +# +# + +DB_USER="" +DB_PASSWORD="" +BACKUP_FILE="" +DATABASE="" +TABLE="" +TEMP_FILE=/tmp/db_restore_data_$$.sql + +function restore_all +{ + echo "restore_all started ...@`date`" + echo "set foreign_key_checks=0;" > $TEMP_FILE + sed -e 's/LOCK TABLES \(.*\) WRITE;/delete from \1; LOCK TABLES \1 WRITE;/g' $BACKUP_FILE >> $TEMP_FILE + echo "set foreign_key_checks=1;" >> $TEMP_FILE + #cat $TEMP_FILE + echo "Before restore table ..." | tee -a $LOG + echo "--" | tee -a $LOG + mysql -u${DB_USER} -p${DB_PASSWORD} --verbose < $BACKUP_FILE 2>&1 | tee -a $LOG + echo "--" | tee -a $LOG + echo "restore_all completed ...@`date`" +} + +function restore_table +{ + database="${1}" + table="${2}" + echo "restore_table [$database] [$table] started ...@`date`" + # extract sql statement from backup file + echo "use $database;" > $TEMP_FILE + echo "set sql_safe_updates=0;" >> $TEMP_FILE + echo "delete from $table;" >> $TEMP_FILE + sed -n -e '/LOCK TABLES `'$table'` WRITE;/,/UNLOCK TABLES;/p' $BACKUP_FILE >> $TEMP_FILE + echo "set sql_safe_updates=1;" >> $TEMP_FILE + #echo "--" + #cat $TEMP_FILE + echo "Before restore table ..." 2>&1 | tee -a $LOG + echo "--" | tee -a $LOG + mysql -u${DB_USER} -p${DB_PASSWORD} --verbose < $TEMP_FILE 2>&1 | tee -a $LOG + echo "--" | tee -a $LOG + echo "restore_table [$database] [$table] completed ...@`date`" +} + + +# MAIN +echo "db_restore_data.sh started ... `date`" +if [ $# -eq 5 ]; then + DB_USER="${1}" + DB_PASSWORD="${2}" + BACKUP_FILE="${3}" + typeset -l DATABASE="${4}" + typeset -l TABLE="${5}" + echo "DB_USER: $DB_USER" + if [ -f $BACKUP_FILE ]; then + if [ "${TABLE}" != "all" ]; then + restore_table ${DATABASE} ${TABLE} + else + restore_all + fi + else + echo "BACKUP FILE NOT FOUND: $BACKUP_FILE" + fi +else + echo "Usage : db_restore_data.sh db_user_id db_password backup_file database table_name" + echo "Example: db_restore_data.sh policy_user password /opt/app/policy/data/mysql/20150901/backup_xacml_data_20150901102030.sql xacml attribute" +fi + +rm -f $TEMP_FILE +echo "db_restore_data.sh completed ... `date`" diff --git a/packages/base/src/files/install/mysql/bin/db_upgrade.sh b/packages/base/src/files/install/mysql/bin/db_upgrade.sh new file mode 100644 index 000000000..41b79e1ef --- /dev/null +++ b/packages/base/src/files/install/mysql/bin/db_upgrade.sh @@ -0,0 +1,150 @@ +#!/bin/bash +# ============LICENSE_START======================================================= +# ECOMP Policy Engine +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### +# +# db_upgrade.sh: Run this script to upgrade database to a given release level, it is recommanded switch to policy user to run this script +# +# Usage : db_upgrade.sh target_db_release_level db_user_id db_user_password +# Example: db_upgrade.sh 151000 policy_user password +# +# Assumption: 1. DB upgrade sql script in $POLICY_HOME/data/mysql folder with read permission +# 2. DB user has privilege to create/drop/alter database table +# +# Note: The default location for db release script is $POLICY_HOME/data/mysql +# The release level is represented as Two-digit-Year+Two-digit-Month+two-digit-Sub-release (151000, 151001) +# +# + +TARGET_RELEASE="" +CURRENT_RELEASE="" +DB_UPGRADE_USER="" +DB_UPGRADE_PASSWORD="" +DB_UPGRADE_DIR=$POLICY_HOME/data/mysql +DATE=`date +"%Y%m%d%H%M%S"` +LOG="" +ERR="" + +function get_current_release_level +{ + echo "Get current release level started ...@`date`" | tee -a $LOG + # display output vertical + query="select version from support.db_version where the_key = 'VERSION' \G" + CURRENT_RELEASE=`${MYSQL} --skip-column-names --execute "${query}" 2>$ERR | grep -v "*"` + echo "CURRENT_RELEASE: [$CURRENT_RELEASE]" | tee -a $LOG + echo "Get current release level completed ...@`date`" | tee -a $LOG +} + +function evaluate_upgrade_downgrade +{ + echo "CURRENT_RELEASE --> [$CURRENT_RELEASE]" | tee -a $LOG + echo "TARGET_RELEASE --> [$TARGET_RELEASE] " | tee -a $LOG + if [[ "${CURRENT_RELEASE}" < "${TARGET_RELEASE}" ]]; then + # perform db upgrade + UPGRADE_LIST=/tmp/db_upgrade_list.$$ + find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort > $UPGRADE_LIST + while read -r file + do + RELEASE=`basename $file | cut -d'_' -f1` + #echo "[$RELEASE] [$TARGET_RELEASE]" | tee -a $LOG + if [ "${RELEASE}" -gt "${CURRENT_RELEASE}" ] && [ "${RELEASE}" -le "${TARGET_RELEASE}" ]; then + run_script "UPGRADE" "${file}" 2>&1 | tee -a $LOG + fi + done < $UPGRADE_LIST + rm -f $UPGRADE_LIST + set_current_release_level $TARGET_RELEASE + elif [[ "${CURRENT_RELEASE}" > "${TARGET_RELEASE}" ]]; then + # perform db downgrade + DOWNGRADE_LIST=/tmp/db_downgrade_list.$$ + find ${DB_UPGRADE_DIR} -name "*_downgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort -r > $DOWNGRADE_LIST + while read -r file + do + RELEASE=`basename $file | cut -d'_' -f1` + #echo "[$RELEASE] [$TARGET_RELEASE]" | tee -a $LOG + if [ "${RELEASE}" -le "${CURRENT_RELEASE}" ] && [ "${RELEASE}" -gt "${TARGET_RELEASE}" ]; then + run_script "DOWNGRADE" "${file}" + fi + done < $DOWNGRADE_LIST + rm -f $DOWNGRADE_LIST + set_current_release_level $TARGET_RELEASE + else + echo "CURRENT DB RELEASE LEVEL THE SAME AS TARGET RELEASE LEVEL, NO ACTION TAKEN ..." | tee -a $LOG + fi +} + +function run_script +{ + action="${1}" + script="${2}" + echo "Perform DB $action use $script ..." | tee -a $LOG + echo "--" | tee -a $LOG + ${MYSQL} --verbose < "${script}" 2>$ERR | tee -a $LOG + echo "--" | tee -a $LOG +} + +function set_current_release_level +{ + RELEASE="${1}" + echo "Set current release level to [$RELEASE] started ...@`date`" | tee -a $LOG + update_statement="insert into support.db_version (the_key, version) values ('VERSION', '${RELEASE}') on duplicate key update version='${RELEASE}';" + ${MYSQL} --execute "${update_statement}" + + echo "" | tee -a $LOG + echo "CURRENT_RELEASE set to: [$RELEASE]" | tee -a $LOG + echo "" | tee -a $LOG + echo "Set current release level completed ...@`date`" | tee -a $LOG +} + +function check_directory +{ + if [ ! -d $DB_UPGRADE_DIR ]; then + echo "ERROR, DIRECTORY NOT EXIST: $DB_UPGRADE_DIR, PROCESS EXIT ..." + exit; + else + if [ ! -d $DB_UPGRADE_DIR/logs ]; then + mkdir $DB_UPGRADE_DIR/logs + fi + fi +} + +# MAIN +#check_directory +LOG=$POLICY_HOME/logs/db_upgrade_$DATE.log +ERR=$POLICY_HOME/logs/db_upgrade_$DATE.err +echo "db_upgrade.sh started ..." | tee -a $LOG +if [ $# -eq 3 ]; then + TARGET_RELEASE="${1}" + DB_UPGRADE_USER="${2}" + DB_UPGRADE_PASSWORD="${3}" + echo "TARGET_RELEASE : $TARGET_RELEASE" | tee -a $LOG + echo "DB_UPGRADE_USER: $DB_UPGRADE_USER" | tee -a $LOG + echo "DB_UPGRADE_DIR : $DB_UPGRADE_DIR" | tee -a $LOG + # + if [ ${#TARGET_RELEASE} -ne 6 ]; then + echo "ERROR, TARGET_RELEASE MUST BE 6 DIGITS: $TARGET_RELEASE" | tee -a $LOG | tee -a $ERR + else + typeset -r MYSQL="mysql -u${DB_UPGRADE_USER} -p${DB_UPGRADE_PASSWORD} "; + get_current_release_level + evaluate_upgrade_downgrade + fi +else + echo "Usage : db_upgrade.sh target_release_level db_user_id db_user_password" | tee -a $LOG + echo "Example: db_upgrade.sh 151000 policy_user password" | tee -a $LOG +fi + +echo "db_upgrade.sh completed ..." | tee -a $LOG diff --git a/packages/base/src/files/install/mysql/bin/db_upgrade_remote.sh b/packages/base/src/files/install/mysql/bin/db_upgrade_remote.sh new file mode 100644 index 000000000..a5b6e77da --- /dev/null +++ b/packages/base/src/files/install/mysql/bin/db_upgrade_remote.sh @@ -0,0 +1,173 @@ +#!/bin/bash +### +# ============LICENSE_START======================================================= +# ECOMP Policy Engine +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= + +### +# +# db_upgrade_remote.sh: This script is to perform database schema upgrade on remote db, +# no shecma downgrade will be performed in case db_version is higher then target_version +# +# Logic: 1. Get target schema version from db scripts in $POLICY_HOME/data/mysql +# 2. Get current db schema version from support.db_version table (of target system) +# 3. Apply db upgrade script in order if target_version is HIGHER than db_version +# 4. Print out warning message if target_version is LOWER than db_version +# 4. Print out message if target_version is EQUAL to db_version +# +# +# Usage : db_upgrade_remote.sh db_user_id db_user_password hostname +# Example: db_upgrade_remote.sh policy_user password localhost.com +# +# Assumption: 1. DB schema upgrade script in $POLICY_HOME/data/mysql folder with read permission +# 2. DB user has privilege to create/drop/alter database table +# +# Note: The default location for db schema upgrade script is $POLICY_HOME/data/mysql +# The release level is represented as Two-digit-Year+Two-digit-Month+two-digit-Sub-release (151000, 151001) +# +# + +TARGET_SCHEMA_VERSION="" +CURRENT_SCHEMA_VERSION="" +DB_UPGRADE_USER="" +DB_UPGRADE_PASSWORD="" +DB_HOSTNAME="" +DB_UPGRADE_DIR=$POLICY_HOME/data/mysql +DATE=`date +"%Y%m%d%H%M%S"` +LOG="" +ERR="" + +function get_current_schema_version +{ + echo "Get current schema version from [${DB_HOSTNAME}] started ...@`date`" | tee -a $LOG + # display output vertical + query="select version from support.db_version where the_key = 'VERSION' \G" + CURRENT_SCHEMA_VERSION=`${MYSQL} --skip-column-names --execute "${query}" 2>$ERR | grep -v "*"` + error_msg=`cat $ERR | grep "doesn't exist"` + if [ "${error_msg}" != "" ]; then + echo "Create support.db_version table ..." | tee -a $LOG + sql="create database if not exists support;" + ${MYSQL} --execute "${sql}" + sql="create table support.db_version(the_key varchar(20) not null, version varchar(20), primary key(the_key));" + ${MYSQL} --execute "${sql}" + CURRENT_SCHEMA_VERSION="00" + fi + echo "CURRENT_SCHEMA_VERSION: [$CURRENT_SCHEMA_VERSION]" | tee -a $LOG + echo "Get current schema version from [${DB_HOSTNAME}] completed ...@`date`" | tee -a $LOG +} + +function get_target_schema_version +{ + UPGRADE_LIST=/tmp/db_upgrade_list.$$ + find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort -r | head -1 > $UPGRADE_LIST + while read -r file + do + TARGET_SCHEMA_VERSION=`basename $file | cut -d'_' -f1` + echo "TARGET_SCHEMA_VERSION: [$TARGET_SCHEMA_VERSION]" | tee -a $LOG + break + done < $UPGRADE_LIST + rm -f $UPGRADE_LIST +} + +function evaluate_upgrade_downgrade +{ + echo "CURRENT_SCHEMA_VERSION --> [$CURRENT_SCHEMA_VERSION]" | tee -a $LOG + echo "TARGET_SCHEMA_VERSION --> [$TARGET_SCHEMA_VERSION] " | tee -a $LOG + if [[ "${CURRENT_SCHEMA_VERSION}" < "${TARGET_SCHEMA_VERSION}" ]]; then + # perform db upgrade + UPGRADE_LIST=/tmp/db_upgrade_list.$$ + find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort > $UPGRADE_LIST + while read -r file + do + DB_VERSION=`basename $file | cut -d'_' -f1` + #echo "[$DB_VERSION] [$TARGET_SCHEMA_VERSION]" | tee -a $LOG + if [ "${DB_VERSION}" -gt "${CURRENT_SCHEMA_VERSION}" ] && [ "${DB_VERSION}" -le "${TARGET_SCHEMA_VERSION}" ]; then + run_script "UPGRADE" "${file}" 2>&1 | tee -a $LOG + fi + done < $UPGRADE_LIST + rm -f $UPGRADE_LIST + set_current_release_level $TARGET_SCHEMA_VERSION + elif [[ "${CURRENT_SCHEMA_VERSION}" > "${TARGET_SCHEMA_VERSION}" ]]; then + # db downgrade + echo "WARNING: Target db schema version is LOWER than current db scema version, please run downgrade script manually." | tee -a $LOG | tee -a $ERR + else + echo "CURRENT SCHEMA VERSION THE SAME AS TARGET SCHEMA VERSION, NO ACTION TAKEN ..." | tee -a $LOG + fi +} + +function run_script +{ + action="${1}" + script="${2}" + echo "Perform DB $action on [${DB_HOSTNAME}] use $script ..." | tee -a $LOG + echo "--" | tee -a $LOG + ${MYSQL} --verbose < "${script}" 2>$ERR | tee -a $LOG + echo "--" | tee -a $LOG +} + +function set_current_release_level +{ + DB_VERSION="${1}" + echo "Set current release level on [${DB_HOSTNAME}] to [$DB_VERSION] started ...@`date`" | tee -a $LOG + update_statement="insert into support.db_version (the_key, version) values ('VERSION', '${DB_VERSION}') on duplicate key update version='${DB_VERSION}';" + ${MYSQL} --execute "${update_statement}" + + echo "" | tee -a $LOG + echo "CURRENT_SCHEMA_VERSION set to: [$DB_VERSION]" | tee -a $LOG + echo "" | tee -a $LOG + echo "Set current release level on [${DB_HOSTNAME}] to [$DB_VERSION] completed ...@`date`" | tee -a $LOG +} + +function check_directory +{ + if [ ! -d $DB_UPGRADE_DIR ]; then + echo "ERROR, DIRECTORY NOT EXIST: $DB_UPGRADE_DIR, PROCESS EXIT ..." + exit; + else + if [ ! -d $DB_UPGRADE_DIR/logs ]; then + mkdir $DB_UPGRADE_DIR/logs + fi + fi +} + +# MAIN +#check_directory +LOG=$POLICY_HOME/logs/db_upgrade_remote_$DATE.log +ERR=$POLICY_HOME/logs/db_upgrade_remote_$DATE.err +echo "db_upgrade_remote.sh started ..." | tee -a $LOG +if [ $# -eq 3 ]; then + DB_UPGRADE_USER="${1}" + DB_UPGRADE_PASSWORD="${2}" + DB_HOSTNAME="${3}" + echo "DB_UPGRADE_USER: $DB_UPGRADE_USER" | tee -a $LOG + echo "DB_UPGRADE_DIR : $DB_UPGRADE_DIR" | tee -a $LOG + echo "DB_HOSTNAME : $DB_HOSTNAME" | tee -a $LOG + # + typeset -r MYSQL="mysql -u${DB_UPGRADE_USER} -p${DB_UPGRADE_PASSWORD} -h ${DB_HOSTNAME}"; + get_target_schema_version + if [ ${#TARGET_SCHEMA_VERSION} -ne 6 ]; then + echo "ERROR, TARGET_SCHEMA_VERSION MUST BE 6 DIGITS: $TARGET_SCHEMA_VERSION" | tee -a $LOG | tee -a $ERR + else + get_current_schema_version + evaluate_upgrade_downgrade + fi +else + echo "Usage : db_upgrade_remote.sh db_user_id db_user_password db_hostname" | tee -a $LOG + echo "Example: db_upgrade_remote.sh policy_user password localhost.com" | tee -a $LOG +fi + +echo "db_upgrade_remote.sh completed ..." | tee -a $LOG -- cgit 1.2.3-korg