|
|
package main
|
|
|
|
|
|
import (
|
|
|
"dsTools/Utils/ConfigUtil"
|
|
|
"dsTools/Utils/NetUtil"
|
|
|
"dsTools/Utils/ObsUtil"
|
|
|
"fmt"
|
|
|
"io/ioutil"
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
//杀掉老进程
|
|
|
command := "ps -ef | grep mysqlBackup | grep -v grep | awk '{print $2}' | xargs kill -9"
|
|
|
cmd := exec.Command("/bin/bash", "-c", command)
|
|
|
|
|
|
//1、执行Shell进行备份数据库
|
|
|
fmt.Println("1、正在备份数据库,请稍等...")
|
|
|
command = `/usr/local/dsMin/dsTools/Shell/BackupMysqlFullDataBase.sh`
|
|
|
cmd = exec.Command("/bin/bash", "-c", command)
|
|
|
output, err := cmd.Output()
|
|
|
if err != nil {
|
|
|
fmt.Printf("Execute Shell:%s failed with error:%s", command, err.Error())
|
|
|
return
|
|
|
}
|
|
|
fmt.Printf("Execute Shell:%s finished with output:\n%s", command, string(output))
|
|
|
//2、上传数据库备份到云存储
|
|
|
fmt.Println("2、正在将备份文件上传到云存储...")
|
|
|
macAddress := NetUtil.GetMacAddrs()[0]
|
|
|
dirPath := "/usr/local/Backup"
|
|
|
//此目录下,最近2小时内生成的文件,并且以base_db_开头的找出来,应该只有1个
|
|
|
files, err := ioutil.ReadDir(dirPath) //读取目录下文件
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
//云存储前缀
|
|
|
var prefix = ObsUtil.BackupPrefix + "/" + ConfigUtil.InstallAreaCode + "/" + macAddress
|
|
|
for _, file := range files {
|
|
|
if file.IsDir() {
|
|
|
continue
|
|
|
} else {
|
|
|
now := time.Now()
|
|
|
sumD := now.Sub(file.ModTime())
|
|
|
if sumD.Hours() < 2 && strings.Index(file.Name(), "base_db_") == 0 {
|
|
|
fmt.Println("3、找到刚刚备份的数据库文件:" + file.Name())
|
|
|
key := prefix + "/" + file.Name()
|
|
|
sourceFile := dirPath + "/" + file.Name()
|
|
|
fmt.Println("4、正在上传备份的数据库文件...")
|
|
|
ObsUtil.UploadFileMultiPart(key, sourceFile)
|
|
|
}
|
|
|
//删除历史文件
|
|
|
if sumD.Hours() > 24 {
|
|
|
sourceFile := dirPath + "/" + file.Name()
|
|
|
err := os.Remove(sourceFile)
|
|
|
if err != nil {
|
|
|
fmt.Println("删除历史文件" + sourceFile + "失败!")
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//3、对云备份的文件进行清理
|
|
|
fmt.Println("5、正在进行云备份文件的清理工作...")
|
|
|
ObsUtil.DeleteExpireFile(prefix+"/base_db_", ObsUtil.RemainDays)
|
|
|
//4、提示信息
|
|
|
fmt.Println("恭喜,数据库备份成功完成!")
|
|
|
}
|