-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
56 lines (50 loc) · 1.09 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
package copi
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"time"
)
// Backup things from src to dst
func Backup(src, dst string, keep int) error {
if err := cleanBackupDir(filepath.Base(src), dst, keep); err != nil {
return err
}
now := strconv.FormatInt(time.Now().Unix(), 10)
dst = filepath.Join(dst, now+"-"+filepath.Base(src))
fmt.Printf("Backup: %s\n", dst)
err := copyDir(src, dst)
if err != nil {
return fmt.Errorf("cannot copy directory: %v", err)
}
return nil
}
func cleanBackupDir(name, dst string, keep int) error {
re := regexp.MustCompile(`^[0-9]{10}-` + name)
backups := make([]os.FileInfo, 0, 5)
err := os.MkdirAll(dst, os.ModePerm)
if err != nil {
return err
}
entries, err := ioutil.ReadDir(dst)
if err != nil {
return err
}
for _, entry := range entries {
if entry.IsDir() && re.MatchString(entry.Name()) {
backups = append(backups, entry)
}
}
count := len(backups)
for i := 0; count >= keep; i++ {
err := os.RemoveAll(filepath.Join(dst, backups[i].Name()))
if err != nil {
return err
}
count--
}
return nil
}