1
0
mirror of https://github.com/garraflavatra/docker-volume-s3-backup.git synced 2025-05-17 13:14:38 +00:00

Updated removing old backups logic.

This commit is contained in:
Dmitriy Haidiuk 2022-04-13 11:16:23 +03:00
parent f08931bf80
commit 2b28f9f0ec
3 changed files with 11 additions and 6 deletions

View File

@ -19,7 +19,7 @@ ENV S3_ENDPOINT ''
ENV S3_S3V4 'no'
ENV SCHEDULE ''
ENV PASSPHRASE ''
ENV BACKUP_KEEP_DAYS 7
ENV BACKUP_KEEP_DAYS ''
ADD src/run.sh run.sh
ADD src/backup.sh backup.sh

View File

@ -24,7 +24,7 @@ pg_backup_s3:
POSTGRES_DATABASE: dbname
POSTGRES_USER: user
POSTGRES_PASSWORD: password
BACKUP_KEEP_DAYS: 7
BACKUP_KEEP_DAYS: 7 // Disabled by default.
```
- Images are tagged by the major PostgreSQL version they support: `10`, `11`, `12`, `13`, or `14`.

View File

@ -84,12 +84,17 @@ rm "$local_file"
echo "Backup complete."
if [ "$BACKUP_KEEP_DAYS" -ne 0 ]; then
ms=$((86400*BACKUP_KEEP_DAYS))
date_from_remove=$(date -d "@$(($(date +%s) - ms))" +%Y-%m-%d)
if [ -n "$BACKUP_KEEP_DAYS" ]; then
sec=$((86400*BACKUP_KEEP_DAYS))
date_from_remove=$(date -d "@$(($(date +%s) - sec))" +%Y-%m-%d)
backups_query="Contents[?LastModified<='${date_from_remove} 00:00:00'].{Key: Key}"
echo "Removing old backup from $S3_BUCKET..."
aws s3api list-objects --bucket "${S3_BUCKET}" --prefix "${S3_PREFIX}" --query "${backups_query}" --output text | xargs -n1 -t -I 'KEY' aws s3 rm s3://"${S3_BUCKET}"/'KEY'
aws s3api list-objects \
--bucket "${S3_BUCKET}" \
--prefix "${S3_PREFIX}" \
--query "${backups_query}" \
--output text \
| xargs -n1 -t -I 'KEY' aws s3 rm s3://"${S3_BUCKET}"/'KEY'
echo "Removing complete."
fi