-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore_database.sh
84 lines (72 loc) · 1.93 KB
/
restore_database.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
#!/bin/bash
#
# restore.sh 1.0.0
#
# <Description>
#
# (c)2020-2022 Simon Gaus
#
# Check if url to backup is provided
#
if [ $# -ne 1 ]; then
echo "$0: usage: sudo ./restore.sh <url_to_zipped_backup>"
exit 1
fi
# Save backu url to variable
#
backup_url=$1
# Check if the backup user exists
#
database_user_exists="$(mysql -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'festivals.api.backup');")"
if [ "$database_user_exists" != 1 ]; then
echo "This restore script only works if the database was properly setup via the install script. The backup user appears to be missing."
sleep 1
exit 1
fi
# Check if the api read user exists
#
reader_user_exists="$(mysql -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'festivals.api.reader');")"
if [ "$reader_user_exists" != 1 ]; then
echo "This restore script only works if the database was properly setup via the install script. The api read user appears to be missing."
sleep 1
exit 1
fi
# Check if the api write user exists
#
writer_user_exists="$(mysql -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'festivals.api.writer');")"
if [ "$writer_user_exists" != 1 ]; then
echo "This restore script only works if the database was properly setup via the install script. The api write user appears to be missing."
sleep 1
exit 1
fi
# Move to the project folder
#
cd /usr/local/festivals-database || exit
# Donwloading the backup file
#
echo "Downloading the backup..."
curl --progress-bar -L -o backup_zip.gz "$backup_url"
# Decompressing the backup file
#
echo "Decompressing the backup..."
gunzip -c backup_zip.gz > backup.sql
# Restoring the database
#
echo "Restoring the backup..."
mysql festivals_api_database < backup.sql
sleep 1
# Cleanup
#
echo "Checking all databases - this can take a while ..."
echo
sleep 1
mysqlcheck --auto-repair --all-databases
sleep 1
# Cleanup
#
echo "Cleanup"
sleep 1
rm backup_zip.gz
rm backup.sql
echo "Done!"
sleep 1