-
Notifications
You must be signed in to change notification settings - Fork 4
/
restore.sh
189 lines (170 loc) · 6.78 KB
/
restore.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
#!/usr/bin/env bash
######################################################################################
# Title: Cloudbox Restore Service: Restore Script #
# Author(s): l3uddz, desimaniac #
# URL: https://github.com/Cloudbox/Cloudbox #
# Description: Restores encrypted config files from Cloudbox Restore Service. #
# -- #
# Part of the Cloudbox project: https://cloudbox.works #
######################################################################################
# GNU General Public License v3.0 #
######################################################################################
# Usage: #
# ====== #
# Simple: #
# curl -s https://cloudbox.works/scripts/restore.sh | bash -s 'USER' 'PASS' #
# wget -qO- https://cloudbox.works/scripts/restore.sh | bash -s 'USER' 'PASS' #
# #
# Custom Cloudbox Path: #
# curl -s https://cloudbox.works/scripts/restore.sh | bash -s 'USER' 'PASS' 'PATH' #
# wget -qO- https://cloudbox.works/scripts/restore.sh | bash -s 'USER' 'PASS' 'PATH' #
######################################################################################
# vars
files=( "ansible_vault" "ansible.cfg" "accounts.yml" "settings.yml" "adv_settings.yml" "backup_config.yml" "rclone.conf")
restore="restore.cloudbox.works"
folder="$HOME/.restore_service_tmp"
green="\e[1;32m"
red="\e[1;31m"
nc="\e[0m"
done="[ ${green}DONE${nc} ]"
fail="[ ${red}FAIL${nc} ]"
ignore="[ ${red}IGNORE${nc} ]"
# Print banner
echo -e "
$green┌─────────────────────────────────────────────────────────────────────┐
$green│ Title: Cloudbox Restore Service: Restore Script │
$green│ Author(s): l3uddz, desimaniac │
$green│ URL: https://github.com/cloudbox/cloudbox │
$green│ Description: Restores encrypted config files from the │
$green│ Cloudbox Restore Service. │
$green├─────────────────────────────────────────────────────────────────────┤
$green│ Part of the Cloudbox project: https://cloudbox.works │
$green├─────────────────────────────────────────────────────────────────────┤
$green│ GNU General Public License v3.0 │
$green└─────────────────────────────────────────────────────────────────────┘
$nc"
## Functions
# validate url
# https://gist.github.com/hrwgc/7455343
function validate_url(){
if [[ `wget -S --spider $1 2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then
return 0
else
return 1
fi
}
## Main
# inputs
USER=$1
PASS=$2
DIR=${3:-$HOME/cloudbox}
# validate inputs
if [ -z "$USER" ] || [ -z "$PASS" ]
then
echo "You must provide the USER & PASS as arguments"
exit 1
fi
# validate folders exist
TMP_FOLDER_RESULT=$(mkdir -p $folder)
if [ ! -z "$TMP_FOLDER_RESULT" ]
then
echo "Failed to ensure $folder was created..."
exit 1
else
rm -rf $folder/*
fi
RESTORE_FOLDER_RESULT=$(mkdir -p $DIR)
if [ ! -z "$RESTORE_FOLDER_RESULT" ]
then
echo "Failed to ensure $DIR was created..."
exit 1
fi
# SHA1 username
USER_HASH=$(echo -n "$USER" | openssl dgst -sha1 | sed 's/^.*= //')
echo "User Hash: $USER_HASH"
echo ''
# Fetch files
echo "Fetching files from $restore..."
echo ''
for file in "${files[@]}"
do
:
# wget file
if [ "$file" == "ansible_vault" ]; then
printf '%-20.20s' ."$file"
else
printf '%-20.20s' "$file"
fi
URL=http://$restore/load/$USER_HASH/$file
if validate_url $URL; then
wget -qO $folder/$file.enc $URL
# is the file encrypted?
file_header=$(head -c 10 $folder/$file.enc | tr -d '\0')
if [[ $file_header == Salted* ]]; then
echo -e $done
else
echo -e $fail
exit 1
fi
else
echo -e $ignore
fi
done
echo ''
# Decrypt files
echo 'Decrypting fetched files...'
echo ''
for file in $folder/*
do
:
filename="$(basename -- $file .enc)"
# wget file
if [ "$filename" == "ansible_vault" ]; then
printf '%-20.20s' ."$filename"
else
printf '%-20.20s' "$filename"
fi
DECRYPT_RESULT=$(openssl enc -aes-256-cbc -d -salt -md md5 -in $folder/${filename}.enc -out $folder/$filename -k "$PASS" >/dev/null 2>&1)
# was the file decryption successful?
if [ -z "$DECRYPT_RESULT" ]; then
echo -e $done
rm $folder/${filename}.enc
elif [ "$filename" == "ansible_vault" ]; then
echo -e $ignore
else
echo -e $fail
exit 1
fi
done
echo ''
# Move decrypted files
echo 'Moving decrypted files...'
echo ''
for file in $folder/*
do
:
# move file
filename="$(basename -- $file)"
if [ "$filename" == "ansible_vault" ]; then
printf '%-20.20s' ."$filename"
if [ -f "$folder/$filename" ]; then
mv $folder/$filename $HOME/.$filename 2>&1
echo -e $done
else
echo -e $ignore
fi
else
printf '%-20.20s' "$filename"
MOVE_RESULT=$(mv $folder/$filename $DIR/$filename 2>&1)
# was the decrypted file moved successfully?
if [ -z "$MOVE_RESULT" ]; then
echo -e $done
else
echo -e $fail
exit 1
fi
fi
done
echo ''
# finish
exit 0