5 # This script wants these variable to be set.
7 ## S3_BUCKET <- The name of the bucket where the backups are stored
8 ## S3_ENDPOINT <- The endpoint of the S3 service
9 ## AWS_ACCESS_KEY_ID <- Access credentials
10 ## AWS_SECRET_ACCESS_KEY <- Access credentials
11 ## DAYS_TO_RETAIN <- The TTL for the backups === number of backups to keep.
13 # Sanity check to avoid removing all backups.
14 [[ "$DAYS_TO_RETAIN" -lt 1 ]] && DAYS_TO_RETAIN=1
16 function get_records {
19 aws s3api list-objects \
20 --bucket ${S3_BUCKET} \
21 --endpoint-url ${S3_ENDPOINT} \
22 --query "Contents[?LastModified<='${before_date}'][].{Key: Key}"
25 function remove_old_backups {
26 before_date=$(date --iso-8601=seconds -d "-${DAYS_TO_RETAIN} days")
27 now=$(date --iso-8601=seconds)
29 del_records=$(get_records "${before_date}")
30 all_records=$(get_records "${now}")
36 echo ${row} | base64 --decode | jq -r ${1}
39 for row in $(echo "${del_records}" | jq -r '.[] | @base64'); do
40 del_paths+=($(_jq '.Key'))
43 for row in $(echo "${all_records}" | jq -r '.[] | @base64'); do
44 all_paths+=($(_jq '.Key'))
47 # Number of backups left if all old backups are removed.
48 left=$((${#all_paths[@]} - ${#del_paths[@]}))
50 # We ALWAYS keep N backups even if their TTL has expired!
51 if (( ${left} < ${DAYS_TO_RETAIN} )); then
52 num_to_delete=$((${#all_paths[@]} - ${DAYS_TO_RETAIN}))
54 num_to_delete=${#del_paths[@]}
57 for path in "${del_paths[@]::${num_to_delete}}"; do
58 aws s3 rm "s3://${S3_BUCKET}/${path}" \
59 --endpoint-url "${S3_ENDPOINT}"