Skip to content

Commit 695ad44

Browse files
stoewerFxKu
andauthored
Logical backup retention time (zalando#1337)
* Add optional logical backup retention time * Set defaults for potentially unbound variables, so that the script will work with older operator versions * Document retention time parameter for logical backups * Add retention time parameter to resources and charts Co-authored-by: Felix Kunde <[email protected]>
1 parent ca0c27a commit 695ad44

File tree

12 files changed

+78
-0
lines changed

12 files changed

+78
-0
lines changed

charts/postgres-operator/crds/operatorconfigurations.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ spec:
450450
type: string
451451
logical_backup_s3_sse:
452452
type: string
453+
logical_backup_s3_retention_time:
454+
type: string
453455
logical_backup_schedule:
454456
type: string
455457
pattern: '^(\d+|\*)(/\d+)?(\s+(\d+|\*)(/\d+)?){4}$'

charts/postgres-operator/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ configLogicalBackup:
310310
logical_backup_s3_secret_access_key: ""
311311
# S3 server side encryption
312312
logical_backup_s3_sse: "AES256"
313+
# S3 retention time for stored backups for example "2 week" or "7 days"
314+
logical_backup_s3_retention_time: ""
313315
# backup schedule in the cron format
314316
logical_backup_schedule: "30 00 * * *"
315317

docker/logical-backup/dump.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
1515
K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1
1616
CERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
1717

18+
LOGICAL_BACKUP_PROVIDER=${LOGICAL_BACKUP_PROVIDER:="s3"}
19+
LOGICAL_BACKUP_S3_RETENTION_TIME=${LOGICAL_BACKUP_S3_RETENTION_TIME:=""}
20+
1821
function estimate_size {
1922
"$PG_BIN"/psql -tqAc "${ALL_DB_SIZE_QUERY}"
2023
}
@@ -28,6 +31,57 @@ function compress {
2831
pigz
2932
}
3033

34+
function aws_delete_objects {
35+
args=(
36+
"--bucket=$LOGICAL_BACKUP_S3_BUCKET"
37+
)
38+
39+
[[ ! -z "$LOGICAL_BACKUP_S3_ENDPOINT" ]] && args+=("--endpoint-url=$LOGICAL_BACKUP_S3_ENDPOINT")
40+
[[ ! -z "$LOGICAL_BACKUP_S3_REGION" ]] && args+=("--region=$LOGICAL_BACKUP_S3_REGION")
41+
42+
aws s3api delete-objects "${args[@]}" --delete Objects=["$(printf {Key=%q}, "$@")"],Quiet=true
43+
}
44+
export -f aws_delete_objects
45+
46+
function aws_delete_outdated {
47+
if [[ -z "$LOGICAL_BACKUP_S3_RETENTION_TIME" ]] ; then
48+
echo "no retention time configured: skip cleanup of outdated backups"
49+
return 0
50+
fi
51+
52+
# define cutoff date for outdated backups (day precision)
53+
cutoff_date=$(date -d "$LOGICAL_BACKUP_S3_RETENTION_TIME ago" +%F)
54+
55+
# mimic bucket setup from Spilo
56+
prefix="spilo/"$SCOPE$LOGICAL_BACKUP_S3_BUCKET_SCOPE_SUFFIX"/logical_backups/"
57+
58+
args=(
59+
"--no-paginate"
60+
"--output=text"
61+
"--prefix=$prefix"
62+
"--bucket=$LOGICAL_BACKUP_S3_BUCKET"
63+
)
64+
65+
[[ ! -z "$LOGICAL_BACKUP_S3_ENDPOINT" ]] && args+=("--endpoint-url=$LOGICAL_BACKUP_S3_ENDPOINT")
66+
[[ ! -z "$LOGICAL_BACKUP_S3_REGION" ]] && args+=("--region=$LOGICAL_BACKUP_S3_REGION")
67+
68+
# list objects older than the cutoff date
69+
aws s3api list-objects "${args[@]}" --query="Contents[?LastModified<='$cutoff_date'].[Key]" > /tmp/outdated-backups
70+
71+
# spare the last backup
72+
sed -i '$d' /tmp/outdated-backups
73+
74+
count=$(wc -l < /tmp/outdated-backups)
75+
if [[ $count == 0 ]] ; then
76+
echo "no outdated backups to delete"
77+
return 0
78+
fi
79+
echo "deleting $count outdated backups created before $cutoff_date"
80+
81+
# deleted outdated files in batches with 100 at a time
82+
tr '\n' '\0' < /tmp/outdated-backups | xargs -0 -P1 -n100 bash -c 'aws_delete_objects "$@"' _
83+
}
84+
3185
function aws_upload {
3286
declare -r EXPECTED_SIZE="$1"
3387

@@ -59,6 +113,7 @@ function upload {
59113
;;
60114
*)
61115
aws_upload $(($(estimate_size) / DUMP_SIZE_COEFF))
116+
aws_delete_outdated
62117
;;
63118
esac
64119
}

docs/reference/operator_parameters.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,11 @@ grouped under the `logical_backup` key.
676676
Specify server side encryption that S3 storage is using. If empty string
677677
is specified, no argument will be passed to `aws s3` command. Default: "AES256".
678678

679+
* **logical_backup_s3_retention_time**
680+
Specify a retention time for logical backups stored in S3. Backups older than the specified retention
681+
time will be deleted after a new backup was uploaded. If empty, all backups will be kept. Example values are
682+
"3 days", "2 weeks", or "1 month". The default is empty.
683+
679684
* **logical_backup_schedule**
680685
Backup schedule in the cron format. Please take the
681686
[reference schedule format](https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#schedule)

manifests/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ data:
7777
# logical_backup_s3_endpoint: ""
7878
# logical_backup_s3_secret_access_key: ""
7979
logical_backup_s3_sse: "AES256"
80+
# logical_backup_s3_retention_time: ""
8081
logical_backup_schedule: "30 00 * * *"
8182
major_version_upgrade_mode: "manual"
8283
# major_version_upgrade_team_allow_list: ""

manifests/operatorconfiguration.crd.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ spec:
448448
type: string
449449
logical_backup_s3_sse:
450450
type: string
451+
logical_backup_s3_retention_time:
452+
type: string
451453
logical_backup_schedule:
452454
type: string
453455
pattern: '^(\d+|\*)(/\d+)?(\s+(\d+|\*)(/\d+)?){4}$'

manifests/postgresql-operator-default-configuration.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ configuration:
146146
# logical_backup_s3_region: ""
147147
# logical_backup_s3_secret_access_key: ""
148148
logical_backup_s3_sse: "AES256"
149+
# logical_backup_s3_retention_time: ""
149150
logical_backup_schedule: "30 00 * * *"
150151
debug:
151152
debug_logging: true

pkg/apis/acid.zalan.do/v1/crds.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,9 @@ var OperatorConfigCRDResourceValidation = apiextv1.CustomResourceValidation{
15561556
"logical_backup_s3_sse": {
15571557
Type: "string",
15581558
},
1559+
"logical_backup_s3_retention_time": {
1560+
Type: "string",
1561+
},
15591562
"logical_backup_schedule": {
15601563
Type: "string",
15611564
Pattern: "^(\\d+|\\*)(/\\d+)?(\\s+(\\d+|\\*)(/\\d+)?){4}$",

pkg/apis/acid.zalan.do/v1/operator_configuration_type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ type OperatorLogicalBackupConfiguration struct {
213213
S3AccessKeyID string `json:"logical_backup_s3_access_key_id,omitempty"`
214214
S3SecretAccessKey string `json:"logical_backup_s3_secret_access_key,omitempty"`
215215
S3SSE string `json:"logical_backup_s3_sse,omitempty"`
216+
RetentionTime string `json:"logical_backup_s3_retention_time,omitempty"`
216217
GoogleApplicationCredentials string `json:"logical_backup_google_application_credentials,omitempty"`
217218
JobPrefix string `json:"logical_backup_job_prefix,omitempty"`
218219
}

pkg/cluster/k8sres.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,10 @@ func (c *Cluster) generateLogicalBackupPodEnvVars() []v1.EnvVar {
21342134
Name: "LOGICAL_BACKUP_S3_SSE",
21352135
Value: c.OpConfig.LogicalBackup.LogicalBackupS3SSE,
21362136
},
2137+
{
2138+
Name: "LOGICAL_BACKUP_S3_RETENTION_TIME",
2139+
Value: c.OpConfig.LogicalBackup.LogicalBackupS3RetentionTime,
2140+
},
21372141
{
21382142
Name: "LOGICAL_BACKUP_S3_BUCKET_SCOPE_SUFFIX",
21392143
Value: getBucketScopeSuffix(string(c.Postgresql.GetUID())),

0 commit comments

Comments
 (0)