Skip to content

Commit e90d0a0

Browse files
committed
Adding subdirectory optional variable.
1 parent c6935e7 commit e90d0a0

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ RUN apk add --no-cache \
1212
su-exec \
1313
dcron \
1414
gzip \
15-
&& mkdir -p /scripts /backups
15+
&& mkdir -p /scripts
1616

1717
COPY backup_full.sh /scripts/backup_full.sh
1818
COPY backup_incremental.sh /scripts/backup_incremental.sh

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ A simple, containerized solution for automated full and incremental backups of a
44

55
## Features
66

7-
- **Full Backups:** Scheduled `pg_dump` backups of your PostgreSQL database, compressed and stored in `/backups/full`.
8-
- **Incremental Backups:** Optionally archive PostgreSQL WAL files for point-in-time recovery, stored in `/backups/incremental`.
7+
- **Full Backups:** Scheduled `pg_dump` backups of your PostgreSQL database, compressed and stored in `/backups/full` or `/backups/$BACKUP_SUBDIR/full`.
8+
- **Incremental Backups:** Optionally archive PostgreSQL WAL files for point-in-time recovery, stored in `/backups/incremental` or `/backups/$BACKUP_SUBDIR/incremental`.
99
- **Retention Policies:** Automatically remove old backups based on configurable retention periods.
1010
- **Configurable Scheduling:** Use environment variables to control backup intervals via cron.
1111
- **Easy Integration:** Designed to run as a Docker container, with minimal configuration.
@@ -28,6 +28,7 @@ A simple, containerized solution for automated full and incremental backups of a
2828
| `RETENTION_INC_DAYS` | Days to keep incremental backups | `3` |
2929
| `BACKUP_FULL_INTERVAL` | Cron schedule for full backups | `0 2 * * 0` |
3030
| `BACKUP_INCREMENTAL_INTERVAL` | Cron schedule for incremental backups | `0 */6 * * *` |
31+
| `BACKUP_SUBDIR` | Subdirectory for backups to be stored | (undefined) |
3132

3233
### Volumes
3334

backup_full.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
BACKUP_SUBDIR="${BACKUP_SUBDIR:-}"
5+
if [ -n "$BACKUP_SUBDIR" ]; then
6+
BASE_BACKUP_DIR="/backups/$BACKUP_SUBDIR"
7+
else
8+
BASE_BACKUP_DIR="/backups"
9+
fi
10+
411
DATE=$(date +%F_%H-%M-%S)
5-
DEST_DIR="/backups/full/$DATE"
12+
DEST_DIR="$BASE_BACKUP_DIR/full/$DATE"
613
mkdir -p "$DEST_DIR"
714

815
export PGPASSWORD=$(cat $PGPASSWORD_FILE)
@@ -14,6 +21,6 @@ pg_dump -h "${POSTGRES_HOST}" \
1421
"${POSTGRES_DB}" | gzip > "$DEST_DIR/$BACKUP_NAME.gz"
1522

1623
# Apply retention
17-
find /backups/full -type d -mtime +${RETENTION_FULL_DAYS} -exec rm -rf {} +
24+
find $BASE_BACKUP_DIR/full -type d -mtime +${RETENTION_FULL_DAYS} -exec rm -rf {} +
1825

1926
echo "[$(date)] Full backup completed."

backup_incremental.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
BACKUP_SUBDIR="${BACKUP_SUBDIR:-}"
5+
if [ -n "$BACKUP_SUBDIR" ]; then
6+
BASE_BACKUP_DIR="/backups/$BACKUP_SUBDIR"
7+
else
8+
BASE_BACKUP_DIR="/backups"
9+
fi
10+
411
DATE=$(date +%F_%H-%M-%S)
5-
DEST_DIR="/backups/incremental/$DATE"
12+
DEST_DIR="$BASE_BACKUP_DIR/incremental/$DATE"
613
mkdir -p "$DEST_DIR"
714

815
echo "[$(date)] Performing incremental backup..."
@@ -11,7 +18,7 @@ echo "[$(date)] Performing incremental backup..."
1118
cp /wal_archive/* "$DEST_DIR/" || true
1219

1320
# Apply retention on the incremental backups themselves
14-
find /backups/incremental -type d -mtime +${RETENTION_INC_DAYS} -exec rm -rf {} +
21+
find $BASE_BACKUP_DIR/incremental -type d -mtime +${RETENTION_INC_DAYS} -exec rm -rf {} +
1522

1623
# Clean up WAL files using pg_archivecleanup
1724
# Determine the last WAL file to keep

0 commit comments

Comments
 (0)