-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp
153 lines (127 loc) · 2.72 KB
/
app
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
#!/bin/bash
set -e
cd "$(dirname "$0")"
COMMAND=$1
COMPOSE_PROJECT_NAME="sg"
show_help() {
echo "Usage: app COMMAND"
echo "Commands:"
echo " build: Build SG"
echo " start: Start SG"
echo " stop: Stop SG"
echo " restart: Restart SG"
echo " update: Update the SG codebase. Backup/Restore will run automatically."
echo " backup: Backup database to the 'mongo_backup' directory"
echo " restore: Restore database from the 'mongo_backup' directory"
echo " "
echo " clean: Clean up images, containers"
echo " destroy: Stop and remove the SG image. BACKUP FIRST!"
echo " rebuild: Rebuild SG (stop, destroy, start new). BACKUP FIRST!"
exit 1
}
remove_container_old() {
local OLD_CONTAINER=$(docker image ls -f 'reference=sg' --format '{{.ID}}')
if [ -n "$OLD_CONTAINER" ] ; then
stop
echo "Removing old container"
docker image rm $OLD_CONTAINER
else
echo "Could not find old container to remove."
fi
}
build() {
docker build --rm -t sg .
docker-compose up --build -d
}
clean() {
docker container prune -f
docker image prune --all -f
}
stop() {
docker-compose stop
}
start() {
docker-compose start
}
restart() {
docker-compose restart
}
_del_backup_in_container() {
docker exec -w /data sg-db rm -rf mongo_backup
}
backup() {
# Delete local backup
rm -rf mongo_backup
# Create a backup in mongo container
docker exec -w /data sg-db mongodump -o mongo_backup
# Copy it to local directory
docker cp sg-db:/data/mongo_backup .
# Delete backup from mongo container
_del_backup_in_container
}
restore() {
if [ -d "mongo_backup/sg/" ] ; then
# Delete possible leftover backup from mongo container
_del_backup_in_container
# Copy local backup to container
docker cp mongo_backup sg-db:/data/
# Restore from backup
docker exec -w /data sg-db mongorestore --drop mongo_backup
# Delete backup in container
_del_backup_in_container
else
echo "Backup not found in $PWD/mongo_backup/"
fi
}
update() {
local FOUND_REPO=0
if [ -d "./.git/config" ] && [ grep -q '\[remote "origin"\]' "./.git/config" ]; then
FOUND_REPO=1
fi
backup
docker-compose down
if [ $FOUND_REPO -eq 1 ]; then
git pull
fi
build
restore
if [ $FOUND_REPO -eq 0 ]; then
echo "Could not find a git repo to pull from, code was not updated."
fi
}
case ${COMMAND} in
'start')
start
;;
'stop')
stop
;;
'restart')
restart
;;
'destroy')
remove_container_old
;;
'build')
build
;;
'rebuild')
remove_container_old
build
;;
'clean')
clean
;;
'backup')
backup
;;
'restore')
restore
;;
'update')
update
;;
*)
show_help
;;
esac