You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 gogsBackup | grep -v grep | awk '{print $2}' | xargs kill -9"
cmd := exec.Command("/bin/bash", "-c", command)
//1、执行Shell进行备份数据库
fmt.Println("1、正在备份gogs请稍等...")
command = `/usr/local/dsMin/dsTools/Shell/BackupGogs.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小时内生成的文件并且以gogs_开头的找出来应该只有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(), "gogs_") == 0 {
fmt.Println("3、找到刚刚备份的gogs文件:" + file.Name())
key := prefix + "/" + file.Name()
sourceFile := dirPath + "/" + file.Name()
fmt.Println("4、正在上传备份的gogs文件...")
ObsUtil.UploadFileByObsUtil(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+"/gogs_", ObsUtil.RemainDays)
//4、提示信息
fmt.Println("恭喜gogs备份成功完成")
}