Skip to content

Commit

Permalink
Add support for latest backup symlink
Browse files Browse the repository at this point in the history
  • Loading branch information
prodrigestivill committed May 21, 2024
1 parent 2feedd1 commit 4d1f860
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
POSTGRES_PASSFILE_STORE: "**None**"
POSTGRES_CLUSTER: "FALSE"
BACKUP_SUFFIX: ".sql.gz"
BACKUP_LATEST_TYPE: "symlink"
BACKUP_KEEP_DAYS: 7
BACKUP_KEEP_WEEKS: 4
BACKUP_KEEP_MONTHS: 6
Expand All @@ -72,6 +73,7 @@ jobs:
POSTGRES_PORT: 5432
POSTGRES_EXTRA_OPTS: "-Z0 -Fd"
BACKUP_SUFFIX: ""
BACKUP_LATEST_TYPE: "symlink"
BACKUP_DIR: ".data"
# Defaults
POSTGRES_DB_FILE: "**None**"
Expand Down Expand Up @@ -108,6 +110,7 @@ jobs:
POSTGRES_PASSWORD_FILE: "**None**"
POSTGRES_PASSFILE_STORE: "**None**"
BACKUP_SUFFIX: ".sql.gz"
BACKUP_LATEST_TYPE: "symlink"
BACKUP_KEEP_DAYS: 7
BACKUP_KEEP_WEEKS: 4
BACKUP_KEEP_MONTHS: 6
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Most variables are the same as in the [official postgres image](https://hub.dock
| BACKUP_KEEP_WEEKS | Number of weekly backups to keep before removal. Defaults to `4`. |
| BACKUP_KEEP_MONTHS | Number of monthly backups to keep before removal. Defaults to `6`. |
| BACKUP_KEEP_MINS | Number of minutes for `last` folder backups to keep before removal. Defaults to `1440`. |
| BACKUP_LATEST_TYPE | Type of `latest` pointer (`symlink`,`hardlink`,`none`). Defaults to `symlink`. |
| HEALTHCHECK_PORT | Port listening for cron-schedule health check. Defaults to `8080`. |
| POSTGRES_DB | Comma or space separated list of postgres databases to backup. If POSTGRES_CLUSTER is set this refers to the database to connect to for dumping global objects and discovering what other databases should be dumped (typically is either `postgres` or `template1`). Required. |
| POSTGRES_DB_FILE | Alternative to POSTGRES_DB, but with one database per line, for usage with docker secrets. |
Expand Down
1 change: 1 addition & 0 deletions alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ENV POSTGRES_DB="**None**" \
SCHEDULE="@daily" \
BACKUP_DIR="/backups" \
BACKUP_SUFFIX=".sql.gz" \
BACKUP_LATEST_TYPE="symlink" \
BACKUP_KEEP_DAYS=7 \
BACKUP_KEEP_WEEKS=4 \
BACKUP_KEEP_MONTHS=6 \
Expand Down
24 changes: 16 additions & 8 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,22 @@ for DB in ${POSTGRES_DBS}; do
ln -vf "${FILE}" "${MFILE}"
fi
# Update latest symlinks
echo "Point last backup file to this last backup..."
ln -svf "${LAST_FILENAME}" "${BACKUP_DIR}/last/${DB}-latest${BACKUP_SUFFIX}"
echo "Point latest daily backup to this last backup..."
ln -svf "${DAILY_FILENAME}" "${BACKUP_DIR}/daily/${DB}-latest${BACKUP_SUFFIX}"
echo "Point latest weekly backup to this last backup..."
ln -svf "${WEEKLY_FILENAME}" "${BACKUP_DIR}/weekly/${DB}-latest${BACKUP_SUFFIX}"
echo "Point latest monthly backup to this last backup..."
ln -svf "${MONTHY_FILENAME}" "${BACKUP_DIR}/monthly/${DB}-latest${BACKUP_SUFFIX}"
LATEST_LN_ARG=""
if [ "${BACKUP_LATEST_TYPE}" = "symlink" ]; then
LATEST_LN_ARG="-s"
fi
if [ "${BACKUP_LATEST_TYPE}" = "symlink" -o "${BACKUP_LATEST_TYPE}" = "hardlink" ]; then
echo "Point last backup file to this last backup..."
ln "${LATEST_LN_ARG}" -vf "${LAST_FILENAME}" "${BACKUP_DIR}/last/${DB}-latest${BACKUP_SUFFIX}"
echo "Point latest daily backup to this last backup..."
ln "${LATEST_LN_ARG}" -vf "${DAILY_FILENAME}" "${BACKUP_DIR}/daily/${DB}-latest${BACKUP_SUFFIX}"
echo "Point latest weekly backup to this last backup..."
ln "${LATEST_LN_ARG}" -vf "${WEEKLY_FILENAME}" "${BACKUP_DIR}/weekly/${DB}-latest${BACKUP_SUFFIX}"
echo "Point latest monthly backup to this last backup..."
ln "${LATEST_LN_ARG}" -vf "${MONTHY_FILENAME}" "${BACKUP_DIR}/monthly/${DB}-latest${BACKUP_SUFFIX}"
else # [ "${BACKUP_LATEST_TYPE}" = "none" ]
echo "Not updating lastest backup."
fi
#Clean old files
echo "Cleaning older files for ${DB} database from ${POSTGRES_HOST}..."
find "${BACKUP_DIR}/last" -maxdepth 1 -mmin "+${KEEP_MINS}" -name "${DB}-*${BACKUP_SUFFIX}" -exec rm -rvf '{}' ';'
Expand Down
1 change: 1 addition & 0 deletions debian.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ENV POSTGRES_DB="**None**" \
SCHEDULE="@daily" \
BACKUP_DIR="/backups" \
BACKUP_SUFFIX=".sql.gz" \
BACKUP_LATEST_TYPE="symlink" \
BACKUP_KEEP_DAYS=7 \
BACKUP_KEEP_WEEKS=4 \
BACKUP_KEEP_MONTHS=6 \
Expand Down

0 comments on commit 4d1f860

Please sign in to comment.