Skip to content

Commit cc19a64

Browse files
committed
rclone added
1 parent 24f9076 commit cc19a64

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed

.env.example

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,16 @@ PRIMARY_DB=your_laterbase_primary_db_name
2525
PGADMIN_EMAIL=your_pgadmin_email@example.com
2626

2727
# Password for the default pgAdmin user login
28-
PGADMIN_PASSWORD=your_pgadmin_password
28+
PGADMIN_PASSWORD=your_pgadmin_password
29+
30+
# --- Backup Agent Configuration (Rclone & Retention) ---
31+
# Name of the rclone remote configured in your rclone.conf (e.g., "mygdrive", "s3remote")
32+
# Leave blank to disable cloud upload.
33+
RCLONE_REMOTE_NAME=
34+
35+
# Path within the rclone remote where backups should be stored (e.g., "resolve_backups/production")
36+
# Leave blank to disable cloud upload.
37+
RCLONE_REMOTE_PATH=
38+
39+
# Number of days to keep local backups in the ./backups volume (default is 7 if not set)
40+
BACKUP_RETENTION_DAYS=7

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This project, nicknamed "Laterbase", sets up a Docker-based environment specifically designed for **DaVinci Resolve PostgreSQL databases**. It consists of:
44

55
1. **PostgreSQL Standby Server:** Creates a hot standby replica of your primary DaVinci Resolve PostgreSQL database using streaming replication.
6-
2. **Hourly Backup Agent:** Performs hourly logical backups (`pg_dump`) of your primary DaVinci Resolve database.
6+
2. **Hourly Backup Agent:** Performs hourly logical backups (`pg_dump`) of your primary DaVinci Resolve database, optionally uploads them to cloud storage using `rclone`, and manages local backup retention.
77
3. **pgAdmin 4 UI:** Provides a web-based graphical interface for managing and monitoring both the primary DaVinci Resolve database and the standby replica.
88

99
<p align="center">
@@ -24,14 +24,21 @@ graph TD
2424
2525
subgraph "Laterbase (Docker Host)"
2626
StandbyDB[("laterbase-standby<br>PostgreSQL Standby")]
27-
BackupAgent["laterbase-backup-agent<br>(Hourly pg_dump)"]
27+
BackupAgent["laterbase-backup-agent<br>(Hourly pg_dump, rclone upload, retention)"]
2828
PgAdminUI[("laterbase-pgadmin<br>pgAdmin 4 UI")]
29-
BackupVolume[/backups Volume/]
29+
BackupVolume[/Local Backups<br>(./backups)/]
30+
RcloneConfigVolume[/Rclone Config<br>(./rclone_config)/]
31+
end
32+
33+
subgraph "Cloud Storage (Optional)"
34+
CloudBackup{{Rclone Remote<br>e.g., S3, Google Drive}}
3035
end
3136
3237
PrimaryDB -- Streaming Replication --> StandbyDB
3338
PrimaryDB -- pg_dump --> BackupAgent
3439
BackupAgent -- Writes .sql.gz --> BackupVolume
40+
BackupAgent -- Copies .sql.gz --> CloudBackup
41+
BackupAgent -- Reads Config --> RcloneConfigVolume
3542
PgAdminUI -- User Connects --> PrimaryDB
3643
PgAdminUI -- User Connects --> StandbyDB
3744
StandbyDB -- Uses --> ReplicationSlot
@@ -42,6 +49,8 @@ graph TD
4249
style PgAdminUI fill:#0b9e92,stroke:#333,stroke-width:2px
4350
style ReplicationSlot fill:#359e0b,stroke:#333,stroke-width:1px
4451
style BackupVolume fill:#9e8f0b,stroke:#333,stroke-width:1px
52+
style RcloneConfigVolume fill:#b0a01c,stroke:#333,stroke-width:1px
53+
style CloudBackup fill:#1caab0,stroke:#333,stroke-width:2px
4554
```
4655

4756
**Note on Physical Replication Slot:** A physical replication slot (`laterbase_standby_slot` in this setup) is a feature on the primary PostgreSQL server. It ensures that the primary server retains the necessary transaction logs (WAL segments) required by the standby server, even if the standby disconnects temporarily. This prevents the standby from falling too far behind and needing a full resynchronization.
@@ -56,6 +65,11 @@ graph TD
5665
* Replace `YOUR_PGADMIN_PASSWORD_HERE` with the password you want for the pgAdmin login.
5766
* Adjust `PRIMARY_PORT` or `PRIMARY_USER` if they differ from the defaults (5432, postgres).
5867
* Optionally, uncomment and set `POSTGRES_USER`, `POSTGRES_DB`, or `PGDATA` under the "Standby Server Configuration" section to override the defaults used by the standby service.
68+
* **Rclone Upload (Optional):**
69+
* Set `RCLONE_REMOTE_NAME` to the name of the remote you have configured in your `rclone.conf` file (e.g., `mygdrive`, `s3remote`). Leave blank to disable uploads.
70+
* Set `RCLONE_REMOTE_PATH` to the directory path within your rclone remote where backups should be stored (e.g., `resolve_backups/production`). Leave blank to disable uploads.
71+
* **Backup Retention (Optional):**
72+
* Set `BACKUP_RETENTION_DAYS` to the number of days you want to keep local backups in the `./backups` directory. Defaults to 7 if not set.
5973

6074
2. **Primary PostgreSQL Server Preparation (`PRIMARY_HOST`):**
6175
* Ensure the target database is marked as shared or accessible on the resolve box
@@ -77,6 +91,16 @@ graph TD
7791
mkdir backups
7892
```
7993

94+
4. **Rclone Configuration (Optional):**
95+
* If you want to use the cloud upload feature:
96+
* Create a directory named `rclone_config` in the same directory as `docker-compose.yml`:
97+
```bash
98+
mkdir rclone_config
99+
```
100+
* Place your configured `rclone.conf` file inside the `./rclone_config/` directory. The backup container will mount this directory read-only at `/config/rclone.conf`.
101+
* Ensure the remote name you use in `rclone.conf` matches the `RCLONE_REMOTE_NAME` set in your `.env` file.
102+
* **Security Note:** The `rclone.conf` file contains sensitive credentials. Ensure appropriate file permissions are set on the host machine (`chmod 600 ./rclone_config/rclone.conf`).
103+
80104
## Usage
81105

82106
1. **Build and Start Containers:**
@@ -109,5 +133,6 @@ graph TD
109133
* `setup_standby.sh`: Script run inside the standby container on first start to perform the initial base backup and configure replication.
110134
* `backup.sh`: Script run by cron inside the backup agent container to perform hourly `pg_dump` backups.
111135
* `crontab.txt`: Defines the cron schedule for `backup.sh`.
112-
* `./backups/` (Directory to be created): Host directory where backup files (`.sql.gz`) will be stored by the backup agent.
136+
* `./backups/` (Directory to be created): Host directory where local backup files (`.sql.gz`) will be stored by the backup agent.
137+
* `./rclone_config/` (Directory to be created, optional): Host directory containing the `rclone.conf` file for cloud uploads.
113138

backup/Dockerfile.backup

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ FROM debian:bullseye-slim
55
RUN apt-get update && apt-get install -y --no-install-recommends \
66
cron \
77
postgresql-client \
8+
rclone \
9+
ca-certificates \
810
&& rm -rf /var/lib/apt/lists/*
911

1012
# Create directory for backup scripts and backups
11-
RUN mkdir -p /etc/cron.d /app /backups
13+
RUN mkdir -p /etc/cron.d /app /backups /config
1214

1315
# Copy the backup script and crontab
1416
COPY backup.sh /app/backup.sh

backup/backup.sh

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ DB_USER="${PRIMARY_USER:-postgres}"
1212
DB_NAME="${PRIMARY_DB}" # This needs to be the actual Resolve DB name
1313
# PGPASSWORD should be set as an environment variable in docker-compose
1414

15+
# Rclone Configuration (Set these in docker-compose/.env)
16+
RCLONE_REMOTE_NAME="${RCLONE_REMOTE_NAME}" # e.g., mygdrive
17+
RCLONE_REMOTE_PATH="${RCLONE_REMOTE_PATH}" # e.g., resolve_backups
18+
19+
# Backup Retention (Set in docker-compose/.env, defaults to 7 days)
20+
BACKUP_RETENTION_DAYS="${BACKUP_RETENTION_DAYS:-7}"
21+
1522
BACKUP_DIR="/backups"
1623
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
1724
BACKUP_FILENAME="${DB_NAME}_backup_${TIMESTAMP}.sql.gz"
@@ -30,6 +37,14 @@ if [ -z "$PGPASSWORD" ]; then
3037
echo "$(date): Error: PGPASSWORD environment variable is not set." >> /var/log/cron.log
3138
exit 1
3239
fi
40+
if [ -z "$RCLONE_REMOTE_NAME" ]; then
41+
echo "$(date): Warning: RCLONE_REMOTE_NAME is not set. Backup will not be uploaded." >> /var/log/cron.log
42+
# Decide if this should be a fatal error (exit 1) or just a warning
43+
fi
44+
if [ -z "$RCLONE_REMOTE_PATH" ]; then
45+
echo "$(date): Warning: RCLONE_REMOTE_PATH is not set. Backup will not be uploaded." >> /var/log/cron.log
46+
# Decide if this should be a fatal error (exit 1) or just a warning
47+
fi
3348

3449
# --- Backup Execution ---
3550
echo "$(date): Starting backup of database '${DB_NAME}' from host '${DB_HOST}:${DB_PORT}' to ${BACKUP_PATH}" >> /var/log/cron.log
@@ -52,9 +67,24 @@ else
5267
exit 1
5368
fi
5469

55-
# --- Optional: Cleanup old backups (e.g., keep last 7 days) ---
56-
# echo "$(date): Cleaning up backups older than 7 days..." >> /var/log/cron.log
57-
# find "$BACKUP_DIR" -name "${DB_NAME}_backup_*.sql.gz" -type f -mtime +7 -delete
70+
# --- Rclone Upload ---
71+
if [ -n "$RCLONE_REMOTE_NAME" ] && [ -n "$RCLONE_REMOTE_PATH" ]; then
72+
RCLONE_DESTINATION="${RCLONE_REMOTE_NAME}:${RCLONE_REMOTE_PATH}"
73+
echo "$(date): Uploading backup to ${RCLONE_DESTINATION}..." >> /var/log/cron.log
74+
rclone copy "$BACKUP_PATH" "$RCLONE_DESTINATION" --config /config/rclone.conf --log-file /var/log/cron.log --log-level INFO
75+
if [ $? -eq 0 ]; then
76+
echo "$(date): Upload successful: ${BACKUP_FILENAME} to ${RCLONE_DESTINATION}" >> /var/log/cron.log
77+
else
78+
echo "$(date): Error: Upload failed for ${BACKUP_FILENAME} to ${RCLONE_DESTINATION}." >> /var/log/cron.log
79+
# Decide if upload failure should be fatal (exit 1) or just logged
80+
fi
81+
else
82+
echo "$(date): Skipping rclone upload as RCLONE_REMOTE_NAME or RCLONE_REMOTE_PATH is not set." >> /var/log/cron.log
83+
fi
84+
85+
# --- Local Cleanup ---
86+
echo "$(date): Cleaning up local backups older than ${BACKUP_RETENTION_DAYS} days in ${BACKUP_DIR}..." >> /var/log/cron.log
87+
find "$BACKUP_DIR" -name "${DB_NAME}_backup_*.sql.gz" -type f -mtime "+${BACKUP_RETENTION_DAYS}" -print -delete >> /var/log/cron.log 2>&1
5888

5989
echo "$(date): Backup process finished." >> /var/log/cron.log
6090

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,16 @@ services:
5151
# Optional: Define these in .env if different from defaults in backup.sh
5252
# PRIMARY_PORT: ${PRIMARY_PORT:-5432}
5353
# PRIMARY_USER: ${PRIMARY_USER:-postgres}
54+
# --- Rclone & Retention Configuration (Define in .env) ---
55+
RCLONE_REMOTE_NAME: ${RCLONE_REMOTE_NAME:-} # e.g., mygdrive
56+
RCLONE_REMOTE_PATH: ${RCLONE_REMOTE_PATH:-} # e.g., resolve_backups
57+
BACKUP_RETENTION_DAYS: ${BACKUP_RETENTION_DAYS:-7} # Default 7 days
5458
volumes:
5559
# Mount a local directory on the host to store the backups
5660
- ./backups:/backups
61+
# Mount a local directory containing rclone.conf
62+
# Create ./rclone_config directory and place your rclone.conf file there
63+
- ./rclone_config:/config:ro # Mount read-only for security
5764
restart: unless-stopped
5865
networks:
5966
- laterbase_net

0 commit comments

Comments
 (0)