Skip to content

Commit cf87776

Browse files
authored
Merge pull request #197 from acekingke/backupS3
*: backup to S3 and restore from S3 #116
2 parents acaf8f0 + bd4fbd8 commit cf87776

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2031
-27
lines changed

.github/workflows/auto_build_operator.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
- controllers/*
1111
- internal/*
1212
- utils/*
13+
- backup/*
1314
workflow_dispatch:
1415

1516
jobs:

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ COPY cmd/manager/main.go cmd/manager/main.go
1515
COPY api/ api/
1616
COPY cluster/ cluster/
1717
COPY controllers/ controllers/
18+
COPY backup/ backup/
1819
COPY internal/ internal/
1920
COPY utils/ utils/
2021

Dockerfile.sidecar

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ WORKDIR /workspace
88
# Copy the Go Modules manifests
99
COPY go.mod go.mod
1010
COPY go.sum go.sum
11+
1112
# cache deps before building and copying source so that we don't need to re-download as much
1213
# and so that source changes don't invalidate our downloaded layer
1314
RUN go env -w GOPROXY=https://goproxy.cn,direct; \
@@ -24,12 +25,28 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o bin/sidecar cmd/sidecar
2425
###############################################################################
2526
# Docker image for Sidecar
2627
###############################################################################
27-
FROM alpine:3.13
28+
FROM ubuntu:focal
29+
30+
RUN set -ex; \
31+
groupadd --gid 1001 --system mysql; \
32+
useradd \
33+
--uid 1001 \
34+
--system \
35+
--home-dir /var/lib/mysql \
36+
--no-create-home \
37+
--gid mysql \
38+
mysql;
2839

29-
# Create a group and user
30-
RUN addgroup -g 1001 mysql && adduser -u 1001 -g 1001 -S mysql
40+
ARG XTRABACKUP_PKG=percona-xtrabackup-24
41+
RUN set -ex; \
42+
apt-get update; \
43+
apt-get install -y --no-install-recommends gnupg2 wget lsb-release curl; \
44+
wget -P /tmp --no-check-certificate https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb; \
45+
dpkg -i /tmp/percona-release_latest.$(lsb_release -sc)_all.deb; \
46+
apt-get update; \
47+
apt-get install -y --no-install-recommends ${XTRABACKUP_PKG}; \
48+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
3149

3250
WORKDIR /
3351
COPY --from=builder /workspace/bin/sidecar /usr/local/bin/sidecar
34-
3552
ENTRYPOINT ["sidecar"]

PROJECT

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ resources:
1818
group: mysql
1919
kind: Status
2020
version: v1alpha1
21+
- api:
22+
crdVersion: v1
23+
namespaced: true
24+
controller: true
25+
domain: radondb.com
26+
group: mysql
27+
kind: Backup
28+
path: github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1
29+
version: v1alpha1
2130
version: "3"

api/v1alpha1/backup_types.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright 2021 RadonDB.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
corev1 "k8s.io/api/core/v1"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
25+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
26+
27+
// BackupSpec defines the desired state of Backup
28+
type BackupSpec struct {
29+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
30+
// Important: Run "make" to regenerate code after modifying this file
31+
32+
// To specify the image that will be used for sidecar container.
33+
// +optional
34+
// +kubebuilder:default:="radondb/mysql-sidecar:0.1.88"
35+
Image string `json:"image"`
36+
37+
// HostName represents the host for which to take backup
38+
HostName string `json:"hostname"`
39+
40+
// Cluster represents the cluster name to backup
41+
ClusterName string `json:"clustname"`
42+
43+
// History Limit of job
44+
// +optional
45+
// +kubebuilder:default:=3
46+
HistoryLimit *int32 `json:"historyLimit,omitempty"`
47+
}
48+
49+
// BackupStatus defines the observed state of Backup
50+
type BackupStatus struct {
51+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
52+
// Important: Run "make" to regenerate code after modifying this file
53+
54+
// Completed indicates whether the backup is in a final state,
55+
// no matter whether its' corresponding job failed or succeeded
56+
Completed bool `json:"completed,omitempty"`
57+
58+
// Conditions represents the backup resource conditions list.
59+
Conditions []BackupCondition `json:"conditions,omitempty"`
60+
}
61+
62+
//+kubebuilder:object:root=true
63+
//+kubebuilder:subresource:status
64+
65+
// Backup is the Schema for the backups API
66+
type Backup struct {
67+
metav1.TypeMeta `json:",inline"`
68+
metav1.ObjectMeta `json:"metadata,omitempty"`
69+
70+
Spec BackupSpec `json:"spec,omitempty"`
71+
Status BackupStatus `json:"status,omitempty"`
72+
}
73+
74+
// BackupCondition defines condition struct for backup resource
75+
type BackupCondition struct {
76+
// type of cluster condition, values in (\"Ready\")
77+
Type BackupConditionType `json:"type"`
78+
// Status of the condition, one of (\"True\", \"False\", \"Unknown\")
79+
Status corev1.ConditionStatus `json:"status"`
80+
// LastTransitionTime
81+
LastTransitionTime metav1.Time `json:"lastTransitionTime"`
82+
// Reason
83+
Reason string `json:"reason"`
84+
// Message
85+
Message string `json:"message"`
86+
}
87+
88+
// BackupConditionType defines condition types of a backup resources
89+
type BackupConditionType string
90+
91+
const (
92+
// BackupComplete means the backup has finished his execution
93+
BackupComplete BackupConditionType = "Complete"
94+
// BackupFailed means backup has failed
95+
BackupFailed BackupConditionType = "Failed"
96+
)
97+
98+
//+kubebuilder:object:root=true
99+
100+
// BackupList contains a list of Backup
101+
type BackupList struct {
102+
metav1.TypeMeta `json:",inline"`
103+
metav1.ListMeta `json:"metadata,omitempty"`
104+
Items []Backup `json:"items"`
105+
}
106+
107+
func init() {
108+
SchemeBuilder.Register(&Backup{}, &BackupList{})
109+
}

api/v1alpha1/cluster_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ type ClusterSpec struct {
7272
// +optional
7373
// +kubebuilder:default:={enabled: true, accessModes: {"ReadWriteOnce"}, size: "10Gi"}
7474
Persistence Persistence `json:"persistence,omitempty"`
75+
76+
// Represents the name of the secret that contains credentials to connect to
77+
// the storage provider to store backups.
78+
// +optional
79+
BackupSecretName string `json:"backupSecretName,omitempty"`
80+
81+
// Represents the name of the cluster restore from backup path
82+
// +optional
83+
RestoreFrom string `json:"restoreFrom,omitempty"`
7584
}
7685

7786
// MysqlOpts defines the options of MySQL container.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backup/backup.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2021 RadonDB.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package backup
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/radondb/radondb-mysql-kubernetes/utils"
23+
logf "sigs.k8s.io/controller-runtime/pkg/log"
24+
25+
v1alhpa1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
26+
)
27+
28+
var log = logf.Log.WithName("backup")
29+
30+
// Backup is a type wrapper over Backup that contains the Business logic
31+
type Backup struct {
32+
*v1alhpa1.Backup
33+
}
34+
35+
// New returns a wraper object over Backup
36+
func New(backup *v1alhpa1.Backup) *Backup {
37+
return &Backup{
38+
Backup: backup,
39+
}
40+
}
41+
42+
// Unwrap returns the api backup object
43+
func (b *Backup) Unwrap() *v1alhpa1.Backup {
44+
return b.Backup
45+
}
46+
47+
// GetNameForJob returns the name of the job
48+
func (b *Backup) GetNameForJob() string {
49+
return fmt.Sprintf("%s-backup", b.Name)
50+
}
51+
52+
// Create the backup Domain Name.
53+
func (b *Backup) GetBackupURL(cluster_name string, hostname string) string {
54+
return fmt.Sprintf("%s.%s-mysql.%s:%v", hostname, cluster_name, b.Namespace, utils.XBackupPort)
55+
}

0 commit comments

Comments
 (0)