Skip to content

Commit

Permalink
GH-160: Timestamp database archive
Browse files Browse the repository at this point in the history
  • Loading branch information
markhobson committed Oct 7, 2024
1 parent 27a4c11 commit a6c3208
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 6 additions & 4 deletions cold-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ BACKUP_INSTANCE=schemes
RESTORE_INSTANCE=${BACKUP_INSTANCE}-backup
PGDATABASE=schemes
PGUSER=schemes
ARCHIVE=${PGDATABASE}-${ENVIRONMENT}.dump

# Create Cloud SQL instance for backup

Expand All @@ -27,15 +26,18 @@ gcloud sql instances create ${RESTORE_INSTANCE} \
--tier db-custom-1-3840 \
--edition enterprise

# Obtain latest backup id
# Obtain latest backup id and timestamp

BACKUP_ID=$(gcloud sql backups list \
BACKUP=$(gcloud sql backups list \
--project ${PROJECT} \
--instance ${BACKUP_INSTANCE} \
--sort-by "~windowStartTime" \
--limit 1 \
--format="value(id)"
--format="value(id,windowStartTime)"
)
BACKUP_ID=$(echo "${BACKUP}" | cut -f1)
BACKUP_TIMESTAMP=$(echo "${BACKUP}" | cut -f2)
ARCHIVE=${PGDATABASE}-${ENVIRONMENT}-$(date -d $BACKUP_TIMESTAMP -u +"%Y%m%dT%H%M%SZ").dump

# Restore latest backup to Cloud SQL instance

Expand Down
10 changes: 5 additions & 5 deletions docs/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ To download the latest database backup for storing offline:
./cold-backup.sh $ENVIRONMENT
```

This will create an encrypted compressed PostgreSQL custom-format archive `schemes-$ENVIRONMENT.dump.gz.gpg`.
This will create an encrypted compressed PostgreSQL custom-format archive `schemes-$ENVIRONMENT-$BACKUP_TIMESTAMP.dump.gz.gpg`.

## Restoring a cold backup

Expand All @@ -52,14 +52,14 @@ To restore a backup to a local or proxied database:

```bash
bw get password "Schemes Database Backup Passphrase" \
| gpg --batch --decrypt --passphrase-fd 0 --output schemes-$ENVIRONMENT.dump.gz schemes-$ENVIRONMENT.dump.gz.gpg \
&& rm schemes-$ENVIRONMENT.dump.gz.gpg
| gpg --batch --decrypt --passphrase-fd 0 --output $ARCHIVE.gz $ARCHIVE.gz.gpg \
&& rm $ARCHIVE.gz.gpg
```

1. Uncompress the archive:

```bash
gunzip schemes-$ENVIRONMENT.dump.gz
gunzip $ARCHIVE.gz
```

1. Restore the backup:
Expand All @@ -70,5 +70,5 @@ To restore a backup to a local or proxied database:
-e PGUSER=schemes \
-e PGPASSWORD=$PGPASSWORD \
postgres:16 \
pg_restore -h localhost -d schemes --no-owner < schemes-$ENVIRONMENT.dump
pg_restore -h localhost -d schemes --no-owner < $ARCHIVE
```

0 comments on commit a6c3208

Please sign in to comment.