-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_restore
More file actions
executable file
·301 lines (245 loc) · 9.42 KB
/
Copy pathbackup_restore
File metadata and controls
executable file
·301 lines (245 loc) · 9.42 KB
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
#!/bin/bash
# This script configures the developers group depending on the type of server
# It then connects as each users, so that homes are created
# Finally, it sets the developers group as primary group for each user
#
# Version 1.0
# tbartolone 30/06/2020
set -x
usage() {
init
PROGNAME=$0
cat <<- EOF
Missing arguments
$PROGNAME <archive full path> <original project name> <original project mode> <new project name> <new mode>
NB: do not add the -dev or -prod extension to the name of the project
<mode> : dev or prod
For example:
$PROGNAME /backups/test1-dev-20220920-1835.tgz test1 dev test1-restore dev
$PROGNAME /backups/test1-dev-20220920-1835.tgz test1 dev
EOF
clearmsg "\nList of archives in $backups_path"
for file in $(ls -1Atr $backups_path); do
readlink -m $backups_path/$file
done
exit 1
}
init(){
GREEN='\033[0;32m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
timestamp=$(date +%Y%m%H%M)
. config.ini
}
clearmsg(){
msg=$1
printf "${CYAN}${msg}${NC}\n"
}
go(){
:
}
testexec(){
:
}
generaldb_credential(){
unset login
prompt="Enter generaldb_mariadb admin username: "
read -p "$prompt" username
unset password
prompt="Enter generaldb_mariadb admin password: "
while IFS= read -p "$prompt" -r -s -n 1 char
do
if [[ $char == $'\0' ]]
then
break
fi
prompt='*'
password+="$char"
done
echo
}
# generate_new_credentials(){
# export new_username=$project$suffix-$(bw generate -p)
# export new_password=$(bw generate)
# }
input_credentials(){
echo "Enter project username for the DB"
read new_username
echo "Enter project password for the DB"
read new_password
export new_username
export new_password
}
generaldb_credentials_file(){
mode=$1
unset username
unset password
if [[ ! -f .${mode}_generaldb_creds ]]; then
echo "Credentials file cannot be found (.generaldb_creds)"
exit 1
fi
. ./.${mode}_generaldb_creds
username=${mode}_username
username=${!username}
password=${mode}_password
password=${!password}
if [[ -z $username ]] || [[ -z password ]]; then
echo "Credentials are empty (in .generaldb_creds)"
exit 1
fi
}
delete_dbs(){
project_to_delete=$1
command="DROP DATABASE \`$project_to_delete\`;"
docker exec -it $generaldb_container_name mysql -u "$username" -p$password -e"$command"
}
create_db_user(){
new_project=$1
new_username=$2
new_password=$3
command="CREATE DATABASE \`$new_project\`; SHOW DATABASES; CREATE USER \"$new_username\" IDENTIFIED BY \"$new_password\"; SELECT User FROM mysql.user; GRANT ALL PRIVILEGES ON \`$new_project\`.* TO \"$new_username\"; FLUSH PRIVILEGES; SHOW GRANTS FOR \"$new_username\";"
docker exec -it $generaldb_container_name mysql -u "$username" -p$password -e"$command"
}
create_db(){
project=$1
command="CREATE DATABASE \`$project\`;"
docker exec -it $generaldb_container_name mysql -u "$username" -p$password -e"$command"
}
main(){
init
archive=$1
original_name=$2
original_mode=$3
new_mode=${@: -1}
original_name_mode=${original_name}-${original_mode}
generaldb_container_name=${new_mode}_generaldb_container_name
generaldb_container_name=${!generaldb_container_name}
original_db_shared_path_host=${new_mode}_db_shared_path_host
original_db_shared_path_host=${!original_db_shared_path_host}
original_relative_db_shared_path_host=${new_mode}_relative_db_shared_path_host
original_relative_db_shared_path_host=${!original_relative_db_shared_path_host}
new_db_shared_path_host=${new_mode}_db_shared_path_host
new_db_shared_path_host=${!new_db_shared_path_host}
new_relative_db_shared_path_host=${new_mode}_relative_db_shared_path_host
new_relative_db_shared_path_host=${!new_relative_db_shared_path_host}
if [[ $# -eq 5 ]]; then
new_name=$4
new_name_mode=${new_name}-${new_mode}
else
new_name=''
fi
# Checks
if [[ -n $new_name ]]; then
if [[ -d $varwww_path/$new_name_mode ]]; then
if [[ -f $varwww_path/$new_name_mode/wordpress/wp-config.php ]]; then
clearmsg "There's already a project $new_name in $new_mode mode, in $varwww_path/$new_name_mode"
echo "Maybe it's just a brand new empty project you've just created for this restoration"
echo "In this case, you can just proceed by pressing a key"
read
else
clearmsg "There's already a project $new_name in $new_mode mode, in $varwww_path/$new_name_mode"
clearmsg "But $varwww_path/$new_name_mode/wordpress/wp-config.php does not exist"
echo "You need to let the container start, or recreate the project"
exit 1
fi
else
clearmsg "First please create project $new_name in $new_mode mode"
read
fi
else
if [[ -d $varwww_path/$original_name_mode ]]; then
clearmsg "There's already a project original_name_mode, in $varwww_path/$original_name_mode"
exit 1
fi
fi
archive_name=$(basename $archive | sed 's/\.tgz//')
mkdir -p $restore_path/$archive_name
tar -xzf $archive --directory $restore_path/$archive_name
# RESTORE DATA IN /var/www
# If there's new name, means we don't overwrite the existing project with the original name
if [[ -n $new_name ]]; then
# we first stop the new created project
docker compose -f $project_path/$new_name_mode/docker-compose.yml down
# Restore data
# we cannot delete and just copy the wordpress folder
# as we need to keep wordpress/wp-config.php
rm -rf $varwww_path/$new_name_mode/wordpress/wp-content
mv $restore_path/$archive_name/$relative_varwww_path/$original_name_mode/wordpress/wp-content $varwww_path/$new_name_mode/wordpress/
else
# Restore data
# In the case where we just restore the project, it is same wordpress folders in the same locations
mv $restore_path/$archive_name/$relative_varwww_path/$original_name_mode $varwww_path/$original_name_mode
mv $restore_path/$archive_name/$relative_project_path/$original_name_mode $project_path/$original_name_mode
fi
generaldb_credentials_file $new_mode
# RESTORE DATABASE
if [[ -n $new_name ]]; then
# we first need to drop the empty DB that has just been created and recreate a totally empty one
delete_dbs $new_name_mode
create_db $new_name_mode
# now we import the content of the original DB into the new one
mv $restore_path/$archive_name/$original_relative_db_shared_path_host/$original_name_mode.sql $new_db_shared_path_host
docker exec $generaldb_container_name bash -c "mysql -u $username -p$password $new_name_mode < $db_shared_path_container/$original_name_mode.sql"
clearmsg "\nProject restored. You now need to restart the container, using the commands provided to you when creating it:"
cat <<-EOF
export new_project=...; export new_username=...; export new_password=...
docker compose -f $project_path/$new_name_mode/docker-compose.yml up -d
EOF
else
new_username=$(grep DB_USER $varwww_path/$original_name_mode/wordpress/wp-config.php | awk '{print $3}' | tr -d \')
new_password=$(grep DB_PASSWORD $varwww_path/$original_name_mode/wordpress/wp-config.php | awk '{print $3}' | tr -d \')
new_project=$original_name_mode
echo $new_username $new_password $new_project
export new_username; export new_password; export new_project
create_db_user $new_project $new_username $new_password
mv $restore_path/$archive_name/$original_relative_db_shared_path_host/$original_name_mode.sql $original_db_shared_path_host
docker exec $generaldb_container_name bash -c "mysql -u $username -p$password $original_name_mode < $db_shared_path_container/$original_name_mode.sql"
docker compose -f $project_path/$original_name_mode/docker-compose.yml up -d
fi
# if [[ -n $new_name ]]; then
# clearmsg "Recreating $new_name_mode container"
# docker-compose -f $project_path/$new_name_mode/docker-compose.yml up -d
# fi
exit
clearmsg "mode $archive project $project"
generaldb_credentials_file $mode
clearmsg "$username $password"
# RESTORE CONTAINER CONFIG
# mv $restore_path/$archive_name/$relative_project_path/$project $project_path/$project$suffix
# sed -i "s/$project/$project$suffix/g" $project_path/$project$suffix/docker-compose.yml
# Restore base
echo -e "\nRestoring base"
set +x
generaldb_credential
input_credentials
create_db_user
set -x
mv $restore_path/$archive_name/$relative_db_shared_path_host/$project.sql $db_shared_path_host
# command="CREATE DATABASE \`$project$suffix\`; SHOW DATABASES; CREATE USER \"$new_username\" IDENTIFIED BY \"$new_password\"; SELECT User FROM mysql.user; GRANT ALL PRIVILEGES ON \`$project$suffix\`.* TO \"$new_username\"; FLUSH PRIVILEGES; SHOW GRANTS FOR \"$new_username\";"
docker exec $generaldb_container_name bash -c "mysql -u $username -p$password $project$suffix < $db_shared_path_container/$project.sql"
# generate_new_credentials
echo -e "\nSave this info (in Bitwarden?):"
echo "New database: $new_project"
echo "New user: $new_username"
echo "New password: $new_password"
echo "When recreating the project container, you will need to type this line before:"
echo "export new_project=$project$suffix; export new_username=$new_username; export new_password=$new_password"
echo "Then:"
echo "docker compose -f /projects/$project$suffix/docker-compose.yml up -d"
exit
if [[ $1 = "go" ]]
then
init
go
testexec
else
init
testexec
fi
}
if [[ $# -eq 0 ]]
then
usage
fi
main $@