913794a5dd35ffbc62fb0b5fb0d0de08fe73b331
[ric-plt/ric-dep.git] / helm / 3rdparty / influxdb / files / backup-retention-script.sh
1 #! /usr/bin/env bash
2
3 set -e
4
5 # This script wants these variable to be set.
6
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.
12
13 # Sanity check to avoid removing all backups.
14 [[ "$DAYS_TO_RETAIN" -lt 1 ]] && DAYS_TO_RETAIN=1
15
16 function get_records {
17     before_date="$1"
18
19     aws s3api list-objects \
20         --bucket ${S3_BUCKET} \
21         --endpoint-url ${S3_ENDPOINT} \
22         --query "Contents[?LastModified<='${before_date}'][].{Key: Key}"
23 }
24
25 function remove_old_backups {
26     before_date=$(date --iso-8601=seconds -d "-${DAYS_TO_RETAIN} days")  
27     now=$(date --iso-8601=seconds)
28
29     del_records=$(get_records "${before_date}")
30     all_records=$(get_records "${now}")
31
32     del_paths=()
33     all_paths=()
34     
35     function _jq {
36         echo ${row} | base64 --decode | jq -r ${1}
37     }
38
39     for row in $(echo "${del_records}" | jq -r '.[] | @base64'); do       
40         del_paths+=($(_jq '.Key'))                
41     done
42
43     for row in $(echo "${all_records}" | jq -r '.[] | @base64'); do
44         all_paths+=($(_jq '.Key'))                
45     done
46
47     # Number of backups left if all old backups are removed.
48     left=$((${#all_paths[@]} - ${#del_paths[@]}))
49
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}))
53     else
54         num_to_delete=${#del_paths[@]}
55     fi
56
57     for path in "${del_paths[@]::${num_to_delete}}"; do
58         aws s3 rm "s3://${S3_BUCKET}/${path}" \
59             --endpoint-url "${S3_ENDPOINT}"
60     done
61 }
62
63 # Installs jq.
64 yum install -y jq
65
66 remove_old_backups