-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-backup.sh
48 lines (37 loc) · 1.43 KB
/
db-backup.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
#!/bin/bash
routes=("cards" "choirs" "concerts" "cv" "discography" "memories" "orchestras" "pages" "pictures" "repertoire")
# colors
GREEN='\033[0;32m'
RED='\033[0;31m'
COLORRESET='\033[0m'
url=$(grep REACT_APP_API_URL .env | xargs)
url="${url##*'url='}"
date=$(date +%F)
time=$(date +%H-%M-%S)
directory=$(echo $date--$time)
backup="db-backup"
if [ ! -d $backup ]; then
mkdir $backup
fi
mkdir $backup/$directory
errorCounter=0
for ix in ${!routes[*]}
do
content=$(curl $url/${routes[$ix]})
echo $content >$backup/$directory/${routes[$ix]}.json
jsoncontent=$(<$backup/$directory/${routes[$ix]}.json)
if [[ $jsoncontent == \[* ]]; then
echo -ne "$(date +"%Y-%m-%d %T") Creating a backup of /${routes[$ix]} was successful.\n" >>$backup/$directory/backup.log
echo -ne "${GREEN}Created json file for /${routes[$ix]}${COLORRESET}\n\n"
else
errorCounter=$[$errorCounter + 1]
echo -ne "$(date +"%Y-%m-%d %T") ERROR Creating a backup of /${routes[$ix]} failed.\n" >>$backup/$directory/backup.log
echo -ne "${RED}Error! JSON creation failed for /${routes[$ix]}${COLORRESET}${COLORRESET}\n\n"
fi
done
if [ "$errorCounter" -gt "0" ]; then
logfile=$backup/$directory/backup.log
echo -ne "${GREEN}Backup completed with ${RED}$errorCounter errors!${COLORRESET} For details have a look at the log file: $logfile \n"
else
echo -ne "${GREEN}Backup completed without errors!${COLORRESET}\n"
fi