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

Added auto-remove for old backups.

This commit is contained in:
Dmitriy Haidiuk 2022-04-12 21:39:51 +03:00
parent 80b1fd7936
commit 1ea38070f6
4 changed files with 15 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

View File

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

View File

@ -24,11 +24,14 @@ pg_backup_s3:
POSTGRES_DATABASE: dbname POSTGRES_DATABASE: dbname
POSTGRES_USER: user POSTGRES_USER: user
POSTGRES_PASSWORD: password POSTGRES_PASSWORD: password
BACKUP_KEEP_DAYS: 7
``` ```
- Images are tagged by the major PostgreSQL version they support: `10`, `11`, `12`, `13`, or `14`. - Images are tagged by the major PostgreSQL version they support: `10`, `11`, `12`, `13`, or `14`.
- The `SCHEDULE` variable determines backup frequency. See go-cron schedules documentation [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules). Omit to run the backup immediately and then exit. - The `SCHEDULE` variable determines backup frequency. See go-cron schedules documentation [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules). Omit to run the backup immediately and then exit.
- If `PASSPHRASE` is provided, the backup will be encrypted using GPG. - If `PASSPHRASE` is provided, the backup will be encrypted using GPG.
- Run `docker exec <container name> sh backup.sh` to trigger a backup ad-hoc - Run `docker exec <container name> sh backup.sh` to trigger a backup ad-hoc
- Use `BACKUP_KEEP_DAYS` to set time for how long you want to keep backup.
## Restore ## Restore
> **WARNING:** DATA LOSS! All database objects will be dropped and re-created. > **WARNING:** DATA LOSS! All database objects will be dropped and re-created.

View File

@ -79,7 +79,16 @@ else
fi fi
echo "Uploading backup to $S3_BUCKET..." echo "Uploading backup to $S3_BUCKET..."
aws $aws_args s3 cp "$local_file" "$s3_uri" aws "$aws_args" s3 cp "$local_file" "$s3_uri"
rm "$local_file" rm "$local_file"
echo "Backup complete." echo "Backup complete."
if [ "$BACKUP_KEEP_DAYS" -ne 0 ]; then
date_from_remove=$(date -v -"${BACKUP_KEEP_DAYS}"d +"%Y-%m-%d")
backups_query="Contents[?LastModified<='${date_from_remove} 00:00:00'].{Key: Key}"
remove_backups=$(aws s3api list-objects-v2 --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 old backup from $S3_BUCKET..."
eval "$remove_backups";
echo "Removing complete."
fi