-
Notifications
You must be signed in to change notification settings - Fork 0
/
nettverksdagen
executable file
·105 lines (97 loc) · 2.64 KB
/
nettverksdagen
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
#!/bin/bash
backup_root_dir="/opt/backups"
max_backups=10
backup() {
local backup_time="$(date --rfc-3339='seconds' | sed 's/\ /_/g')"
local backup_dir="$backup_root_dir/$backup_time"
rsync -a fileserver/uploads "$backup_dir" &> /dev/null
rsync -a ghost/data "$backup_dir" &> /dev/null
docker exec postgres pg_dumpall -c -U postgres > "$backup_dir/api_db.sql"
echo "$backup_time"
}
restore() {
local backup_time="$1"
local backup_dir="$backup_root_dir/$backup_time"
rsync -a "$backup_dir/uploads" fileserver &> /dev/null
rsync -a "$backup_dir/data" ghost &> /dev/null
docker exec -i postgres psql -q -U postgres < "$backup_dir/api_db.sql" &> /dev/null
echo "$backup_time"
}
start() {
#git reset --hard origin/master
git pull --ff-only
docker compose -f docker-compose-prod.yml up --build --detach
}
stop() {
local backup_time="$(backup)"
docker compose -f docker-compose-prod.yml down --volumes &> /dev/null
echo "$backup_time"
}
restart() {
#git reset --hard origin/master
git pull --ff-only
docker compose -f docker-compose-prod.yml build --pull
local backup_time="$(backup)"
docker compose -f docker-compose-prod.yml down --volumes
docker compose -f docker-compose-prod.yml up --build --detach
sleep 10
restore "$backup_time"
}
prune() {
local backups=$(/bin/ls $backup_root_dir)
# 'backups' is an empty string if no backups are present,
# and 'wc -l' returns 1 for an empty string, so we check if
# 'backups' is empty before proceeding
if [[ ! -z "$backups" ]]; then
local num_backups=$(echo "$backups" | wc -l)
if (( "$num_backups" > "$max_backups" )); then
local old_backups=$(echo "$backups" | head -n $(("$num_backups" - "$max_backups")))
for old_backup in $old_backups; do
rm -r "$backup_root_dir/$old_backup"
done
echo "$old_backups"
fi
fi
}
case $1 in
backup)
backup_time="$(backup)"
echo "$backup_time"
;;
restore)
backup_time="$(restore $2)"
echo "$backup_time"
;;
start)
start
;;
stop)
backup_time="$(stop)"
echo "$backup_time"
;;
restart)
backup_time="$(stop)"
start
sleep 10
restore "$backup_time" &> /dev/null
echo "$backup_time"
;;
fast-restart)
restart
;;
prune)
deleted_backups="$(prune)"
# 'deleted_backups' is an empty string if no backups were deleted,
# and 'wc -l' returns 1 for an empty string, so we have to check if
# 'deleted_backups' is empty to avoid printing that a backup was
# deleted
if [[ ! -z "$deleted_backups" ]]; then
num_deleted_backups=$(echo "$deleted_backups" | wc -l)
echo "Deleted $num_deleted_backups backups"
fi
;;
*)
echo "Unknown subcommand"
exit 1
;;
esac