This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.go
141 lines (120 loc) · 2.9 KB
/
backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2019. Temple3x ([email protected])
* Copyright (c) 2014 Nate Finch
*
* Use of this source code is governed by the MIT License
* that can be found in the LICENSE file.
*/
package logro
import (
"container/heap"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
// Backup holds backup log file' path & create time.
type Backup struct {
ts int64
fp string
}
// Backups implements heap interface.
type Backups struct {
bs []Backup
}
func (b *Backups) Less(i, j int) bool {
return (*b).bs[i].ts < ((*b).bs[j].ts)
}
func (b *Backups) Swap(i, j int) {
if i >= 0 && j >= 0 {
(*b).bs[i], (*b).bs[j] = (*b).bs[j], (*b).bs[i]
}
}
func (b *Backups) Len() int {
return len((*b).bs)
}
func (b *Backups) Pop() (v interface{}) {
if b.Len() > 0 {
v = (*b).bs[b.Len()-1]
b.bs = (*b).bs[:b.Len()-1]
}
return
}
func (b *Backups) Push(v interface{}) {
b.bs = append((*b).bs, v.(Backup))
}
func listBackups(outputPath string, max int) (*Backups, error) {
bs := make([]Backup, 0, max*2) // Enough cap.
b := &Backups{
bs: bs,
}
err := b.list(outputPath, max)
if err != nil {
return nil, err
}
return b, nil
}
// List all backup log files (in init process),
// and remove them if there are too many backups.
func (b *Backups) list(outputPath string, max int) error {
dir := filepath.Dir(outputPath)
ns, err := ioutil.ReadDir(dir)
if err != nil {
return err // Path error
}
prefix, ext := getPrefixAndExt(outputPath)
for _, f := range ns {
if f.IsDir() {
continue
}
if ts := parseTime(f.Name(), prefix, ext); ts != 0 {
heap.Push(b, Backup{ts, filepath.Join(dir, f.Name())})
continue
}
}
for b.Len() > max {
v := heap.Pop(b)
os.Remove(v.(Backup).fp)
}
return nil
}
// getPrefixAndExt returns the filename part and extension part from the rotation's filename.
func getPrefixAndExt(outputPath string) (prefix, ext string) {
name := filepath.Base(outputPath)
ext = filepath.Ext(name)
prefix = name[:len(name)-len(ext)] + "-"
return prefix, ext
}
const backupTimeFmt = "2006-01-02T15:04:05.000Z0700"
// parseTime extracts the formatted time from the filename by stripping off
// the filename's prefix and extension.
//
// Return 0 if the file is illegal logro backup file.
func parseTime(fp, prefix, ext string) int64 {
filename := filepath.Base(fp)
if !strings.HasPrefix(filename, prefix) {
return 0
}
if !strings.HasSuffix(filename, ext) {
return 0
}
tsStr := filename[len(prefix) : len(filename)-len(ext)]
t, err := time.Parse(backupTimeFmt, tsStr)
if err != nil {
return 0
}
return t.Unix()
}
func makeBackupFP(name string, local bool, t time.Time) (string, int64) {
dir := filepath.Dir(name)
filename := filepath.Base(name)
ext := filepath.Ext(filename)
prefix := filename[:len(filename)-len(ext)]
if !local {
t = t.UTC()
}
timestamp := t.Format(backupTimeFmt)
return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext)), t.Unix()
}