-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDropStore.sh
executable file
·334 lines (293 loc) · 12.2 KB
/
DropStore.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/bash
#################################################################################################
# File : DropStore Installation and Configuration Script
# Author : Reda Maher ([email protected])
# Details: This script installs the DropStore on any Linux based OS (Ubuntu/Raspberry Pi OS)
# The script does the following tasks:
# 1- Install the system prerequisites
# 2- Configure the Edge devices accounts
# 3- Configure DropStore Cloud parameters/accounts/encryption key
# 4- Restore old backup (if needed)
# 5- Setup the periodic DropStore Cloud Backup job
#################################################################################################
HORIZONTAL_LINE="========================================================================="
DUPLICITY_GIT_REPO="https://github.com/RedaMaher/DropStore_duplicity"
DROPSTORE_DUPLICITY_BRANCH="DropStore_support"
DROPSTORE_DUPLICITY_TMP_DIR="/tmp/DropStore_duplicity"
SFTP_GROUP_NAME="dropstore-edge-users"
DROPSTORE_ROOT_DIR="/opt/dropstore"
clear
clear
echo -e "\n${HORIZONTAL_LINE}"
echo -e "\n Welcome to DropStore Installation"
echo -e "\n${HORIZONTAL_LINE}"
if [ $(id -u) != 0 ]; then
echo "This script must be run as root for proper operation"
exit 1
fi
echo -e "\n#########################"
echo -e "\n# Install prerequisites"
echo -e "\n#########################"
apt install -y git vim gnupg python3-distutils python3-dev librsync-dev python3-setuptools gettext python3-pip cron
exit_1=$?
python3 -m pip install future fasteners mediafire
exit_2=$?
exit_status=$((${exit_1} + ${exit_2}))
if [ ${exit_status} != 0 ]; then
echo "ERROR: Colud not install the prerequisites .. Abort!"
exit 1
fi
echo -e "\n#################################"
echo -e "\n# Configure DropStore location"
echo -e "\n#################################"
# Prompt the user for dropstore location
while true; do
read -p "Default DropStore Location is ${DROPSTORE_ROOT_DIR}, Do you like to change it [y/n]? " yn
case $yn in
[Yy]* )
read -p "Please Enter the path to the new location: " DROPSTORE_ROOT_DIR; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
rm -rf ${DROPSTORE_ROOT_DIR}
mkdir -p ${DROPSTORE_ROOT_DIR}
EDGE_USERS_BACKUP_DIR="${DROPSTORE_ROOT_DIR}/edge-users"
DROPSTORE_CONFIG_DIR="${DROPSTORE_ROOT_DIR}/config"
echo -e "\n#################################################"
echo -e "\n# Install the DropStore version of Duplicity"
echo -e "\n#################################################"
pushd /tmp > /dev/null
rm -rf ${DROPSTORE_DUPLICITY_TMP_DIR}
mkdir -p ${DROPSTORE_DUPLICITY_TMP_DIR} && cd ${DROPSTORE_DUPLICITY_TMP_DIR}
git clone ${DUPLICITY_GIT_REPO} ${DROPSTORE_DUPLICITY_TMP_DIR}
if [ $? != 0 ]; then
echo "ERROR: Colud not clone the duplicity source code .. Abort!"
exit 1
fi
git checkout ${DROPSTORE_DUPLICITY_BRANCH}
python3 setup.py build
if [ $? != 0 ]; then
echo "ERROR: Failed to build Duplicity .. Abort!"
exit 1
fi
python3 setup.py install
if [ $? != 0 ]; then
echo "ERROR: Failed to install Duplicity .. Abort!"
exit 1
fi
# Check that duplicitiy was installed correctly
duplicity -h > /dev/null
if [ $? != 0 ]; then
echo "ERROR: Duplicity was not installed correctly .. Abort!"
exit 1
fi
rm -rf ${DROPSTORE_DUPLICITY_TMP_DIR}
popd > /dev/null
echo -e "\n########################################"
echo -e "\n# Configure the Edge devices accounts"
echo -e "\n########################################"
while true; do
read -p "Enter the count of the Edge Devices: " EDGE_DEVICES_COUNT
read -p "The Edge devices count is ${EDGE_DEVICES_COUNT}, please confirm [y/n]? " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) continue;;
* ) echo "Please answer yes or no.";;
esac
done
EDGE_DEVICES_USER_NAMES=()
EDGE_DEVICES_PASSWORD=()
# Read User name and passwords for the edge devices
count=0
while [ ${count} -lt ${EDGE_DEVICES_COUNT} ]
do
read -p "Enter the user name for Edge Device ${count}: " EDGE_DEVICES_USER_NAMES[${count}]
read -sp "Enter the password for Edge Device ${count}: " EDGE_DEVICES_PASSWORD[${count}]
echo ""
count=`expr $count + 1`
done
# Configure the edge devices account
# Create SFTP group
groupadd ${SFTP_GROUP_NAME} > /dev/null 2>&1
# Create edge users accounts
count=0
for edgeuser in "${EDGE_DEVICES_USER_NAMES[@]}"
do
userdel ${edgeuser} > /dev/null 2>&1
useradd -g ${SFTP_GROUP_NAME} -d /backup -s /sbin/nologin ${edgeuser}
echo ${edgeuser}:${EDGE_DEVICES_PASSWORD[${count}]} | chpasswd
mkdir -p ${EDGE_USERS_BACKUP_DIR}/${edgeuser}/backup
chown ${edgeuser}:${SFTP_GROUP_NAME} ${EDGE_USERS_BACKUP_DIR}/${edgeuser}/backup
count=`expr $count + 1`
done
# Configure the SFTP Jail
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# Remove old configs
sed -ri "s/^Subsystem(.*?)sftp(.*)/\#Subsystem\1sftp\2/" /etc/ssh/sshd_config
sed -ri "s/^Match Group(.*?)/\#Match Group\1/" /etc/ssh/sshd_config
sed -ri "s/^(\s*)ChrootDirectory(.*?)/\#\1ChrootDirectory\2/" /etc/ssh/sshd_config
sed -ri "s/^(\s*)X11Forwarding(.*?)/\#\X11Forwarding\2/" /etc/ssh/sshd_config
sed -ri "s/^(\s*)AllowTcpForwarding(.*?)/\#\AllowTcpForwarding\2/" /etc/ssh/sshd_config
sed -ri "s/^(\s*)PermitTunnel(.*?)/\#\PermitTunnel\2/" /etc/ssh/sshd_config
sed -ri "s/^(\s*)AllowAgentForwarding(.*?)/\#\AllowAgentForwarding\2/" /etc/ssh/sshd_config
sed -ri "s/^(\s*)ForceCommand(.*?)/\#\ForceCommand\2/" /etc/ssh/sshd_config
sed -ri "s/^(\s*)PasswordAuthentication(.*?)/\#\PasswordAuthentication\2/" /etc/ssh/sshd_config
# Add the new configs
echo "Subsystem sftp internal-sftp" >> /etc/ssh/sshd_config
echo "Match Group ${SFTP_GROUP_NAME}
ChrootDirectory ${EDGE_USERS_BACKUP_DIR}/%u
X11Forwarding no
AllowTcpForwarding no
PermitTunnel no
AllowAgentForwarding no
ForceCommand internal-sftp
PasswordAuthentication yes" >> /etc/ssh/sshd_config
service sshd restart > /dev/null
if [ $? != 0 ]; then
echo "ERROR: Failed to configure SFTP .. Abort!"
exit 1
fi
echo -e "\n########################################"
echo -e "\n# Configure DropStore Cloud Parameters"
echo -e "\n########################################"
while true; do
read -p "Enter the count of Cloud Servers: " CLOUD_SERVERS_COUNT
read -p "Enter the Replica Count : " DATA_REPLICA_COUNT
read -p "Enter the data Chunk Size : " DATA_CHUNCK_SIZE
echo "DropStore Cloud Configurations:"
echo " Number of Cloud Servers: ${CLOUD_SERVERS_COUNT}"
echo " Replica Count : ${DATA_REPLICA_COUNT}"
echo " Data Chunk Size : ${DATA_CHUNCK_SIZE}"
read -p "Please confirm [y/n]? " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -e "\n#########################################"
echo -e "\n# Configure the Cloud Servers accounts"
echo -e "\n#########################################"
echo -e "\nPlease enter the cloud server URL in the following format:"
echo -e "\n scheme://[user[:password]@]host[:port]/[/]path"
echo -e "\n for more information, please refer to Duplicity manual page\n"
CLOUD_SERVERS_URLS=()
count=0
while [ ${count} -lt ${CLOUD_SERVERS_COUNT} ]
do
read -p "Cloud Server ${count}: " CLOUD_SERVERS_URLS[${count}]
count=`expr $count + 1`
done
# Create the Multi-Cloud configuration file
mkdir -p ${DROPSTORE_CONFIG_DIR}
touch ${DROPSTORE_CONFIG_DIR}/config.json
echo "[" > ${DROPSTORE_CONFIG_DIR}/config.json
count=0
for csp_url in "${CLOUD_SERVERS_URLS[@]}"
do
count=`expr $count + 1`
if [ ${count} -lt ${CLOUD_SERVERS_COUNT} ]; then
echo " {
\"url\":\"${csp_url}\"
}," >> ${DROPSTORE_CONFIG_DIR}/config.json
else
echo " {
\"url\":\"${csp_url}\"
}" >> ${DROPSTORE_CONFIG_DIR}/config.json
fi
done
echo "]" >> ${DROPSTORE_CONFIG_DIR}/config.json
echo -e "\n###########################################"
echo -e "\n# Configure the DropStore Encryption Key"
echo -e "\n###########################################"
while true; do
echo "1) Generate a New Key."
echo "2) Import an existing Key."
echo "3) List the available Public Keys."
echo "4) List the available Private Keys"
echo "5) Configure DropStore Key ID and Continue."
read -p "Type your selected option number: " choice
if [ "${choice}" -eq "${choice}" 2> /dev/null ]; then
if [ ${choice} -lt 1 -o ${choice} -gt 5 ]; then
echo -e "\n\n==> Enter number between 1 and 5 <==\n\n"
elif [ ${choice} -eq 1 ]; then
echo "Generating a New Key .."
gpg --gen-key
elif [ ${choice} -eq 2 ]; then
read -p "Enter the path to the Key: " KEY_PATH
gpg --import ${KEY_PATH}/DropStore.key && gpg --import-ownertrust ${KEY_PATH}/DropStore.trust
if [ $? != 0 ]; then
echo -e "\n\nERROR: Failed to import the Key .. Make sure the path is correct!\n\n"
fi
elif [ ${choice} -eq 3 ]; then
echo "List Public Keys .."
gpg --list-public-keys
elif [ ${choice} -eq 4 ]; then
echo "List Private Keys .."
gpg --list-secret-keys
elif [ ${choice} -eq 5 ]; then
read -p "Enter the DropStore Key ID: " DROPSTORE_ENC_KEY_ID
break;
fi
else
echo -e "\n\n==> This is not a number <==\n\n"
fi
done
# Export the key to the configuration folder
gpg --export-secret-keys ${DROPSTORE_ENC_KEY_ID} > /tmp/key
gpg --export-ownertrust > /tmp/trust
mv /tmp/key ${DROPSTORE_CONFIG_DIR}/DropStore.key
mv /tmp/trust ${DROPSTORE_CONFIG_DIR}/DropStore.trust
# Configure Periodic DropStore Cloud backup task
crontab -r > /dev/null 2>&1
echo "0 1 * * * duplicity --progress --allow-source-mismatch --volsize ${DATA_CHUNCK_SIZE} --encrypt-key ${DROPSTORE_ENC_KEY_ID} ${EDGE_USERS_BACKUP_DIR} \"multi://${DROPSTORE_CONFIG_DIR}/config.json?mode=redundent&onfail=abort&count=${DATA_REPLICA_COUNT}\"" > /tmp/dropstore_cron
crontab /tmp/dropstore_cron
rm -f /tmp/dropstore_cron
# Write the configuration of the DropStore to the configuration folder
echo "====================================================
DropStore Retrieval Information
====================================================
CLOUD_SERVERS_COUNT : ${CLOUD_SERVERS_COUNT}
DATA_REPLICA_COUNT : ${DATA_REPLICA_COUNT}
DATA_CHUNCK_SIZE : ${DATA_CHUNCK_SIZE}
DROPSTORE_ENC_KEY_ID : ${DROPSTORE_ENC_KEY_ID}
====================================================
DropStore Edge Users
---------------------" > ${DROPSTORE_CONFIG_DIR}/DropStore_Retrieval.info
count=0
for edgeuser in "${EDGE_DEVICES_USER_NAMES[@]}"
do
echo "USER ${count}: ${edgeuser}, ${EDGE_DEVICES_PASSWORD[${count}]}" >> ${DROPSTORE_CONFIG_DIR}/DropStore_Retrieval.info
count=`expr $count + 1`
done
echo "====================================================
DropStore Cloud Accounts
------------------------" >> ${DROPSTORE_CONFIG_DIR}/DropStore_Retrieval.info
count=0
for csp_url in "${CLOUD_SERVERS_URLS[@]}"
do
echo "ACCOUNT ${count}: ${csp_url}" >> ${DROPSTORE_CONFIG_DIR}/DropStore_Retrieval.info
count=`expr $count + 1`
done
# Retore Old backup (if any)
while true; do
read -p "Do you like to restore an old backup from the cloud [y/n]? " yn
case $yn in
[Yy]* )
echo "Restoring the old backup .."
rm -rf ${EDGE_USERS_BACKUP_DIR} # To restore it from the cloud
duplicity --progress --volsize ${DATA_CHUNCK_SIZE} --encrypt-key ${DROPSTORE_ENC_KEY_ID} "multi://${DROPSTORE_CONFIG_DIR}/config.json?mode=redundent&onfail=abort&count=${DATA_REPLICA_COUNT}" ${EDGE_USERS_BACKUP_DIR}
break;;
[Nn]* )
break;;
* ) echo "Please answer yes or no.";;
esac
done
# Finalize
echo -e "\n######################################################################################################################"
echo -e "\n# DropStore Setup was Successfully Done !\n"
echo -e "# CAUTION: DropStore Retrieval Information was saved in:"
echo -e "# ${DROPSTORE_CONFIG_DIR}"
echo -e "# Please save it in a secure external storage for disaster recovery."
echo -e "\n#######################################################################################################################"