123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package jobs
- import (
- "gadmin/internal/cron/jobs/types"
- "gadmin/utility"
- "gadmin/utility/character"
- "github.com/sirupsen/logrus"
- "github.com/spf13/cast"
- "io"
- "io/ioutil"
- "os"
- "path/filepath"
- "sort"
- "strings"
- "time"
- )
- var BackupNotice = new(jBackupNotice)
- const NoticeBackupPath = "/notice/" // 备份二级路径
- type jBackupNotice struct{}
- func (j *jBackupNotice) Run() {
- logrus.WithField("source", "notice").Info("BackupNotice Run.....")
- j.createDir()
- data, err := ioutil.ReadFile(os.Getenv("STORAGE_NOTICE"))
- if err != nil {
- logrus.WithField("source", "notice").Errorf("Run read file err:%v", err.Error())
- return
- }
- if j.getLastMd5() != character.Md5Content(data) {
- dstFileName := j.backupPath() + time.Now().Format("2006-01-02-150405")
- j.copyFile(dstFileName, os.Getenv("STORAGE_NOTICE"))
- j.cleanupExpired()
- return
- }
- logrus.WithField("source", "notice").Infof("neglect..")
- }
- func (j *jBackupNotice) getLastMd5() string {
- lastName := ""
- lastIndex := 0
- err := filepath.Walk(j.backupPath(), func(pat string, info os.FileInfo, err error) error {
- n := cast.ToInt(strings.ReplaceAll(info.Name(), "-", ""))
- if n > lastIndex {
- lastIndex = n
- lastName = info.Name()
- }
- return nil
- })
- if err != nil {
- logrus.WithField("source", "notice").Errorf("getLastMd5 file err = %v\n", err)
- }
- if lastIndex == 0 {
- return ""
- }
- data, err := ioutil.ReadFile(j.backupPath() + lastName)
- if err != nil {
- logrus.WithField("source", "notice").Errorf("getLastMd5 read file err:%v", err.Error())
- return ""
- }
- return character.Md5Content(data)
- }
- func (j *jBackupNotice) cleanupExpired() {
- var files types.SrcFiles
- err := filepath.Walk(j.backupPath(), func(pat string, info os.FileInfo, err error) error {
- index := cast.ToInt(strings.ReplaceAll(info.Name(), "-", ""))
- if index > 0 {
- files = append(files, types.SrcFile{
- Name: info.Name(),
- Index: index,
- })
- }
- return nil
- })
- if err != nil {
- logrus.WithField("source", "notice").Errorf("getLastMd5 file err = %v\n", err)
- }
- // 倒序,最新的备份排在最前面
- sort.Sort(files)
- // 保留最新的100份,之前的加入到清理
- var cleanFiles types.SrcFiles
- for k, v := range files {
- if k+1 > 100 {
- cleanFiles = append(cleanFiles, v)
- }
- }
- if len(cleanFiles) == 0 {
- return
- }
- for _, v := range cleanFiles {
- path := j.backupPath() + v.Name
- logrus.WithField("source", "notice").Infoln("清理通知备份:", path)
- if err := os.Remove(path); err != nil {
- logrus.WithField("source", "notice").Errorf("os.Remove err:%v", err)
- }
- }
- }
- func (j *jBackupNotice) copyFile(dstFileName string, srcFileName string) (written int64, err error) {
- srcFile, err := os.Open(srcFileName)
- if err != nil {
- logrus.WithField("source", "notice").Errorf("open file err = %v\n", err)
- return
- }
- defer srcFile.Close()
- dstFile, err := os.Create(dstFileName)
- if err != nil {
- logrus.WithField("source", "notice").Errorf("open file err = %v\n", err)
- return
- }
- defer dstFile.Close()
- return io.Copy(dstFile, srcFile)
- }
- func (j *jBackupNotice) createDir() {
- utility.CreateDir(j.backupPath())
- }
- func (j *jBackupNotice) backupPath() string {
- return os.Getenv("BACKUP_PATH") + NoticeBackupPath
- }
|