-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupbitbucket.sh
195 lines (175 loc) · 7.01 KB
/
backupbitbucket.sh
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
190
191
192
193
194
195
#!/bin/bash
#######################################################################################
# Script to get all repos on bitbucket and clone / pull the master branch #############
########################################################################################
# Script Defaults
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
logfile=/archive_workspace/logs/backupbitbucket.log
REPO="ssh://[email protected]/${bitbucketUser}/"
BACKUPDIR="${BASEDIR}/BACKUP"
Application_User=$( whoami )
HOST=$( hostname )
APPLICATION="$( basename "$0" )"
bitbucketUser="DEFAULT"
bitbucketapiKey="DEFAULT"
project_name="DEFAULT"
####################################################################################
#
# Log File setup
#
####################################################################################
runInitializeLogfile()
{
if [ -f ${logfile} ]
then
runLogPrint "Application log file exist" "INFO"
else
mkdir -p /archive_workspace/logs
touch ${logfile}
runLogPrint "Creating Deploy Log, since it does not exist" "INFO"
if [ $? == 0 ]; then
runLogPrint "Successfully created deploy log file, ${logfile}" "INFO"
else
runLogPrint "Error creating the deploy log file, check the directory ${logfile} exist and ${Application_User} is got right permission and ownership" "ERROR"
fi
fi
}
####################################################################################
#
# Verbose Logger
#
####################################################################################
runLogPrint()
# logging function for ERROR, WARN, INFO, and DEBUG
{
# Throws Log Statement if not classified as ERROR Level.
if [[ $2 == "" ]]
then
loglevel="ERROR"
else
loglevel=$2
fi
echo "$( date "+%m/%d/%y %H:%M:%S.%3N" ) ${loglevel} ${HOST} ${Application_User} ${1}"
echo "$( date "+%m/%d/%y %H:%M:%S.%3N" ) LEVEL=${loglevel} HOST=${HOST} APPLICATION-USER=${Application_User} APPLICATION=${APPLICATION} MESSAGE=${1} " >> ${logfile}
}
####################################################################################
#
# Validate and set script arguments
#
####################################################################################
runValidateSetArgs()
{
################################# Determine the Arguments ########################################
runLogPrint "Base Directory: ${BASEDIR}" "INFO"
runLogPrint "Backup Directory: ${BACKUPDIR}" "INFO"
runLogPrint "logfile: ${logfile}" "INFO"
runLogPrint "BitBucket Owner: ${REPO}" "INFO"
}
####################################################################################
#
# Exit program - display error messages if needed
#
####################################################################################
runExit()
{
runLogPrint "$( date "+%m/%d/%y %H:%M:%S.%3N" )" "INFO"
if [[ ${GRC} -gt 0 ]]
then
runLogPrint "#######################################################################" "ERROR"
runLogPrint " !!! Errors Detected During Deploy !!!" "ERROR"
runLogPrint " Please review the output of this script for error messages." "ERROR"
runLogPrint "Re-run this script for the apps that failed. This can be done with the -a flag." "ERROR"
runLogPrint "#######################################################################" "ERROR"
exit ${GRC}
else
runLogPrint "Command Completed Successfully..." "INFO"
exit 0
fi
}
####################################################################################
#
# Main driver for script
#
####################################################################################
runMain()
{
runLogPrint "Removing ${BASEDIR}/repodata ${BASEDIR}/repo ${BASEDIR}/repotemp" "DEBUG"
rm -f ${BASEDIR}/repodata
rm -f ${BASEDIR}/repo
rm -f ${BASEDIR}/repotemp
####### Get the List of Repos from Bitbucket############################################
runLogPrint "Creating ${BASEDIR}/repodata from api.bitbucket.org" "INFO"
curl --silent --request GET -H "Authorization: Basic ${bitbucketapiKey}" "https://api.bitbucket.org/2.0/repositories/${bitbucketUser}?pagelen=100" >> ${BASEDIR}/repodata
totalsize=$( cat ${BASEDIR}/repodata | jq '.size' )
runLogPrint "Total Size of repodata: $totalsize" "INFO"
pages=$(( totalsize / 100))
remainder=$(( totalsize % 100))
if [ ${remainder} > 0 ]; then
pages=$((pages + 1))
fi
runLogPrint "Total Pages of repodata: ${pages}" "INFO"
if [ pages > 2 ]; then
curl --silent --request GET -H "Authorization: Basic ${bitbucketapiKey}" "https://api.bitbucket.org/2.0/repositories/${bitbucketUser}?pagelen=100&page=2" >> ${BASEDIR}/repodata
else
curl --silent --request GET -H "Authorization: Basic ${bitbucketapiKey}" "https://api.bitbucket.org/2.0/repositories/${bitbucketUser}?pagelen=100&page=[2-$pages]" >> ${BASEDIR}/repodata
fi
allreponames=$( cat ${BASEDIR}/repodata | jq -r '. ["values"] [].name' | sed 's/\\n/\n/g; s/\\t/\t/g' )
echo ${allreponames} >> ${BASEDIR}/repotemp
runLogPrint "Repo Full List" "DEBUG"
runLogPrint "cat ${BASEDIR}/repo" "DEBUG"
cat ${BASEDIR}/repotemp | tr ' ' '\n' >> ${BASEDIR}/repo
if [ $? -eq 0 ];
then
runLogPrint "Extracted Repo Information succesfully" "INFO"
else
runLogPrint "Unable to extract the Repo's" "FATAL"
exit 100
fi
runLogPrint "$( cat ${BASEDIR}/repo )" "DEBUG"
#################################################################################################
############################ Clone or Pull from the Repo's as needed ############################
if [ ! -d ${BACKUPDIR} ]; then
cd ${BASEDIR}
mkdir BACKUP
if [ $? == 0 ]; then
runLogPrint "Succesfully created the back up directory" "INFO"
else
runLogPrint "Unable to create the back up directory" "FATAL"
exit 100
fi
else
runLogPrint "Backup Directory Already exist" "INFO"
fi
for i in $(cat ${BASEDIR}/repo); do
if [ ! -d "${BACKUPDIR}/${i}" ]; then
runLogPrint "cloning ${REPO}${i}" "INFO"
cd ${BACKUPDIR}
git clone ${REPO}${i}
if [ $? == 0 ]; then
runLogPrint "Successfully cloned the repo ${i}" "INFO"
else
runLogPrint "Unable to Clone the Repo ${i}" "ERROR"
fi
else
runLogPrint "Backup Exist: Pulling ${REPO}${i}" "INFO"
cd ${BACKUPDIR}/${i}
git pull
if [ $? == 0 ]; then
runLogPrint "Successfully pulled the latest from Remote for repo ${i}" "INFO"
else
runLogPrint "Unable to Pull the repo ${i}" "ERROR"
fi
fi
runLogPrint "syncing to ${BACKUPDIR}/${i} to s3://s3backups-${project_name}/bitbucketbackup/${i}" "INFO"
aws s3 sync ${BACKUPDIR}/${i} s3://s3backups-${project_name}/bitbucketbackup/${i}
done
}
####################################################################################
# EXECUTE PROGRAM
####################################################################################
runInitializeLogfile
runLogPrint "$( date "+%m/%d/%y %H:%M:%S.%3N" )" "INFO"
runLogPrint "$( basename "$0" )" "INFO"
runValidateSetArgs
runMain
runExit