代码拉取完成,页面将自动刷新
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))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。