-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_db.bash
75 lines (56 loc) · 2.18 KB
/
start_db.bash
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
#!/usr/bin bash
# wrapper script to start the postgres docker server, call dbeaver, and do periodic backups of the server
# workaround for use on Windows/WSL as the filesystems between Windows and Linux are somewhat incompatible
# (namely with permissions) and will not allow volume mounts that postgres requires
# To workaround such, we start a postgres service with a named volume to save data
# Then this script will execute a `docker exec` command to `pg_dumpall` into our local file system, then copy
# that dump to two other locations for archival.
# Disclaimer: I have no idea what impacts this has on performance, but it works well for small databases
# assumes this is running WSL2, not native Linux
# assumes dbeaver is downloaded into Program Files on Windows host
# adaptation to native Linux should be fairly easy though
function getEnv(){
export $(grep -v '^#' .env | xargs)
}
function serverUp(){
$(docker compose up -d)
}
function serverDown(){
$(docker compose down)
}
function dbeaverUp(){
# bring up dbeaver
exe="/mnt/c/Program Files/DBeaver/dbeaver.exe"
"$exe" </dev/null &>/dev/null &
}
function archiver(){
local day_time_readable=$(date +'%D %T')
echo "${day_time_readable} Beginning archive..."
local timestamp=$(date +'%Y%m%d_%H%M%S')
docker exec -u ${PG_USER} db-postgres-1 pg_dumpall > "${LOCAL_ARCHIVE}/${timestamp}.dump" && echo "${day_time_readable} Archival successful" || echo "${day_time_readable} Archival failed"
cp "${LOCAL_ARCHIVE}/${timestamp}.dump" "${FS_SERVER_ARCHIVE}"
}
function main(){
day_time_readable=$(date +'%D %T')
getEnv
echo "${day_time_readable} Starting server..."
serverUp
echo "${day_time_readable} Starting DBeaver..."
# dbeaverUp
echo "${day_time_readable} Initial archiving..."
archiver
while true; do
echo "Press 'q' to exit"
read -t 600 -n 1 char <&1
if [[ $char = q ]]; then
echo "${day_time_readable} Getting final archive..."
archiver
echo "Shutting server down..."
serverDown
break;
else
archiver
fi
done
}
main