Browse Source

Added argument DELETE_OLDER_THAN to automatically delete old s3 backups

pull/97/head
Zhong Huiwen 7 years ago
parent
commit
aadf88fed9
  1. 1
      mysql-backup-s3/README.md
  2. 39
      mysql-backup-s3/backup.sh
  3. 2
      mysql-backup-s3/install.sh

1
mysql-backup-s3/README.md

@ -25,6 +25,7 @@ $ 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 files older than e.g. "30 days" reference arguments from [date](https://ss64.com/bash/date.html)
### Automatic Periodic Backups

39
mysql-backup-s3/backup.sh

@ -60,6 +60,41 @@ copy_s3 () {
rm $SRC_FILE
}
deleteOld_s3 () {
if [ "${S3_ENDPOINT}" == "**None**" ]; then
AWS_ARGS=""
else
AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
fi
aws $AWS_ARGS s3 ls s3://$S3_BUCKET/$S3_PREFIX/ | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date -d"-${DELETE_OLDER_THAN}" +%s`
if [[ $createDate -lt $olderThan ]]
then
fileName=`echo $line|awk {'print $4'}`
echo $fileName
if [[ $fileName != "" ]]
then
aws $AWS_ARGS s3 rm s3://$S3_BUCKET/$S3_PREFIX/$fileName
echo "Deleted ${fileName} on S3"
fi
fi
done;
}
shouldDeleteOld_s3() {
if [ $? == 0 ]; then
if [ "${DELETE_OLDER_THAN}" != "**None**" ]; then
deleteOld_s3
fi
else
>&2 echo "Error deleting s3 files older than ${DELETE_OLDER_THAN}"
fi
}
# Multi file: yes
if [ ! -z "$(echo $MULTI_FILES | grep -i -E "(yes|true|1)")" ]; then
if [ "${MYSQLDUMP_DATABASE}" == "--all-databases" ]; then
@ -82,6 +117,8 @@ if [ ! -z "$(echo $MULTI_FILES | grep -i -E "(yes|true|1)")" ]; then
else
>&2 echo "Error creating dump of ${DB}"
fi
shouldDeleteOld_s3
done
# Multi file: no
else
@ -97,6 +134,8 @@ else
else
>&2 echo "Error creating dump of all databases"
fi
shouldDeleteOld_s3
fi
echo "SQL backup finished"

2
mysql-backup-s3/install.sh

@ -20,6 +20,8 @@ curl -L --insecure https://github.com/odise/go-cron/releases/download/v0.0.6/go-
chmod u+x /usr/local/bin/go-cron
apk del curl
# install coreutils for date -d
apk add --update coreutils
# cleanup
rm -rf /var/cache/apk/*
Loading…
Cancel
Save