aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base/src/files/bin/backup.sh
blob: f65eed069034cb68be8a355cd3781d970be2f9e7 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
###
# ============LICENSE_START=======================================================
# ONAP 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=========================================================
###

#!/bin/bash

#########################################################################
##
## Functions
##
#########################################################################

function usage() {
	echo -n "syntax: $(basename $0) "
	echo -n "--debug ("
	echo -n "[--backup <backup-dir-location>] | "
	echo -n "[--restore <backup-dir-location>])"
}

function backup() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi
	
	if [[ -z ${POLICY_HOME} ]]; then
		echo "error: ${POLICY_HOME} is not set"
		exit 1
	fi
	
	BACKUP_DIR=$1	
	if [[ -z ${BACKUP_DIR} ]]; then
		echo "error: a backup directory must be provided"
		usage
		exit 1
	fi
	
	/bin/mkdir -p ${BACKUP_DIR} > /dev/null 2>&1
	if [[ ! -d ${BACKUP_DIR} ]]; then
		echo "error: ${BACKUP_DIR} is not a directory"
		exit 1
	fi	
	
	if [[ ! -w ${BACKUP_DIR} ]] ; then
		echo "error: ${BACKUP_DIR} is not writable"
		exit 1	
	fi
	
	if [ "$(ls -A ${BACKUP_DIR})" ]; then
		echo "error: ${BACKUP_DIR} must be empty"
		exit 1
	fi
	
	echo "backing up ${POLICY_HOME} to ${BACKUP_DIR} to.."
	rsync -a --delete \
			--exclude logs \
			--exclude tmp \
			--exclude backup \
			--exclude servers/pap/webapps/pap \
			--exclude servers/pdp/webapps/pdp \
			--exclude servers/console/webapps/policy \
			${POLICY_HOME}/* \
			${BACKUP_DIR}
}

function restore() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi
	
	if [[ -z ${POLICY_HOME} ]]; then
		echo "error: ${POLICY_HOME} is not set"
		exit 1
	fi
	
	BACKUP_DIR=$1	
	if [[ -z ${BACKUP_DIR} ]]; then
		echo "error: a backup directory must be provided"
		usage
		exit 1
	fi
	
	if [[ ! -d ${BACKUP_DIR} ]]; then
		echo "error: ${BACKUP_DIR} is not a directory"
		exit 1
	fi	
	
	if [ "$(ls -A ${BACKUP_DIR})" ]; then
		echo "OK: ${BACKUP_DIR} has content"
	else
		echo "error: ${BACKUP_DIR} is empty"
		exit 1
	fi
	
	echo "restoring from ${BACKUP_DIR} to ${POLICY_HOME} .."
	rsync -a ${BACKUP_DIR}/* ${POLICY_HOME}
}

OPERATION=none
DEBUG=n

# command line options parsing
until [[ -z "$1" ]]; do
	case $1 in
		-d|--debug) 	DEBUG=y
						set -x
						;;
		-b|--backup) 	OPERATION=backup
						shift
						DIR=$1		
						;;
		-r|--restore) 	OPERATION=restore
						shift
						DIR=$1
						;;						
		*)				usage
						exit 1
						;;
	esac
	shift
done

# operation validation
case $OPERATION in
	backup) 	backup $DIR
				;;
	restore) 	restore $DIR
				;;
	*)		echo "invalid operation (${OPERATION}): must be in {backup|restore}";
			usage
			exit 1
			;;
esac