Skip to content

Commit 21e0621

Browse files
authored
Merge pull request #232 from acekingke/backupForNFS
*: support backup/restore with NFS #148
2 parents 3159e89 + 8b9b76e commit 21e0621

28 files changed

+407
-31
lines changed

api/v1alpha1/backup_types.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ type BackupSpec struct {
3535
Image string `json:"image"`
3636

3737
// HostName represents the host for which to take backup
38-
HostName string `json:"hostname"`
38+
// If is empty, is use leader HostName
39+
HostName string `json:"hostname,omitempty"`
40+
41+
// Represents the name of backup to NFS
42+
// +optional
43+
BackupToNFS string `json:"BackupToNFS,omitempty"`
3944

4045
// ClusterName represents the cluster name to backup
4146
ClusterName string `json:"clustname"`

api/v1alpha1/mysqlcluster_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ type MysqlClusterSpec struct {
8181
// Represents the name of the cluster restore from backup path
8282
// +optional
8383
RestoreFrom string `json:"restoreFrom,omitempty"`
84+
85+
// Represents NFS ip address where cluster restore from.
86+
// +optional
87+
RestoreFromNFS string `json:"restoreFromNFS,omitempty"`
8488
}
8589

8690
// MysqlOpts defines the options of MySQL container.

backup/backup.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ func (b *Backup) GetNameForJob() string {
4949
return fmt.Sprintf("%s-backup", b.Name)
5050
}
5151

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)
52+
// Create the backup Domain Name or leader DNS.
53+
func (b *Backup) GetBackupURL(clusterName string, hostName string) string {
54+
if len(hostName) != 0 {
55+
return fmt.Sprintf("%s.%s-mysql.%s:%v", hostName, clusterName, b.Namespace, utils.XBackupPort)
56+
} else {
57+
return fmt.Sprintf("%s-leader.%s:%v", clusterName, b.Namespace, utils.XBackupPort)
58+
}
5559
}

backup/syncer/job.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,43 @@ func (s *jobSyncer) ensurePodSpec(in corev1.PodSpec) corev1.PodSpec {
116116
sctName := fmt.Sprintf("%s-secret", s.backup.Spec.ClusterName)
117117
in.Containers[0].Name = utils.ContainerBackupName
118118
in.Containers[0].Image = fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), s.backup.Spec.Image)
119-
in.Containers[0].Args = []string{
120-
"request_a_backup",
121-
s.backup.GetBackupURL(s.backup.Spec.ClusterName, s.backup.Spec.HostName),
119+
120+
if len(s.backup.Spec.BackupToNFS) != 0 {
121+
// add volumn about pvc
122+
in.Volumes = []corev1.Volume{
123+
{
124+
Name: utils.XtrabackupPV,
125+
VolumeSource: corev1.VolumeSource{
126+
NFS: &corev1.NFSVolumeSource{
127+
Server: s.backup.Spec.BackupToNFS,
128+
Path: "/",
129+
},
130+
},
131+
},
132+
}
133+
//"rm -rf /backup/*;curl --user sys_backups:sys_backups sample-mysql-0.sample-mysql.default:8082/download|xbstream -x -C /backup"
134+
in.Containers[0].Command = []string{
135+
"/bin/bash", "-c", "--",
136+
}
137+
var backupToDir string = utils.BuildBackupName(s.backup.Spec.ClusterName)
138+
in.Containers[0].Args = []string{
139+
fmt.Sprintf("mkdir -p /backup/%s;"+
140+
"curl --user $BACKUP_USER:$BACKUP_PASSWORD %s/download|xbstream -x -C /backup/%s; exit ${PIPESTATUS[0]}",
141+
backupToDir,
142+
s.backup.GetBackupURL(s.backup.Spec.ClusterName, s.backup.Spec.HostName), backupToDir),
143+
}
144+
in.Containers[0].VolumeMounts = []corev1.VolumeMount{
145+
{
146+
Name: utils.XtrabackupPV,
147+
MountPath: utils.XtrabckupLocal,
148+
},
149+
}
150+
} else {
151+
// in.Containers[0].ImagePullPolicy = s.opt.ImagePullPolicy
152+
in.Containers[0].Args = []string{
153+
"request_a_backup",
154+
s.backup.GetBackupURL(s.backup.Spec.ClusterName, s.backup.Spec.HostName),
155+
}
122156
}
123157
var optTrue bool = true
124158
in.Containers[0].Env = []corev1.EnvVar{

charts/mysql-operator/crds/mysql.radondb.com_backups.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ spec:
3636
spec:
3737
description: BackupSpec defines the desired state of Backup
3838
properties:
39+
BackupToNFS:
40+
description: Represents the name of backup to NFS
41+
type: string
3942
clustname:
4043
description: ClusterName represents the cluster name to backup
4144
type: string
@@ -46,14 +49,14 @@ spec:
4649
type: integer
4750
hostname:
4851
description: HostName represents the host for which to take backup
52+
If is empty, is use leader HostName
4953
type: string
5054
image:
5155
default: radondb/mysql57-sidecar:v2.2.0
5256
description: To specify the image that will be used for sidecar container.
5357
type: string
5458
required:
5559
- clustname
56-
- hostname
5760
type: object
5861
status:
5962
description: BackupStatus defines the observed state of Backup

charts/mysql-operator/crds/mysql.radondb.com_mysqlclusters.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,9 @@ spec:
12451245
description: Represents the name of the cluster restore from backup
12461246
path
12471247
type: string
1248+
restoreFromNFS:
1249+
description: Represents NFS ip address where cluster restore from.
1250+
type: string
12481251
xenonOpts:
12491252
default:
12501253
admitDefeatHearbeatCount: 5

config/crd/bases/mysql.radondb.com_backups.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ spec:
3636
spec:
3737
description: BackupSpec defines the desired state of Backup
3838
properties:
39+
BackupToNFS:
40+
description: Represents the name of backup to NFS
41+
type: string
3942
clustname:
4043
description: ClusterName represents the cluster name to backup
4144
type: string
@@ -46,14 +49,14 @@ spec:
4649
type: integer
4750
hostname:
4851
description: HostName represents the host for which to take backup
52+
If is empty, is use leader HostName
4953
type: string
5054
image:
5155
default: radondb/mysql57-sidecar:v2.2.0
5256
description: To specify the image that will be used for sidecar container.
5357
type: string
5458
required:
5559
- clustname
56-
- hostname
5760
type: object
5861
status:
5962
description: BackupStatus defines the observed state of Backup

config/crd/bases/mysql.radondb.com_mysqlclusters.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,9 @@ spec:
12451245
description: Represents the name of the cluster restore from backup
12461246
path
12471247
type: string
1248+
restoreFromNFS:
1249+
description: Represents NFS ip address where cluster restore from.
1250+
type: string
12481251
xenonOpts:
12491252
default:
12501253
admitDefeatHearbeatCount: 5

config/samples/backup_secret.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
kind: Secret
2-
apiVersion: v1
2+
apiVersion: mysql.radondb.com/v1alpha1
33
metadata:
44
name: sample-backup-secret
55
namespace: default

config/samples/mysql_v1alpha1_backup.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ metadata:
55
spec:
66
# Add fields here
77
image: radondb/mysql-sidecar:latest
8+
# hostname if empty, use the leader as hostname
89
hostname: sample-mysql-0
910
clustname: sample
11+
# BackupToNFS: "IP of NFS server"

0 commit comments

Comments
 (0)