6 Star 53 Fork 13

迷茫的程序员/BackupSerenity

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Backup_XtraBackup_add.go 4.92 KB
一键复制 编辑 原始数据 按行查看 历史
xiaojun 提交于 2023-11-03 16:09 . wip 🕔 - "更新go版本封装代码"
package main
import (
"fmt"
"os/exec"
)
func main() {
script := `
#!/bin/bash
# Title:: Backup_XtraBackup_Add
# Author:: heike-07
# Description:: https://github.com/heike-07/Backup-tools.git
# Time:: 2022-04-12
# Update:: 2022-04-14
# Principle
# ALL -> ADD1 -> ADD2 ...
# ADD ? 1:BASE ALL -> ADD1 2:BASE ADD1 -> ADD2
# Call Backup_XtraBackup_add.conf
source conf/Backup_XtraBackup_add.conf
If_Docker(){
# Principle
# IFIST Is Docker
# df -h | grep -e overlay | awk '{print $1}'
# ll -a / | grep -e .dockerenv | awk '{print $9}'
DockerType=$(df -h | grep -e overlay | awk '{print $1}')
if [[ $DOCKER_TYPE == $DockerType ]] ;then
echo "检测到/文件类型为overlay,判断为Docker容器环境"
If_FOLDER
else
echo "没有检测到/文件类型为overlay,当前环境不是Docker容器环境"
XtraBackup_If_NFS
fi
}
If_FOLDER(){
# Principle
# IFIST FOLDER NFS_LINK_DISK
# ll -a / | grep -e NFS_LINK_DISK | awk '{print $9}'
Folder=$(ls -la / | grep -e NFS_LINK_DISK | awk '{print $9}')
if [[ $MYSQL_Nfs_DiskDir == $Folder ]] ;then
echo "发现文件夹 $Folder,正在继续执行脚本……"
XtraBackup_If
else
echo "没有发现文件夹 $MYSQL_Nfs_DiskDir,请检查环境是否创建文件夹/$MYSQL_Nfs_DiskDir"
fi
}
# Function
XtraBackup_Full(){
# Principle
# ALL
/usr/bin/innobackupex --defaults=$MYSQL_Default --no-timestamp --port=$MYSQL_Port --user=$MYSQL_Username --password=$MYSQL_Password $Data_Storage_Full
}
XtraBackup_If(){
# Principle
# IF FULL isit -> ADD
echo "正在判断是否有FULL指针,请稍后……"
if [[ ! -d $Data_Storage_Full ]] ;then
# 不存在为真
echo "没有找到全量备份FULL的指针,正在执行全量备份FULL"
mkdir -p $Data_Storage_Full
XtraBackup_Full
else
# 存在为假
echo "已经找到全量备份FULL的指针,正在执行增量判断,请稍后……"
XtraBackup_Add
fi
}
XtraBackup_Add(){
# Principle
# ADD
# ls -l | grep ZL | sort | awk 'NR==1 {print $9}' 获取ZL开头的最新的日期排序。
echo "正在判断是否有增量ADD的指针,请稍后……"
if [[ ! -d $Data_Storage_Add ]] ;then
mkdir -p $Data_Storage_Add
# innobackupex --defaults-file=/etc/mcnf --user=root --password='A16&@' --incremental --incremental-basedir=/backup/2021-2/ /backup/zenliang/
/usr/bin/innobackupex --defaults=$MYSQL_Default --port=$MYSQL_Port --user=$MYSQL_Username --password=$MYSQL_Password --incremental --incremental-basedir=$Data_Storage_Full $Data_Storage_Add
echo "首次增量备份已完成"
else
XtraBackup_Add_N
fi
}
XtraBackup_Add_N(){
# Principle
# ADD * N
# ls -l | grep -v total | wc -l
add_number=$(ls -l $Data_Storage_Add | grep -v total | wc -l)
if [[ $add_number -eq 0 ]] ;then
echo "不可能出现的情况"
else
echo "正在执行第N次增量备份……"
add_file=$(ls -l $Data_Storage_Add | sort -r | grep -v total | awk 'NR==1 {print $9}')
/usr/bin/innobackupex --defaults=$MYSQL_Default --port=$MYSQL_Port --user=$MYSQL_Username --password=$MYSQL_Password --incremental --incremental-basedir=$Data_Storage_Add/$add_file $Data_Storage_Add
echo "第N次增量备份已完成"
fi
}
XtraBackup_If_NFS(){
# Principle
# IFIST NFS
# df -h | grep NFS_LINK_DISK | awk '{print $6}'
use_nfs_disk=$(df -h | grep NFS_LINK_DISK | awk '{print $6}' | sed 's/\///g')
#echo use_nfs_disk $use_nfs_disk
#echo MYSQL_Nfs_DiskDir $MYSQL_Nfs_DiskDir
if [[ $MYSQL_Nfs_DiskDir == $use_nfs_disk ]] ;then
echo "发现NFS挂载点 $use_nfs_disk,正在继续执行脚本……"
XtraBackup_If
else
echo "脚本启动失败"
echo "没有发现NFS挂载点,请先检查NFS挂载点 是否正确挂载到 $MYSQL_Nfs_DiskDir"
fi
}
Mysql_binlog_If(){
# Principle
# 要满足2个条件:
# mysql配置中必须含有binlog:1. cat /etc/my.cnf | grep bin | grep -v '#' | wc -l >=1
# mysql配置中必须含有serverid:2. cat /etc/my.cnf | grep server-id | grep -v '#' | wc -l >=1
echo "正在进行MySQL-binlog判断……"
mysql_binlog_num=$(cat $MYSQL_Default | grep bin | grep -v '#' | wc -l)
mysql_serverid_num=$(cat $MYSQL_Default | grep server-id | grep -v '#' | wc -l)
if [[ mysql_binlog_num -ge 1 ]] && [[ mysql_serverid_num -ge 1 ]] ;then
#echo $mysql_binlog_num $mysql_serverid_num
echo "已发现mysqlbinlog相关配置,正在继续执行脚本……"
If_Docker
else
echo "脚本启动失败"
echo "配置文件$MYSQL_Default,中没有binlog相关配置,请先修改相关配置并重启数据库再次尝试。"
fi
}
# Main
cd $Script_Dir
echo START $Date >> $Script_Log
Mysql_binlog_If >> $Script_Log
echo END $Date >> $Script_Log
`
cmd := exec.Command("sh", "-c", script)
output, err := cmd.Output()
if err != nil {
fmt.Print("error...", err)
return
}
fmt.Print(string(output))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/heike07code/BackupSerenity.git
git@gitee.com:heike07code/BackupSerenity.git
heike07code
BackupSerenity
BackupSerenity
main

搜索帮助