-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage
executable file
·140 lines (115 loc) · 2.68 KB
/
manage
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
#!/usr/bin/env bash
set -eu
nuke() {
echo "Deleting the stack"
docker compose kill
docker compose down -v
rm ./test/fakemount/* -rf
echo "Done (Deleting the stack)"
}
dcup() {
echo "Starting up key docker services"
docker compose up -d iiif sftpgo db oni-agent-staging oni-agent-prod oni-staging oni-prod
echo "Done (Starting up key docker services)"
}
backup() {
dir=$(realpath $1)
mkdir -p $dir
echo "Taking down the stack gracefully"
docker compose down
echo "Done (Taking down the stack gracefully)"
echo "Deleting old backups (if any)"
vols="$dir/vols.tar"
fakemount="$dir/fakemount.tar"
sudo rm -f $vols
sudo rm -f $fakemount
echo "Done (Deleting old backups (if any))"
echo "Writing new backup files"
sudo tar -cpf $fakemount ./test/fakemount
sudo su -c "cd /var/lib/docker/volumes && tar -cpf $vols ./nca*"
echo "Done (Writing new backup files)"
dcup
}
migrate() {
docker compose run --rm workers wait_for_database
}
load_seed_data() {
docker compose run --rm workers wait_for_database
docker compose run --rm workers mysql -unca -pnca -Dnca -hdb < ./docker/mysql/nca-seed-data.sql
}
restore() {
nuke
dir="$1"
echo "Restoring from $dir"
vols="$dir/vols.tar"
fakemount="$dir/fakemount.tar"
sudo tar -xspf $fakemount ./test/fakemount
sudo su -c "cd /var/lib/docker/volumes && tar -mxspf $vols"
echo "Done (Restoring from $dir)"
dcup
sleep 2
echo "Hacking SFTPGo 'home' volume permissions"
sudo chown $(whoami):$(whoami) /var/lib/docker/volumes/nca_sftpgo-home/_data
echo "Done (Hacking SFTPGo 'home' volume permissions)"
}
build() {
echo "Building NCA images"
docker compose build
echo "Done (Building NCA images)"
}
resetfakemount() {
pushd .
cd ./test
./makemine.sh
rm fakemount/* -rf
go run copy-sources.go .
./make-older.sh
popd
}
reset() {
echo "Resetting the stack"
build
docker compose down -v
migrate
load_seed_data
resetfakemount
echo "Done (Resetting the stack)"
}
# This ensures the first thing I do is type my password so I can walk away
# without unexpected sudo prompts holding up the process
sudo echo
dir="./backup/${2:-default}"
case ${1:-} in
reset)
reset
;;
build)
build
;;
nuke)
nuke
;;
backup)
echo 'Backing up to "'$dir'" in two seconds....'
sleep 2
backup $dir
;;
restore)
sec=2
dir=$(realpath $dir)
if [[ ! -d $dir ]]; then
dirname=${dir%/*}
wildcard="${dir##*/}*"
dir=$(find $dirname -mindepth 1 -maxdepth 1 -type d -name "$wildcard")
sec=5
fi
echo 'Restoring from "'$dir'" in '"$sec seconds...."
sleep $sec
restore $dir
;;
migrate)
migrate
;;
*)
echo "Usage: ./manage <reset|build|nuke|backup|restore|migrate>"
esac