Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete files older than #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mysql-backup-s3/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ENV S3_PREFIX 'backup'
ENV S3_FILENAME **None**
ENV MULTI_FILES no
ENV SCHEDULE **None**
ENV DELETE_OLDER_THAN **None**

ADD run.sh run.sh
ADD backup.sh backup.sh
Expand Down
7 changes: 7 additions & 0 deletions mysql-backup-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ $ docker run -e S3_ACCESS_KEY_ID=key -e S3_SECRET_ACCESS_KEY=secret -e S3_BUCKET
- `S3_S3V4` set to `yes` to enable AWS Signature Version 4, required for [minio](https://minio.io) servers (default: no)
- `MULTI_FILES` Allow to have one file per database if set `yes` default: no)
- `SCHEDULE` backup schedule time, see explainatons below
- `DELETE_OLDER_THAN` delete old backups, see explanation and warning below

### Automatic Periodic Backups

You can additionally set the `SCHEDULE` environment variable like `-e SCHEDULE="@daily"` to run the backup automatically.

More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules).

### Delete Old Backups

You can additionally set the `DELETE_OLDER_THAN` environment variable like `-e DELETE_OLDER_THAN="30 days ago"` to delete old backups.

WARNING: this will delete all files in the S3_PREFIX path, not just those created by this script.
18 changes: 18 additions & 0 deletions mysql-backup-s3/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,22 @@ else
fi
fi

if [ "${DELETE_OLDER_THAN}" != "**None**" ]; then
aws $AWS_ARGS s3 ls s3://$S3_BUCKET/$S3_PREFIX/ | grep " PRE " -v | while read -r line;
do
created=`echo $line|awk {'print $1" "$2'}`
created=`date -d "$created" +%s`
older_than=`date -d "$DELETE_OLDER_THAN" +%s`
if [ $created -lt $older_than ]
then
fileName=`echo $line|awk {'print $4'}`
if [ $fileName != "" ]
then
printf 'Deleting "%s"\n' $fileName
aws $AWS_ARGS s3 rm s3://$S3_BUCKET/$S3_PREFIX/$fileName
fi
fi
done;
fi

echo "SQL backup finished"
1 change: 1 addition & 0 deletions postgres-backup-s3/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ENV S3_PATH 'backup'
ENV S3_ENDPOINT **None**
ENV S3_S3V4 no
ENV SCHEDULE **None**
ENV DELETE_OLDER_THAN **None**

ADD run.sh run.sh
ADD backup.sh backup.sh
Expand Down
6 changes: 6 additions & 0 deletions postgres-backup-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ You can additionally set the `SCHEDULE` environment variable like `-e SCHEDULE="

More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules).

### Delete Old Backups

You can additionally set the `DELETE_OLDER_THAN` environment variable like `-e DELETE_OLDER_THAN="30 days ago"` to delete old backups.

WARNING: this will delete all files in the S3_PREFIX path, not just those created by this script.

18 changes: 18 additions & 0 deletions postgres-backup-s3/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,22 @@ echo "Uploading dump to $S3_BUCKET"

cat dump.sql.gz | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz || exit 2

if [ "${DELETE_OLDER_THAN}" != "**None**" ]; then
aws $AWS_ARGS s3 ls s3://$S3_BUCKET/$S3_PREFIX/ | grep " PRE " -v | while read -r line;
do
created=`echo $line|awk {'print $1" "$2'}`
created=`date -d "$created" +%s`
older_than=`date -d "$DELETE_OLDER_THAN" +%s`
if [ $created -lt $older_than ]
then
fileName=`echo $line|awk {'print $4'}`
if [ $fileName != "" ]
then
printf 'Deleting "%s"\n' $fileName
aws $AWS_ARGS s3 rm s3://$S3_BUCKET/$S3_PREFIX/$fileName
fi
fi
done;
fi

echo "SQL backup uploaded successfully"