forked from netcore-jsa/unimus-backup-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunimus-backup-exporter.sh
executable file
·305 lines (263 loc) · 7.97 KB
/
unimus-backup-exporter.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env bash
# This is a Unimus to Git API to export your backups to your Git Repo
# $1 is echo message
function echoGreen(){
printf "$(date +'%F %H:%M:%S') $1\n" >> $log
local green='\033[0;32m'
local reset='\033[0m'
echo -e "${green}$1${reset}"
}
# $1 is echo message
function echoYellow(){
printf "WARNING: $(date +'%F %H:%M:%S') $1\n" >> $log
local yellow='\033[1;33m'
local reset='\033[0m'
echo -e "WARNING: ${yellow}$1${reset}"
}
# $1 is echo message
function echoRed(){
printf "ERROR: $(date +'%F %H:%M:%S') $1\n" >> $log
local red='\033[0;31m'
local reset='\033[0m'
echo -e "ERROR: ${red}$1${reset}"
}
# $1 is $? from the command being checked
# #2 is the error message
function errorCheck(){
if [ $1 -ne 0 ]; then
echoRed "$2"
exit "$1"
fi
}
# This function will do a get request
# $1 is the api request
function unimusGet(){
local get_request=$(curl -s -H 'Accept: application/json' -H "Authorization: Bearer $unimus_api_key" "$unimus_server_address/api/v2/$1")
errorCheck "$?" 'Unable to get data from unimus server'
echo "$get_request"
}
# Verify's Server is online
function unimusStatusCheck(){
local get_status=$(unimusGet 'health')
local status=$(jq -r '.data.status' <<< $get_status)
errorCheck "$?" 'Unable to peform unimus Status Check'
echo "$status"
}
# $1 is the device id
# $2 is the date of the backup
# $3 is the base64 encoded backup
# $4 is the backup type
# Decodes and Saves Backup
function saveBackup(){
local address=${devices[$1]}
if [ $4 == 'TEXT' ]; then
local type='txt'
elif [ $4 == 'BINARY' ]; then
local type ='bin'
fi
base64 -d <<< $3 > "$backup_dir/$address - $1.$type"
}
function getAllDevices(){
echoGreen 'Getting Device Information'
for ((page=0; ; page+=1)); do
local contents=$(unimusGet "devices?page=$page")
errorCheck "$?" 'Unable to get device data from unimus'
for((data=0; ; data+=1)); do
if ( jq -e ".data[$data] | length == 0" <<< $contents) >/dev/null; then
break
fi
local id=$(jq -e -r ".data[$data].id" <<< $contents)
local address=$(jq -e -r ".data[$data].address" <<< $contents)
devices[$id]=$address
done
if ( jq -e '.data | length == 0' <<< $contents ) >/dev/null; then
break
fi
done
}
function getAllBackups(){
local backupCount=0
for key in "${!devices[@]}"; do
for ((page=0; ; page+=1)); do
local contents=$(unimusGet "devices/$key/backups?page=$page")
errorCheck "$?" 'Unable to get all backups from unimus'
for ((data=0; ; data+=1)); do
if ( jq -e ".data[$data] | length == 0" <<< $contents) >/dev/null; then
break
fi
local deviceId=$key
local date="$(jq -e -r ".data[$data].validSince" <<< $contents | { read tme ; date "+%F-%T-%Z" -d "@$tme" ; })"
local backup=$(jq -e -r ".data[$data].bytes" <<< $contents)
local type=$(jq -e -r ".data[$data].type" <<< $contents)
saveBackup "$deviceId" "$date" "$backup" "$type"
let backupCount++
done
if [ $(jq -e '.data | length == 0' <<< $contents) ] >/dev/null; then
break
fi
done
done
echoGreen "$backupCount backups exported"
}
# Will Pull down backups and save to Disk
function getLatestBackups(){
local backupCount
# Query for latest backups. This will loop through getting every page
for ((page=0; ; pagae+=1)); do
local contents=$(unimusGet "devices/backups/latest?page=$page")
errorCheck "$?" 'Unable to get latest backups from unimus'
for ((data=0; ; data+=1)); do
# Breaks if looped through all devices
if ( jq -e ".data[$data] | length == 0" <<< $contents) >/dev/null; then
break
fi
local deviceId=$(jq -e -r ".data[$data].deviceId" <<< $contents)
local date=$(jq -e -r ".data[$data].backup.validSince" <<< $contents | { read tme ; date "+%F-%T-%Z" -d "@$tme" ; })
local backup=$(jq -e -r ".data[$data].backup.bytes" <<< $contents)
local type=$(jq -e -r ".data[$data].backup.type" <<< $contents)
saveBackup "$deviceId" "$date" "$backup" "$type"
let backupCount++
done
# Breaks if empty page.
if [ $(jq -e '.data | length == 0' <<< $contents) ] >/dev/null; then
break
fi
done
echoGreen "$backupCount backups exported"
}
function pushToGit(){
cd $backup_dir
errorCheck "$?" 'Failed to enter backup directory'
if ! [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
git init
git add .
git commit -m 'Initial Commit'
case $git_server_protocal in
ssh)
ssh-keyscan -H git_server_address >> ~/.ssh/known_hosts
if [ -z "$git_password" ]; then
git remote add origin ssh://$git_username@$git_server_address:$git_port/$git_repo_name
errorCheck "$?" 'Failed to add git repo'
else
git remote add origin ssh://$git_username:$git_password@$git_server_address:$git_port/$git_repo_name
errorCheck "$?" 'Failed to add git repo'
fi
;;
http)
git remote add origin http://$git_username:$git_password@$git_server_address:$git_port/$git_repo_name
errorCheck "$?" 'Failed to add git repo'
;;
https)
git remote add origin https://$git_username:$git_password@$git_server_address:$git_port/$git_repo_name
errorCheck "$?" 'Failed to add git repo'
;;
*)
echoRed 'Invalid setting for git_server_protocal'
exit 2
;;
esac
git push -u origin $git_branch >> $log
errorCheck "$?" 'Failed to add branch'
git push >> $log
errorCheck "$?" 'Failed to push to git'
else
git pull
errorCheck "$?" 'Failed to pull from backups git repo'
git add --all
git commit -m "Unimus Git Extractor $(date +'%b-%d-%y %H:%M')"
git push
errorCheck "$?" 'Failed to push to backups git repo'
fi
cd $script_dir
}
# We can't pass the variable name in any other way
# $1 is the variable
# $2 is the name
function checkVars(){
if [ -z "$1" ]; then
echoRed "$2 is not set in unimus-backup-exporter.env"
exit 2
fi
}
function importVariables(){
set -a # Automatically export all variables
source unimus-backup-exporter.env
set +a
checkVars "$unimus_server_address" 'unimus_server_address'
checkVars "$unimus_api_key" 'unimus_api_key'
checkVars "$backup_type" 'backup_type'
checkVars "$export_type" 'export_type'
if [ "$export_type" == 'git' ]; then
checkVars "$git_username" 'git_username'
# Only Checking for password for http. SSH may or may not require a password
if [[ "$git_server_protocal" == 'http' || "$git_server_protocal" == 'https' ]]; then
if [ -z "$git_password" ]; then
echoRed 'Please Provide a git password'
exit 2
fi
fi
checkVars "$git_email" 'git_email'
checkVars "$git_server_protocal" 'git_server_protocal'
checkVars "$git_server_address" 'git_server_address'
checkVars "$git_port" 'git_port'
checkVars "$git_repo_name" 'git_repo_name'
checkVars "$git_branch" 'git_branch'
fi
}
function main(){
SCRIPT_VERSION='1.1.0'
# Set script directory and working dir for script
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
cd "$script_dir"
backup_dir=$script_dir/backups
# HashTable for all devices
declare -A devices
# Create Backup Folder
if ! [ -d 'backups' ]; then
mkdir backups
errorCheck "$?" 'Failed to create backup folder'
fi
# Creating a log file
log="$script_dir/unimus-backup-exporter.log"
printf 'Log File - ' >> $log
date +"%F %H:%M:%S" >> $log
git pull >> $log
errorCheck "$?" 'Failed to pull latest code'
# Importing variables
importVariables
status=$(unimusStatusCheck)
errorCheck "$?" 'Status check failed'
if [ $status == 'OK' ]; then
# Getting All Device Information
echoGreen 'Getting device data'
getAllDevices
# Chooses what type of backup we will do
case $backup_type in
latest)
echoGreen 'Exporting latest backups'
getLatestBackups
echoGreen 'Export successful'
;;
all)
echoGreen 'Exporting all backups'
getAllBackups
echoGreen 'Export successful'
;;
esac
# Exporting to git
if [ $export_type == 'git' ]; then
echoGreen 'Pushing to git'
pushToGit
echoGreen 'Push successful'
fi
else
if [ -z $status ]; then
echoRed 'Unable to connect to unimus server'
exit 2
else
echoRed "Unimus server status: $status"
fi
fi
echoGreen 'Script finished'
}
main