Skip to content

Commit 8fc2933

Browse files
Add Backup/restore scripts (#2029)
* add backup/restore scripts * refactor utils to scripts
1 parent c0ff2f4 commit 8fc2933

File tree

9 files changed

+111
-90
lines changed

9 files changed

+111
-90
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ var/
3737
pip-log.txt
3838
pip-delete-this-directory.txt
3939
sentry_install_log*.txt
40-
sentry_clean_log*.txt
40+
sentry_reset_log*.txt
41+
sentry_restore_log*.txt
42+
sentry_backup_log*.txt
4143

4244
# Unit test / coverage reports
4345
htmlcov/

scripts/_lib.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env bash
2+
3+
set -eEuo pipefail
4+
5+
if [ -n "${DEBUG:-}" ]; then
6+
set -x
7+
fi
8+
9+
function confirm() {
10+
read -p "$1 [y/n] " confirmation
11+
if [ "$confirmation" != "y" ]; then
12+
echo "Canceled. 😅"
13+
exit
14+
fi
15+
}
16+
17+
# The purpose of this script is to make it easy to reset a local self-hosted
18+
# install to a clean state, optionally targeting a particular version.
19+
20+
function clean() {
21+
# If we have a version given, validate it.
22+
# ----------------------------------------
23+
# Note that arbitrary git refs won't work, because the *_IMAGE variables in
24+
# .env will almost certainly point to :latest. Tagged releases are generally
25+
# the only refs where these component versions are pinned, so enforce that
26+
# we're targeting a valid tag here. Do this early in order to fail fast.
27+
if [ -n "$version" ]; then
28+
set +e
29+
git rev-parse --verify --quiet "refs/tags/$version" >/dev/null
30+
if [ $? -gt 0 ]; then
31+
echo "Bad version: $version"
32+
exit
33+
fi
34+
set -e
35+
fi
36+
37+
false "noooo"
38+
39+
# Make sure they mean it.
40+
if [ "${FORCE_CLEAN:-}" == "1" ]; then
41+
echo "☠️ Seeing FORCE=1, forcing cleanup."
42+
echo
43+
else
44+
confirm "☠️ Warning! 😳 This is highly destructive! 😱 Are you sure you wish to proceed?"
45+
echo "Okay ... good luck! 😰"
46+
fi
47+
48+
# Hit the reset button.
49+
$dc down --volumes --remove-orphans --rmi local
50+
51+
# Remove any remaining (likely external) volumes with name matching 'sentry-.*'.
52+
for volume in $(docker volume list --format '{{ .Name }}' | grep '^sentry-'); do
53+
docker volume remove $volume >/dev/null &&
54+
echo "Removed volume: $volume" ||
55+
echo "Skipped volume: $volume"
56+
done
57+
58+
# If we have a version given, switch to it.
59+
if [ -n "$version" ]; then
60+
git checkout "$version"
61+
fi
62+
}
63+
64+
function backup() {
65+
chmod +w $(pwd)/sentry
66+
docker-compose run -v $(pwd)/sentry:/sentry-data/backup --rm -T -e SENTRY_LOG_LEVEL=CRITICAL web export /sentry-data/backup/backup.json
67+
}
68+
69+
function restore() {
70+
docker-compose run --rm -T web import /etc/sentry/backup.json
71+
}
72+
73+
# Needed variables to source error-handling script
74+
MINIMIZE_DOWNTIME="${MINIMIZE_DOWNTIME:-}"
75+
STOP_TIMEOUT=60
76+
77+
# Save logs in order to send envelope to Sentry
78+
log_file=sentry_"$cmd"_log-$(date +'%Y-%m-%d_%H-%M-%S').txt
79+
exec &> >(tee -a "$log_file")
80+
version=""
81+
82+
while (($#)); do
83+
case "$1" in
84+
--report-self-hosted-issues) REPORT_SELF_HOSTED_ISSUES=1 ;;
85+
--no-report-self-hosted-issues) REPORT_SELF_HOSTED_ISSUES=0 ;;
86+
*) version=$1 ;;
87+
esac
88+
shift
89+
done
90+
91+
# Source files needed to set up error-handling
92+
source install/dc-detect-version.sh
93+
source install/detect-platform.sh
94+
source install/error-handling.sh
95+
trap_with_arg cleanup ERR INT TERM EXIT

scripts/backup.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
cmd=backup
3+
source scripts/_lib.sh
4+
$cmd

scripts/reset.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
cmd=reset
3+
source scripts/_lib.sh
4+
$cmd

scripts/restore.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
cmd=restore
3+
source scripts/_lib.sh
4+
$cmd

unit-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
FORCE_CLEAN=1 "./utilities/clean.sh"
3+
FORCE_CLEAN=1 "./scripts/reset.sh"
44
fail=0
55
for test_file in _unit-test/*-test.sh; do
66
echo "🙈 Running $test_file ..."

utilities/clean.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

utilities/docker-cleanup.sh

Lines changed: 0 additions & 57 deletions
This file was deleted.

utilities/set-up-error-reporting-for-scripts.sh

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)