Skip to content

Commit

Permalink
feat: 自动删除目录下小于1K的文件 (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeessy2 committed Oct 8, 2023
1 parent 2e94f52 commit 0c3ed03
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
9 changes: 5 additions & 4 deletions client/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package client
import (
"backup-x/entity"
"backup-x/util"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
"time"
)

// 数据库备份最小的文件大小
const minFileSize = 1000

// backupLooper
type backupLooper struct {
Wg sync.WaitGroup
Expand Down Expand Up @@ -214,10 +215,10 @@ func backup(backupConf entity.BackupConfig, encryptKey string, s3Conf entity.S3C
// check file size
if err != nil {
log.Println(err)
} else if outFileName.Size() >= 200 {
} else if outFileName.Size() >= minFileSize {
log.Printf("成功备份项目: %s, 文件名: %s\n", projectName, outFileName.Name())
} else {
err = errors.New(projectName + " 备份后的文件大小小于200字节, 当前大小:" + strconv.Itoa(int(outFileName.Size())))
err = fmt.Errorf("%s 备份后的文件小于 %d 字节, 当前为:%d 字节", projectName, minFileSize, outFileName.Size())
log.Println(err)
}
} else {
Expand Down
8 changes: 5 additions & 3 deletions client/delete_old_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ func deleteLocalOlderFiles(backupConf entity.BackupConfig) {
if !backupFile.IsDir() {
info, err := backupFile.Info()
if err == nil {
if info.Size() > 0 {
if info.Size() >= minFileSize {
backupFileNames = append(backupFileNames, backupFile.Name())
} else {
log.Println("备份后的文件大小为0字节,将删除备份文件: " + backupConf.GetProjectPath() + string(os.PathSeparator) + backupFile.Name())
os.Remove(backupConf.GetProjectPath() + string(os.PathSeparator) + backupFile.Name())
if util.IsFileNameDate(backupFile.Name()) {
log.Printf("备份后的大小为 %d 字节,小于最低值 %d,将删除备份文件: %s", info.Size(), minFileSize, backupConf.GetProjectPath()+string(os.PathSeparator)+backupFile.Name())
os.Remove(backupConf.GetProjectPath() + string(os.PathSeparator) + backupFile.Name())
}
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/aws/aws-sdk-go v1.44.293 h1:oBPrQqsyMYe61Sl/xKVvQFflXjPwYH11aKi8QR3Nhts=
github.com/aws/aws-sdk-go v1.44.293/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.45.24 h1:TZx/CizkmCQn8Rtsb11iLYutEQVGK5PK9wAhwouELBo=
github.com/aws/aws-sdk-go v1.45.24/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down Expand Up @@ -32,8 +30,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand All @@ -43,8 +39,6 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
10 changes: 9 additions & 1 deletion util/file_name_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
)

const FileNameFormatStr = "2006-01-02-15-04"
const fileNameRegStr = `([\d]{4})-([\d]{2})-([\d]{2})-([\d]{2})-([\d]{2})`

// FileNameBeforeDays 查找文件名中有多少在指定天数之前的
func FileNameBeforeDays(days int, fileNames []string, projectName string) []string {
oldFiles := make([]string, 0)
// 2006-01-02-15-04
fileRegxp := regexp.MustCompile(`([\d]{4})-([\d]{2})-([\d]{2})-([\d]{2})-([\d]{2})`)
fileRegxp := regexp.MustCompile(fileNameRegStr)
subDuration, _ := time.ParseDuration("-" + strconv.Itoa(days*24) + "h")
before := time.Now().Add(subDuration)
for i := 0; i < len(fileNames); i++ {
Expand All @@ -32,3 +33,10 @@ func FileNameBeforeDays(days int, fileNames []string, projectName string) []stri
}
return oldFiles
}

// FileNameDate 判断文件名是否为规则的文件
func IsFileNameDate(fileName string) bool {
// 2006-01-02-15-04
fileRegxp := regexp.MustCompile(fileNameRegStr)
return fileRegxp.FindString(fileName) != ""
}

0 comments on commit 0c3ed03

Please sign in to comment.