blob: 75cca73884ee577a7e79dbc0607dbf56f64028dd [file] [log] [blame]
Pamela Dragosh91d04c62017-02-14 19:41:00 -05001###
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
29function 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
36function 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/pypdp/webapps/PyPDPServer \
78 --exclude servers/console/webapps/policy \
79 ${POLICY_HOME}/* \
80 ${BACKUP_DIR}
81}
82
83function restore() {
84 if [[ $DEBUG == y ]]; then
85 echo "-- ${FUNCNAME[0]} $@ --"
86 set -x
87 fi
88
89 if [[ -z ${POLICY_HOME} ]]; then
90 echo "error: ${POLICY_HOME} is not set"
91 exit 1
92 fi
93
94 BACKUP_DIR=$1
95 if [[ -z ${BACKUP_DIR} ]]; then
96 echo "error: a backup directory must be provided"
97 usage
98 exit 1
99 fi
100
101 if [[ ! -d ${BACKUP_DIR} ]]; then
102 echo "error: ${BACKUP_DIR} is not a directory"
103 exit 1
104 fi
105
106 if [ "$(ls -A ${BACKUP_DIR})" ]; then
107 echo "OK: ${BACKUP_DIR} has content"
108 else
109 echo "error: ${BACKUP_DIR} is empty"
110 exit 1
111 fi
112
113 echo "restoring from ${BACKUP_DIR} to ${POLICY_HOME} .."
114 rsync -a ${BACKUP_DIR}/* ${POLICY_HOME}
115}
116
117OPERATION=none
118DEBUG=n
119
120# command line options parsing
121until [[ -z "$1" ]]; do
122 case $1 in
123 -d|--debug) DEBUG=y
124 set -x
125 ;;
126 -b|--backup) OPERATION=backup
127 shift
128 DIR=$1
129 ;;
130 -r|--restore) OPERATION=restore
131 shift
132 DIR=$1
133 ;;
134 *) usage
135 exit 1
136 ;;
137 esac
138 shift
139done
140
141# operation validation
142case $OPERATION in
143 backup) backup $DIR
144 ;;
145 restore) restore $DIR
146 ;;
147 *) echo "invalid operation (${OPERATION}): must be in {backup|restore}";
148 usage
149 exit 1
150 ;;
151esac