-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.rc
189 lines (175 loc) · 5.17 KB
/
backup.rc
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# vim:ft=sh
# shellcheck shell=bash
# shellcheck disable=SC2086
# backup.rc
function checkCommand {
for COMMAND_NAME in $* ; do
local COMMAND_CHECK=$(which $COMMAND_NAME)
if [[ -z $COMMAND_CHECK ]] ; then
echo "$COMMAND_NAME was not found, check path or install"
exit 1
fi
done
}
# Check if region information is available for aws commands
checkCommand aws curl cut awk cat
if [[ ! -f ~/.aws/config ]] ; then
export REGION=$(curl --silent http://169.254.169.254/latest/meta-data/placement/availability-zone | cut -c1-9)
if [[ -z $REGION ]] ; then
echo "Cannot detect region. If not running inside an amazon environment,"
echo "use aws configure to set it"
exit 1
else
AWSREGION="$AWSREGION"
fi
fi
export AWSCMD="aws $AWSREGION --output text"
export AWSGET="aws $AWSREGION --output json"
# User/site specific variables, such as TARGET_TYPE and TARGET_LOCATION can be set in the backup.user.rc file
RCUSERFILE="$(dirname $(readlink -f $0))/backup.user.rc"
if [[ -z $TARGET_TYPE || -z $TARGET_LOCATION || -z $CONDA_ROOT || -z $CONDA_ENV || -z $SCRIPTS_BUCKET ]] ; then
if [[ -f $RCUSERFILE ]] ; then
source $RCUSERFILE
echo "Loaded ${RCUSERFILE}:"
echo "------------------------------------------------------------------------"
cat $RCUSERFILE
echo "------------------------------------------------------------------------"
else
echo "user rc file $RCUSERFILE not found, exiting...."
exit 1
fi
fi
# The folowing can be overwridden by setting the values at the command line or in backup.user.rc file:
# variable value desctiptio
# TARGET_TYPE FS Backup to a file system
# GIT Backup to a git repository
# S3 Backup to an S3 bucket
# if no value is provided for TARGET_TYPE in backup.user.rc, default to FS
if [[ -z $TARGET_TYPE ]] ; then
TARGET_TYPE="FS"
fi
# for TARGET_TYPE=GIT, check if git is available
if [[ "$TARGET_TYPE" == "GIT" ]] ; then
checkCommand git
fi
# The workdir is the base location to which the backup will take place. You can change the setting the values for the FS and GIT targets at the command line or in backup.user.rc file.
# for location
# FS The file system to which the backup will be stored
# GIT A git controlled directory
# S3 A temporaty directory
case $TARGET_TYPE in
GIT|FS)
if [[ -z $TARGET_LOCATION ]] ; then
TARGET_LOCATION="$(pwd)"
fi
;;
S3)
export TEMPDIR=$(mktemp --directory)
if [[ -z $TARGET_LOCATION ]] ; then
echo "The TARGET_LOCATION variable, required for S3 backup is not configured"
echo "You will need to set it on the command line or in $RCUSERFILE"
exit 1
fi
;;
*)
echo "Unknown TARGET_TYPE defined $TARGET_TYPE"
echo "Should be GIT, FS or S3"
exit 1
;;
esac
function commit {
if [[ -z $TARGET_TYPE || -z $TARGET_LOCATION ]] ; then
source $RCUSERFILE
fi
case $TARGET_TYPE in
GIT)
echo "Commiting to git with message:"
echo "$* Backup from $(date +%Y-%m-%d_%T)"
cd $TARGET_LOCATION
git add *
git commit * --message="$* Backup from $(date +%Y-%m-%d_%T)"
cd -
;;
S3)
echo "loading to $TARGET_LOCATION/$1"
$AWSCMD s3 sync --quiet --no-progress --size-only --sse --storage-class INTELLIGENT_TIERING $TEMPDIR $TARGET_LOCATION/$1
rm -rf $TEMPDIR
;;
FS)
echo "Saved to $TARGET_LOCATION/$1"
;;
*)
echo "Error in commit function"
echo "TARGET_TYPE $TARGET_TYPE not identified, check configuration"
exit 1
;;
esac
}
export -f commit
function mksubdir {
if [[ -z $TARGET_TYPE || -z $TARGET_LOCATION || -z $TEMPDIR ]] ; then
source $RCUSERFILE
fi
case $TARGET_TYPE in
GIT|FS)
local SUBDIR="${TARGET_LOCATION}/$1"
;;
S3)
local SUBDIR="${TEMPDIR}/$1"
;;
*)
echo "Error in mksubdir function"
echo "TARGET_TYPE $TARGET_TYPE not identified, check configuration"
exit 1
;;
esac
if [[ ! -d ${SUBDIR} ]]; then
mkdir -p ${SUBDIR}
fi
echo $SUBDIR
}
export -f mksubdir
function backupIfNotNull {
local feature=$1
local sub_feature=$2
local target_file_suffix=$3
case $feature in
lambda)
if [[ "$sub_feature" == "list-tags" ]] ; then
feature_prefix="--resource"
else
feature_prefix="--function-name"
fi
;;
iam)
feature_prefix="--user-name"
;;
*)
echo "No feature identified, exiting.."
exit 1
;;
esac
local result=$($AWSCMD $FEATURE $sub_feature $feature_prefix $OBJECT_NAME | awk {'print $2'})
if [[ -n "${result}" ]] ; then
if [[ -n "${target_file_suffix}" ]] ; then
$AWSGET $FEATURE $sub_feature $feature_prefix $OBJECT_NAME > ${TARGET_FILE}-$target_file_suffix.json
else
echo "true"
fi
fi
}
export -f backupIfNotNull
function buildTargetFilePrefix {
local Prefix=$1
local Path=$2
if [[ -n "${Path}"} ]] ; then
if [[ ! -d ${SUBDIR}/${Path} ]] ; then
mkdir ${SUBDIR}/${Path}
fi
local TARGET_FILE="${SUBDIR}/${Path}/${Prefix}"
else
local TARGET_FILE="${SUBDIR}/${Prefix}"
fi
echo $TARGET_FILE
}
export -f buildTargetFilePrefix