Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / packages / base / src / files / bin / backup.sh
1 ###
2 # ============LICENSE_START=======================================================
3 # ECOMP Policy Engine
4 # ================================================================================
5 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10
11 #      http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
19 ###
20
21 #!/bin/bash
22
23 #########################################################################
24 ##
25 ## Functions
26 ##
27 #########################################################################
28
29 function usage() {
30         echo -n "syntax: $(basename $0) "
31         echo -n "--debug ("
32         echo -n "[--backup <backup-dir-location>] | "
33         echo -n "[--restore <backup-dir-location>])"
34 }
35
36 function backup() {
37         if [[ $DEBUG == y ]]; then
38                 echo "-- ${FUNCNAME[0]} $@ --"
39                 set -x
40         fi
41         
42         if [[ -z ${POLICY_HOME} ]]; then
43                 echo "error: ${POLICY_HOME} is not set"
44                 exit 1
45         fi
46         
47         BACKUP_DIR=$1   
48         if [[ -z ${BACKUP_DIR} ]]; then
49                 echo "error: a backup directory must be provided"
50                 usage
51                 exit 1
52         fi
53         
54         /bin/mkdir -p ${BACKUP_DIR} > /dev/null 2>&1
55         if [[ ! -d ${BACKUP_DIR} ]]; then
56                 echo "error: ${BACKUP_DIR} is not a directory"
57                 exit 1
58         fi      
59         
60         if [[ ! -w ${BACKUP_DIR} ]] ; then
61                 echo "error: ${BACKUP_DIR} is not writable"
62                 exit 1  
63         fi
64         
65         if [ "$(ls -A ${BACKUP_DIR})" ]; then
66                 echo "error: ${BACKUP_DIR} must be empty"
67                 exit 1
68         fi
69         
70         echo "backing up ${POLICY_HOME} to ${BACKUP_DIR} to.."
71         rsync -a --delete \
72                         --exclude logs \
73                         --exclude tmp \
74                         --exclude backup \
75                         --exclude servers/pap/webapps/pap \
76                         --exclude servers/pdp/webapps/pdp \
77                         --exclude servers/console/webapps/policy \
78                         ${POLICY_HOME}/* \
79                         ${BACKUP_DIR}
80 }
81
82 function restore() {
83         if [[ $DEBUG == y ]]; then
84                 echo "-- ${FUNCNAME[0]} $@ --"
85                 set -x
86         fi
87         
88         if [[ -z ${POLICY_HOME} ]]; then
89                 echo "error: ${POLICY_HOME} is not set"
90                 exit 1
91         fi
92         
93         BACKUP_DIR=$1   
94         if [[ -z ${BACKUP_DIR} ]]; then
95                 echo "error: a backup directory must be provided"
96                 usage
97                 exit 1
98         fi
99         
100         if [[ ! -d ${BACKUP_DIR} ]]; then
101                 echo "error: ${BACKUP_DIR} is not a directory"
102                 exit 1
103         fi      
104         
105         if [ "$(ls -A ${BACKUP_DIR})" ]; then
106                 echo "OK: ${BACKUP_DIR} has content"
107         else
108                 echo "error: ${BACKUP_DIR} is empty"
109                 exit 1
110         fi
111         
112         echo "restoring from ${BACKUP_DIR} to ${POLICY_HOME} .."
113         rsync -a ${BACKUP_DIR}/* ${POLICY_HOME}
114 }
115
116 OPERATION=none
117 DEBUG=n
118
119 # command line options parsing
120 until [[ -z "$1" ]]; do
121         case $1 in
122                 -d|--debug)     DEBUG=y
123                                                 set -x
124                                                 ;;
125                 -b|--backup)    OPERATION=backup
126                                                 shift
127                                                 DIR=$1          
128                                                 ;;
129                 -r|--restore)   OPERATION=restore
130                                                 shift
131                                                 DIR=$1
132                                                 ;;                                              
133                 *)                              usage
134                                                 exit 1
135                                                 ;;
136         esac
137         shift
138 done
139
140 # operation validation
141 case $OPERATION in
142         backup)         backup $DIR
143                                 ;;
144         restore)        restore $DIR
145                                 ;;
146         *)              echo "invalid operation (${OPERATION}): must be in {backup|restore}";
147                         usage
148                         exit 1
149                         ;;
150 esac