aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base/src/files/install/mysql/bin/cleanup_policy.sh
blob: 1eec53f96ef192777527af0a0e33898ae3ed9095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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