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