Skip to content

Commit 679e3ae

Browse files
authored
K8SPS-96: compressed backups improvements (#1294)
1 parent 6111cfd commit 679e3ae

23 files changed

Lines changed: 199 additions & 25 deletions

api/v1/perconaservermysqlbackup_types.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,36 @@ func (b *PerconaServerMySQLBackup) GetContainerOptions(storage *BackupStorageSpe
215215
return nil
216216
}
217217

218-
// IsCompressed reports whether the xtrabackup args contain --compress.
219-
func (b *PerconaServerMySQLBackup) IsCompressed(storage *BackupStorageSpec) bool {
218+
// IsCompressed reports whether compression is enabled via xtrabackup args or
219+
// via the [xtrabackup] section of the MySQL configuration.
220+
func (b *PerconaServerMySQLBackup) IsCompressed(storage *BackupStorageSpec, mysqlConfiguration string) bool {
220221
opts := b.GetContainerOptions(storage)
221-
if opts == nil {
222-
return false
222+
if opts != nil {
223+
for _, arg := range opts.Args.Xtrabackup {
224+
if arg == "--compress" || strings.HasPrefix(arg, "--compress=") {
225+
return true
226+
}
227+
}
223228
}
224-
for _, arg := range opts.Args.Xtrabackup {
225-
if arg == "--compress" || strings.HasPrefix(arg, "--compress=") {
229+
return isCompressedInMySQLConfig(mysqlConfiguration)
230+
}
231+
232+
func isCompressedInMySQLConfig(configuration string) bool {
233+
inSection := false
234+
for _, line := range strings.Split(configuration, "\n") {
235+
line = strings.TrimSpace(line)
236+
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
237+
continue
238+
}
239+
if strings.HasPrefix(line, "[") {
240+
inSection = strings.TrimSpace(strings.Trim(line, "[]")) == "xtrabackup"
241+
continue
242+
}
243+
if !inSection {
244+
continue
245+
}
246+
parts := strings.SplitN(line, "=", 2)
247+
if strings.TrimSpace(parts[0]) == "compress" && len(parts) == 2 && strings.TrimSpace(parts[1]) != "" {
226248
return true
227249
}
228250
}

api/v1/perconaservermysqlbackup_types_test.go

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ func TestPerconaServerMySQLBackup_IsCompressed(t *testing.T) {
463463
tests := map[string]struct {
464464
backup *PerconaServerMySQLBackup
465465
storage *BackupStorageSpec
466+
cluster *PerconaServerMySQL
466467
expected bool
467468
}{
468469
"compress flag in backup spec args": {
@@ -475,6 +476,7 @@ func TestPerconaServerMySQLBackup_IsCompressed(t *testing.T) {
475476
},
476477
},
477478
},
479+
cluster: &PerconaServerMySQL{},
478480
expected: true,
479481
},
480482
"compress flag with algorithm value": {
@@ -487,6 +489,7 @@ func TestPerconaServerMySQLBackup_IsCompressed(t *testing.T) {
487489
},
488490
},
489491
},
492+
cluster: &PerconaServerMySQL{},
490493
expected: true,
491494
},
492495
"compress flag in storage args": {
@@ -502,6 +505,7 @@ func TestPerconaServerMySQLBackup_IsCompressed(t *testing.T) {
502505
},
503506
},
504507
},
508+
cluster: &PerconaServerMySQL{},
505509
expected: true,
506510
},
507511
"spec args take precedence, no compress there": {
@@ -521,6 +525,7 @@ func TestPerconaServerMySQLBackup_IsCompressed(t *testing.T) {
521525
},
522526
},
523527
},
528+
cluster: &PerconaServerMySQL{},
524529
expected: false,
525530
},
526531
"compress-threads alone does not count": {
@@ -533,21 +538,115 @@ func TestPerconaServerMySQLBackup_IsCompressed(t *testing.T) {
533538
},
534539
},
535540
},
541+
cluster: &PerconaServerMySQL{},
536542
expected: false,
537543
},
538544
"no container options": {
539545
backup: &PerconaServerMySQLBackup{
540546
Spec: PerconaServerMySQLBackupSpec{},
541547
},
542548
storage: nil,
549+
cluster: &PerconaServerMySQL{},
543550
expected: false,
544551
},
552+
"compress in mysql configuration xtrabackup section": {
553+
backup: &PerconaServerMySQLBackup{
554+
Spec: PerconaServerMySQLBackupSpec{},
555+
},
556+
cluster: &PerconaServerMySQL{
557+
Spec: PerconaServerMySQLSpec{
558+
MySQL: MySQLSpec{
559+
PodSpec: PodSpec{Configuration: "[mysqld]\nsome=val\n[xtrabackup]\ncompress=zstd\n"},
560+
},
561+
},
562+
},
563+
expected: true,
564+
},
565+
"compress in mysql configuration xtrabackup section with spaces": {
566+
backup: &PerconaServerMySQLBackup{
567+
Spec: PerconaServerMySQLBackupSpec{},
568+
},
569+
cluster: &PerconaServerMySQL{
570+
Spec: PerconaServerMySQLSpec{
571+
MySQL: MySQLSpec{
572+
PodSpec: PodSpec{Configuration: "[xtrabackup]\ncompress = lz4\n"},
573+
},
574+
},
575+
},
576+
expected: true,
577+
},
578+
"compress in mysql configuration with spaces inside section brackets": {
579+
backup: &PerconaServerMySQLBackup{
580+
Spec: PerconaServerMySQLBackupSpec{},
581+
},
582+
cluster: &PerconaServerMySQL{
583+
Spec: PerconaServerMySQLSpec{
584+
MySQL: MySQLSpec{
585+
PodSpec: PodSpec{Configuration: "[ xtrabackup ]\ncompress=zstd\n"},
586+
},
587+
},
588+
},
589+
expected: true,
590+
},
591+
"compress key without value in mysql configuration": {
592+
backup: &PerconaServerMySQLBackup{
593+
Spec: PerconaServerMySQLBackupSpec{},
594+
},
595+
cluster: &PerconaServerMySQL{
596+
Spec: PerconaServerMySQLSpec{
597+
MySQL: MySQLSpec{
598+
PodSpec: PodSpec{Configuration: "[xtrabackup]\ncompress=\n"},
599+
},
600+
},
601+
},
602+
expected: false,
603+
},
604+
"compress-threads alone in mysql configuration does not count": {
605+
backup: &PerconaServerMySQLBackup{
606+
Spec: PerconaServerMySQLBackupSpec{},
607+
},
608+
cluster: &PerconaServerMySQL{
609+
Spec: PerconaServerMySQLSpec{
610+
MySQL: MySQLSpec{
611+
PodSpec: PodSpec{Configuration: "[xtrabackup]\ncompress-threads=4\n"},
612+
},
613+
},
614+
},
615+
expected: false,
616+
},
617+
"compress in wrong section of mysql configuration": {
618+
backup: &PerconaServerMySQLBackup{
619+
Spec: PerconaServerMySQLBackupSpec{},
620+
},
621+
cluster: &PerconaServerMySQL{
622+
Spec: PerconaServerMySQLSpec{
623+
MySQL: MySQLSpec{
624+
PodSpec: PodSpec{Configuration: "[mysqld]\ncompress=zstd\n"},
625+
},
626+
},
627+
},
628+
expected: false,
629+
},
630+
"args take precedence over mysql configuration": {
631+
backup: &PerconaServerMySQLBackup{
632+
Spec: PerconaServerMySQLBackupSpec{
633+
ContainerOptions: &BackupContainerOptions{
634+
Args: BackupContainerArgs{
635+
Xtrabackup: []string{"--compress"},
636+
},
637+
},
638+
},
639+
},
640+
cluster: &PerconaServerMySQL{},
641+
expected: true,
642+
},
545643
}
546644

547645
for name, tt := range tests {
548646
t.Run(name, func(t *testing.T) {
549-
result := tt.backup.IsCompressed(tt.storage)
550-
assert.Equal(t, tt.expected, result)
647+
var status PerconaServerMySQLBackupStatus
648+
status.Compressed = tt.backup.IsCompressed(tt.storage, tt.cluster.Spec.MySQL.Configuration)
649+
assert.Equal(t, tt.expected, status.Compressed)
551650
})
552651
}
553652
}

cmd/example-gen/pkg/defaults/manual.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,12 @@ func backupDefaults(spec *apiv1.BackupSpec) {
128128
{
129129
Name: "sat-night-backup",
130130
Schedule: "0 0 * * 6",
131-
Keep: 3,
132131
StorageName: "s3-us-west",
133132
Type: apiv1.BackupTypeFull,
134133
},
135134
{
136135
Name: "daily-backup",
137136
Schedule: "0 0 * * *",
138-
Keep: 5,
139137
StorageName: "s3",
140138
Type: apiv1.BackupTypeIncremental,
141139
},

cmd/sidecar/handler/backup/create.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ func (h *Handler) createBackupHandler(w http.ResponseWriter, req *http.Request)
199199
log.Info("Backup finished successfully", "destination", backupConf.Destination, "storage", backupConf.Type)
200200
}
201201

202+
const customMyCnfPath = "/etc/mysql/config/my-config.cnf"
203+
202204
func xtrabackupArgs(user, pass string, conf *xb.BackupConfig) []string {
203205
args := []string{
204206
"--backup",
@@ -210,6 +212,9 @@ func xtrabackupArgs(user, pass string, conf *xb.BackupConfig) []string {
210212
fmt.Sprintf("--user=%s", user),
211213
fmt.Sprintf("--password=%s", pass),
212214
}
215+
if _, err := os.Stat(customMyCnfPath); err == nil {
216+
args = append([]string{"--defaults-extra-file=" + customMyCnfPath}, args...)
217+
}
213218
if conf != nil && conf.ContainerOptions != nil {
214219
args = append(args, conf.ContainerOptions.Args.Xtrabackup...)
215220
}

deploy/cr.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,12 +675,12 @@ spec:
675675
# - name: my-secret-1
676676
# - name: my-secret-2
677677
# schedule:
678-
# - keep: 3
678+
# - keep: 0
679679
# name: sat-night-backup
680680
# schedule: 0 0 * * 6
681681
# storageName: s3-us-west
682682
# type: full
683-
# - keep: 5
683+
# - keep: 0
684684
# name: daily-backup
685685
# schedule: 0 0 * * *
686686
# storageName: s3

e2e-tests/tests/auto-config/01-assert.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ spec:
3939
name: backup-logs
4040
- mountPath: /etc/mysql/vault-keyring-secret
4141
name: vault-keyring-secret
42+
- mountPath: /etc/mysql/config
43+
name: config
4244
- name: pt-heartbeat
4345
volumeMounts:
4446
- mountPath: /opt/percona

e2e-tests/tests/config-router/01-assert.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ spec:
3939
name: backup-logs
4040
- mountPath: /etc/mysql/vault-keyring-secret
4141
name: vault-keyring-secret
42+
- mountPath: /etc/mysql/config
43+
name: config
4244
volumes:
4345
- emptyDir: {}
4446
name: bin

e2e-tests/tests/config/01-assert.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ spec:
3939
name: backup-logs
4040
- mountPath: /etc/mysql/vault-keyring-secret
4141
name: vault-keyring-secret
42+
- mountPath: /etc/mysql/config
43+
name: config
4244
- name: pt-heartbeat
4345
volumeMounts:
4446
- mountPath: /opt/percona

e2e-tests/tests/demand-backup-incremental/09-create-incremental-backup.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ spec:
1111
containerOptions:
1212
args:
1313
xtrabackup:
14-
- "--compress=lz4"
14+
- "--compress"

e2e-tests/tests/demand-backup-incremental/28-create-compressed-full-backup.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ spec:
1010
containerOptions:
1111
args:
1212
xtrabackup:
13-
- "--compress=lz4"
13+
- "--compress"

0 commit comments

Comments
 (0)