Skip to content

Commit 4d1f860

Browse files
Add support for latest backup symlink
1 parent 2feedd1 commit 4d1f860

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
POSTGRES_PASSFILE_STORE: "**None**"
5151
POSTGRES_CLUSTER: "FALSE"
5252
BACKUP_SUFFIX: ".sql.gz"
53+
BACKUP_LATEST_TYPE: "symlink"
5354
BACKUP_KEEP_DAYS: 7
5455
BACKUP_KEEP_WEEKS: 4
5556
BACKUP_KEEP_MONTHS: 6
@@ -72,6 +73,7 @@ jobs:
7273
POSTGRES_PORT: 5432
7374
POSTGRES_EXTRA_OPTS: "-Z0 -Fd"
7475
BACKUP_SUFFIX: ""
76+
BACKUP_LATEST_TYPE: "symlink"
7577
BACKUP_DIR: ".data"
7678
# Defaults
7779
POSTGRES_DB_FILE: "**None**"
@@ -108,6 +110,7 @@ jobs:
108110
POSTGRES_PASSWORD_FILE: "**None**"
109111
POSTGRES_PASSFILE_STORE: "**None**"
110112
BACKUP_SUFFIX: ".sql.gz"
113+
BACKUP_LATEST_TYPE: "symlink"
111114
BACKUP_KEEP_DAYS: 7
112115
BACKUP_KEEP_WEEKS: 4
113116
BACKUP_KEEP_MONTHS: 6

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Most variables are the same as in the [official postgres image](https://hub.dock
7979
| BACKUP_KEEP_WEEKS | Number of weekly backups to keep before removal. Defaults to `4`. |
8080
| BACKUP_KEEP_MONTHS | Number of monthly backups to keep before removal. Defaults to `6`. |
8181
| BACKUP_KEEP_MINS | Number of minutes for `last` folder backups to keep before removal. Defaults to `1440`. |
82+
| BACKUP_LATEST_TYPE | Type of `latest` pointer (`symlink`,`hardlink`,`none`). Defaults to `symlink`. |
8283
| HEALTHCHECK_PORT | Port listening for cron-schedule health check. Defaults to `8080`. |
8384
| 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. |
8485
| POSTGRES_DB_FILE | Alternative to POSTGRES_DB, but with one database per line, for usage with docker secrets. |

alpine.Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ENV POSTGRES_DB="**None**" \
2323
SCHEDULE="@daily" \
2424
BACKUP_DIR="/backups" \
2525
BACKUP_SUFFIX=".sql.gz" \
26+
BACKUP_LATEST_TYPE="symlink" \
2627
BACKUP_KEEP_DAYS=7 \
2728
BACKUP_KEEP_WEEKS=4 \
2829
BACKUP_KEEP_MONTHS=6 \

backup.sh

+16-8
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,22 @@ for DB in ${POSTGRES_DBS}; do
121121
ln -vf "${FILE}" "${MFILE}"
122122
fi
123123
# Update latest symlinks
124-
echo "Point last backup file to this last backup..."
125-
ln -svf "${LAST_FILENAME}" "${BACKUP_DIR}/last/${DB}-latest${BACKUP_SUFFIX}"
126-
echo "Point latest daily backup to this last backup..."
127-
ln -svf "${DAILY_FILENAME}" "${BACKUP_DIR}/daily/${DB}-latest${BACKUP_SUFFIX}"
128-
echo "Point latest weekly backup to this last backup..."
129-
ln -svf "${WEEKLY_FILENAME}" "${BACKUP_DIR}/weekly/${DB}-latest${BACKUP_SUFFIX}"
130-
echo "Point latest monthly backup to this last backup..."
131-
ln -svf "${MONTHY_FILENAME}" "${BACKUP_DIR}/monthly/${DB}-latest${BACKUP_SUFFIX}"
124+
LATEST_LN_ARG=""
125+
if [ "${BACKUP_LATEST_TYPE}" = "symlink" ]; then
126+
LATEST_LN_ARG="-s"
127+
fi
128+
if [ "${BACKUP_LATEST_TYPE}" = "symlink" -o "${BACKUP_LATEST_TYPE}" = "hardlink" ]; then
129+
echo "Point last backup file to this last backup..."
130+
ln "${LATEST_LN_ARG}" -vf "${LAST_FILENAME}" "${BACKUP_DIR}/last/${DB}-latest${BACKUP_SUFFIX}"
131+
echo "Point latest daily backup to this last backup..."
132+
ln "${LATEST_LN_ARG}" -vf "${DAILY_FILENAME}" "${BACKUP_DIR}/daily/${DB}-latest${BACKUP_SUFFIX}"
133+
echo "Point latest weekly backup to this last backup..."
134+
ln "${LATEST_LN_ARG}" -vf "${WEEKLY_FILENAME}" "${BACKUP_DIR}/weekly/${DB}-latest${BACKUP_SUFFIX}"
135+
echo "Point latest monthly backup to this last backup..."
136+
ln "${LATEST_LN_ARG}" -vf "${MONTHY_FILENAME}" "${BACKUP_DIR}/monthly/${DB}-latest${BACKUP_SUFFIX}"
137+
else # [ "${BACKUP_LATEST_TYPE}" = "none" ]
138+
echo "Not updating lastest backup."
139+
fi
132140
#Clean old files
133141
echo "Cleaning older files for ${DB} database from ${POSTGRES_HOST}..."
134142
find "${BACKUP_DIR}/last" -maxdepth 1 -mmin "+${KEEP_MINS}" -name "${DB}-*${BACKUP_SUFFIX}" -exec rm -rvf '{}' ';'

debian.Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ ENV POSTGRES_DB="**None**" \
3737
SCHEDULE="@daily" \
3838
BACKUP_DIR="/backups" \
3939
BACKUP_SUFFIX=".sql.gz" \
40+
BACKUP_LATEST_TYPE="symlink" \
4041
BACKUP_KEEP_DAYS=7 \
4142
BACKUP_KEEP_WEEKS=4 \
4243
BACKUP_KEEP_MONTHS=6 \

0 commit comments

Comments
 (0)