forked from vitessio/vitess
/
file.go
148 lines (127 loc) · 3.96 KB
/
file.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
142
143
144
145
146
147
148
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package filebackupstorage implements the BackupStorage interface
// for a local filesystem (which can be an NFS mount).
package filebackupstorage
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/mysqlctl/backupstorage"
)
var (
// FileBackupStorageRoot is where the backups will go.
// Exported for test purposes.
FileBackupStorageRoot = flag.String("file_backup_storage_root", "", "root directory for the file backup storage")
)
// FileBackupHandle implements BackupHandle for local file system.
type FileBackupHandle struct {
fbs *FileBackupStorage
dir string
name string
readOnly bool
}
// Directory is part of the BackupHandle interface
func (fbh *FileBackupHandle) Directory() string {
return fbh.dir
}
// Name is part of the BackupHandle interface
func (fbh *FileBackupHandle) Name() string {
return fbh.name
}
// AddFile is part of the BackupHandle interface
func (fbh *FileBackupHandle) AddFile(ctx context.Context, filename string) (io.WriteCloser, error) {
if fbh.readOnly {
return nil, fmt.Errorf("AddFile cannot be called on read-only backup")
}
p := path.Join(*FileBackupStorageRoot, fbh.dir, fbh.name, filename)
return os.Create(p)
}
// EndBackup is part of the BackupHandle interface
func (fbh *FileBackupHandle) EndBackup(ctx context.Context) error {
if fbh.readOnly {
return fmt.Errorf("EndBackup cannot be called on read-only backup")
}
return nil
}
// AbortBackup is part of the BackupHandle interface
func (fbh *FileBackupHandle) AbortBackup(ctx context.Context) error {
if fbh.readOnly {
return fmt.Errorf("AbortBackup cannot be called on read-only backup")
}
return fbh.fbs.RemoveBackup(ctx, fbh.dir, fbh.name)
}
// ReadFile is part of the BackupHandle interface
func (fbh *FileBackupHandle) ReadFile(ctx context.Context, filename string) (io.ReadCloser, error) {
if !fbh.readOnly {
return nil, fmt.Errorf("ReadFile cannot be called on read-write backup")
}
p := path.Join(*FileBackupStorageRoot, fbh.dir, fbh.name, filename)
return os.Open(p)
}
// FileBackupStorage implements BackupStorage for local file system.
type FileBackupStorage struct{}
// ListBackups is part of the BackupStorage interface
func (fbs *FileBackupStorage) ListBackups(ctx context.Context, dir string) ([]backupstorage.BackupHandle, error) {
// ReadDir already sorts the results
p := path.Join(*FileBackupStorageRoot, dir)
fi, err := ioutil.ReadDir(p)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
result := make([]backupstorage.BackupHandle, 0, len(fi))
for _, info := range fi {
if !info.IsDir() {
continue
}
if info.Name() == "." || info.Name() == ".." {
continue
}
result = append(result, &FileBackupHandle{
fbs: fbs,
dir: dir,
name: info.Name(),
readOnly: true,
})
}
return result, nil
}
// StartBackup is part of the BackupStorage interface
func (fbs *FileBackupStorage) StartBackup(ctx context.Context, dir, name string) (backupstorage.BackupHandle, error) {
// Make sure the directory exists.
p := path.Join(*FileBackupStorageRoot, dir)
if err := os.MkdirAll(p, os.ModePerm); err != nil {
return nil, err
}
// Create the subdirectory for this named backup.
p = path.Join(p, name)
if err := os.Mkdir(p, os.ModePerm); err != nil {
return nil, err
}
return &FileBackupHandle{
fbs: fbs,
dir: dir,
name: name,
readOnly: false,
}, nil
}
// RemoveBackup is part of the BackupStorage interface
func (fbs *FileBackupStorage) RemoveBackup(ctx context.Context, dir, name string) error {
p := path.Join(*FileBackupStorageRoot, dir, name)
return os.RemoveAll(p)
}
// Close implements BackupStorage.
func (fbs *FileBackupStorage) Close() error {
return nil
}
func init() {
backupstorage.BackupStorageMap["file"] = &FileBackupStorage{}
}