Skip to content

Commit

Permalink
*: pitr backup and recovery radondb#400
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed Mar 22, 2022
1 parent f2d534c commit f983774
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Dockerfile.sidecar
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ RUN set -ex; \
dpkg -i /tmp/percona-release_latest.$(lsb_release -sc)_all.deb; \
apt-get update; \
apt-get install -y --no-install-recommends ${XTRABACKUP_PKG}; \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* ;\
wget https://dl.min.io/client/mc/release/linux-amd64/mc --no-check-certificate ; \
cp ./mc /usr/local/bin/mc; \
chmod +x /usr/local/bin/mc;

WORKDIR /
COPY --from=builder /workspace/bin/sidecar /usr/local/bin/sidecar
Expand Down
2 changes: 1 addition & 1 deletion cmd/sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func main() {
Use: "http",
Short: "start http server",
Run: func(cmd *cobra.Command, args []string) {
if err := sidecar.RunHttpServer(backupCfg, stop); err != nil {
if err := sidecar.RunHttpServerAndPitr(backupCfg, stop); err != nil {
log.Error(err, "run command failed")
os.Exit(1)
}
Expand Down
4 changes: 4 additions & 0 deletions mysqlcluster/container/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,9 @@ func (c *backupSidecar) getVolumeMounts() []corev1.VolumeMount {
Name: utils.SysLocalTimeZone,
MountPath: utils.SysLocalTimeZoneMountPath,
},
{
Name: utils.PodInfoVolumeName,
MountPath: utils.PodInfoVolumeMountPath,
},
}
}
15 changes: 15 additions & 0 deletions mysqlcluster/mysqlcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ func (c *MysqlCluster) EnsureVolumes() []corev1.Volume {
},
},
},
corev1.Volume{
Name: utils.PodInfoVolumeName,
VolumeSource: corev1.VolumeSource{
DownwardAPI: &corev1.DownwardAPIVolumeSource{
Items: []corev1.DownwardAPIVolumeFile{
{
Path: "labels",
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.labels",
},
},
},
},
},
},
)

return volumes
Expand Down
62 changes: 61 additions & 1 deletion sidecar/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package sidecar

import (
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -25,6 +26,7 @@ import (
"os/user"
"path"
"strconv"
"time"

"github.com/radondb/radondb-mysql-kubernetes/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -231,8 +233,66 @@ func runInitCommand(cfg *Config) error {
return nil
}

func RunPitr(cfg *Config) {
if len(cfg.XCloudS3AccessKey) == 0 || len(cfg.XCloudS3SecretKey) == 0 ||
len(cfg.XCloudS3Bucket) == 0 || len(cfg.XCloudS3EndPoint) == 0 {
log.Error(errors.New("s3: S3 not set"), "Do not set the S3 enviroment!")
return
} else {
setalias := `mc alias set S3 $S3_ENDPOINT $S3_ACCESSKEY $S3_SECRETKEY`
// use bash run setalias
cmd := exec.Command("sh", "-c", setalias)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Error(err, "failed to set alias")
return
}

for {
<-time.After(20 * 1000 * time.Millisecond)
workstr := `
if grep -q pitr /etc/podinfo/labels; then
echo has pitr
else
echo do not has pitr lables
exit 0
fi
echo now do the something for binlog
cd /var/lib/mysql
for i in $(cat mysql-bin.index |cut -d'/' -f5)
do
ord=$(echo $i|cut -d'.' -f2)
md5=$(md5sum $i|cut -d' ' -f1)
obj=$(mc find S3/$S3_BUCKET -name binlog-*-$ord)
if [ -z $obj ] ;then
mc cp $i S3/$S3_BUCKET/"binlog-$md5-$ord"
else
# check md5 are not same
# echo $obj
objmd5=$(echo $obj |cut -d'/' -f3 |cut -d'-' -f2)
if [ $md5 != $objmd5 ]; then
echo "checking the $i md5 changed"
mc cp $i S3/$S3_BUCKET/"binlog-$md5-$ord"
mc rm $obj
fi;
fi
done`
cmd := exec.Command("sh", "-c", workstr)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Info("failed to run mc")
continue
}

}
}
}

// start the backup http server.
func RunHttpServer(cfg *Config, stop <-chan struct{}) error {
func RunHttpServerAndPitr(cfg *Config, stop <-chan struct{}) error {
go RunPitr(cfg)
srv := newServer(cfg, stop)
return srv.ListenAndServe()
}
Expand Down
17 changes: 17 additions & 0 deletions sidecar/pitr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2021 RadonDB.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package sidecar
2 changes: 2 additions & 0 deletions utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const (
ScriptsVolumeName = "scripts"
XenonVolumeName = "xenon"
InitFileVolumeName = "init-mysql"
PodInfoVolumeName = "podinfo"

// volumes mount path.
ConfVolumeMountPath = "/etc/mysql"
Expand All @@ -107,6 +108,7 @@ const (
ScriptsVolumeMountPath = "/scripts"
XenonVolumeMountPath = "/etc/xenon"
InitFileVolumeMountPath = "/docker-entrypoint-initdb.d"
PodInfoVolumeMountPath = "/etc/podinfo"

// Volume timezone name.
SysLocalTimeZone = "localtime"
Expand Down

0 comments on commit f983774

Please sign in to comment.