Skip to content

Commit bfc1d36

Browse files
committed
DB backup: improve handling of slow backup export (and API errors)
1 parent 5a7aef3 commit bfc1d36

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

database-backup/scripts/other/backup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ readonly RCLONE_TARGET="s3-remote:${S3_BUCKET}/${S3_PATH}"
2424
case "$DB_PROVIDER" in
2525
scw)
2626
# Create (or fetch existing) backup
27-
echo "Creating (or fetching) backup…"
27+
echo "Creating (or fetching) backup ${BACKUP_NAME}"
2828
if ! backup_id=$(scw_fetch_or_create_backup "$DB_INSTANCE_ID" "$DB_NAME" "$BACKUP_NAME"); then
2929
echo "Failed to create backup: ${backup_id}" >&2
3030
exit 1

database-backup/scripts/other/lib/scw.sh

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ scw_fetch_or_create_backup() {
4343
scw_wait_on_backup() {
4444
scw_cmd rdb backup wait "$1" \
4545
timeout="$SCW_BACKUP_TIMEOUT" > /dev/null
46+
47+
# Because stable could be error as well, do our own wait just in case
48+
backup_status=
49+
until [ "$backup_status" = 'ready' ]; do
50+
if ! result=$(scw_cmd rdb backup get "$1" 2>&1); then
51+
echo "Failed to get backup status (wait for ready): $result" >&2
52+
return 1
53+
fi
54+
55+
backup_status=$(printf '%s\n' "$result" | jq -r '.status')
56+
case "$backup_status" in
57+
ready) ;;
58+
error) echo "Bakcup in error state!"; return 1 ;;
59+
*) sleep 1 ;;
60+
esac
61+
done
4662
}
4763

4864
# Params:
@@ -58,14 +74,15 @@ scw_get_ready_backups() {
5874
# Params:
5975
# - Database Backup ID
6076
scw_export_backup() {
61-
backup_status=
77+
scw_cmd rdb backup export "$1" >/dev/null
78+
}
79+
80+
scw_get_backup_export_url() {
81+
if ! result=$(scw_cmd rdb backup get "$1" 2>&1); then
82+
echo "Failed to get export: $result" >&2
83+
return 1
84+
fi
6285

63-
until [ "$backup_status" = 'ready' ]; do
64-
result=$(scw_cmd rdb backup export "$1" 2>&1) || return 1
65-
backup_status=$(printf '%s\n' "$result" | jq -r '.status')
66-
[ "$backup_status" = 'ready' ] || sleep 1
67-
done
68-
6986
printf '%s\n' "$result" | jq -r '.download_url'
7087
}
7188

@@ -84,15 +101,23 @@ scw_sync_backups() {
84101
scw_get_ready_backups "$1" "$2" | while read -r backup; do
85102
backup_id=$(echo "$backup" | jq -r '.ID')
86103
backup_name=$(echo "$backup" | jq -r '.name')
87-
echo "Syncing backup ${backup_name} (${backup_id})…"
88104

89-
# Get export URL
90-
if ! download_url=$(scw_export_backup "$backup_id"); then
105+
# Get export URL. Note that this is not instant, we need to wait until they're finished
106+
echo "Exporting backup ${backup_name} (${backup_id})…"
107+
if ! scw_export_backup "$backup_id"; then
108+
echo "Failed to export backup ${backup_id}" >&2
109+
continue
110+
fi
111+
112+
echo "Waiting for backup ${backup_id} to be ready…"
113+
scw_wait_on_backup "$backup_id"
114+
if ! download_url=$(scw_get_backup_export_url "$backup_id"); then
91115
echo "Failed to export backup ${backup_id}: ${download_url}" >&2
92116
continue
93117
fi
94118

95119
# Sync it
120+
echo "Syncing backup ${backup_name} (${backup_id})…"
96121
"$sync_callback" "$download_url" "$backup_name"
97122

98123
# Delete backup

0 commit comments

Comments
 (0)