-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_folders.go
190 lines (159 loc) · 6.4 KB
/
backup_folders.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/alessio/shellescape"
"github.com/google/uuid"
)
// ListBackupsInPath returns a string slice contaning relative paths to all backups
// in the passed absPath, relative to basePath
func ListBackupsInPath(options *Options, basePath string, absPath string) []string {
Log.Debug.Printf("listBackupsInPath(%s)", absPath)
backups := []string{}
if options.IsRemoteTarget() {
stdout, _, _, err := sshCall(
options,
fmt.Sprintf("find %s -type d -maxdepth 1", shellescape.Quote(absPath)),
Log.Debug,
)
if err != nil {
panic(fmt.Sprintf("listBackupsInPath: unexpected error while listing remote target folder %s: %v", options.TargetPath(), err))
}
for _, folderPath := range stdout {
folderRelativePath, err := filepath.Rel(basePath, folderPath)
if err != nil {
panic(fmt.Sprintf("listBackupsInPath: unexpected error while computing relative path on folder %s: %v", folderPath, err))
}
folderName := path.Base(folderPath)
Log.Debug.Printf("listBackupsInPath: candidate folder: %s", folderRelativePath)
if BackupFolderNameRegex.MatchString(folderName) {
Log.Debug.Printf("listBackupsInPath: matched folder: %s", folderRelativePath)
backups = append(backups, folderRelativePath)
}
}
} else {
files, err := ioutil.ReadDir(absPath)
if err != nil {
panic(fmt.Sprintf("listBackupsInPath: unexpected error while listing local target folder %s: %v", options.TargetPath(), err))
}
for _, f := range files {
if !f.IsDir() {
continue
}
folderRelativePath, err := filepath.Rel(basePath, filepath.Join(absPath, f.Name()))
if err != nil {
panic(fmt.Sprintf("listBackupsInPath: unexpected error while computing relative path on folder %s: %v", filepath.Join(absPath, f.Name()), err))
}
folderName := path.Base(f.Name())
Log.Debug.Printf("listBackupsInPath: candidate folder: %s", folderName)
if BackupFolderNameRegex.MatchString(folderName) {
Log.Debug.Printf("listBackupsInPath: matched folder: %s", folderRelativePath)
backups = append(backups, folderRelativePath)
}
}
}
return backups
}
// PrepareTargetFolder ensures all relevant folders exist at the target location
func PrepareTargetFolder(options *Options) {
if options.IsRemoteTarget() {
Log.Info.Printf(
"Check/prepare target folder: %s on %s@%s:%d",
options.TargetPath(),
options.targetUser,
options.targetHost,
options.targetPort,
)
EnsureRemoteFolderExists(options, options.TargetPath())
EnsureRemoteFolderExists(options, options.DailyFolderPath())
EnsureRemoteFolderExists(options, options.WeeklyFolderPath())
EnsureRemoteFolderExists(options, options.MonthlyFolderPath())
} else {
Log.Info.Printf("Check/prepare target folder: %s", options.TargetPath())
EnsureLocalFolderExists(options, options.TargetPath())
EnsureLocalFolderExists(options, options.DailyFolderPath())
EnsureLocalFolderExists(options, options.WeeklyFolderPath())
EnsureLocalFolderExists(options, options.MonthlyFolderPath())
}
}
// EnsureRemoteFolderExists checks for the existence of a remote folder at absPathOnRemote
// and creates it if it does not yet exist
func EnsureRemoteFolderExists(options *Options, absPathOnRemote string) {
Log.Debug.Printf("EnsureRemoteFolderExists(%s)", absPathOnRemote)
existsCode, err := uuid.NewRandom()
if err != nil {
panic("EnsureRemoteFolderExists: Could not generate okUuid for remote target check")
}
notExistsCode, err := uuid.NewRandom()
if err != nil {
panic("EnsureRemoteFolderExists: Could not generate notOkUuid for remote target check")
}
stdout, _, _, err := sshCall(
options,
fmt.Sprintf(
"if [ -d %s ]; then echo %s; else echo %s; fi",
shellescape.Quote(absPathOnRemote),
existsCode.String(),
notExistsCode.String(),
),
Log.Debug,
)
if err != nil {
panic(fmt.Sprintf("EnsureRemoteFolderExists: error checking for remote folder existence: %v", err))
} else if stdout[0] == existsCode.String() {
Log.Debug.Println("EnsureRemoteFolderExists: remote folder exists")
return
} else if stdout[0] != notExistsCode.String() {
panic("EnsureRemoteFolderExists: unexpected output checking for remote folder existence (was waiting for one of existCode/notExistsCode")
}
Log.Debug.Println("EnsureRemoteFolderExists: remote folder does not exist, creating")
_, _, _, err = sshCall(
options,
fmt.Sprintf("mkdir -p -m 0700 %s", shellescape.Quote(absPathOnRemote)),
Log.Debug,
)
if err != nil {
panic(fmt.Sprintf("EnsureRemoteFolderExists: unexpected error while creating remote folder %s: %v", absPathOnRemote, err))
}
}
// EnsureLocalFolderExists checks for the existence of a local folder at absPath
// and creates it if it does not yet exist
func EnsureLocalFolderExists(options *Options, absPath string) {
Log.Debug.Printf("EnsureLocalFolderExists(%s)", absPath)
if stat, err := os.Stat(absPath); err == nil {
Log.Debug.Printf("EnsureLocalFolderExists: target %s exists", absPath)
if stat.IsDir() {
Log.Debug.Printf("EnsureLocalFolderExists: %s is a folder, as expected", absPath)
return
} else {
panic(fmt.Sprintf("EnsureLocalFolderExists: %s is not a folder", absPath))
}
} else if os.IsNotExist(err) {
Log.Debug.Printf("EnsureLocalFolderExists: %s does not exist, creating", absPath)
err := os.MkdirAll(absPath, 0700)
if err != nil {
panic(fmt.Sprintf("EnsureLocalFolderExists: %s does not exist and could not be created", absPath))
}
} else {
panic(fmt.Sprintf("EnsureLocalFolderExists: unexpected error while checking for %s existence: %v", absPath, err))
}
}
// DetermineLastBackup fetches all backup folder names in the target path and determines the most
// most recent one, returning its relative path relative to the MAIN target folder
func DetermineLastBackup(options *Options) string {
var backups []string
backups = append(backups, ListBackupsInPath(options, options.TargetPath(), options.TargetPath())...)
backups = append(backups, ListBackupsInPath(options, options.TargetPath(), options.DailyFolderPath())...)
backups = append(backups, ListBackupsInPath(options, options.TargetPath(), options.WeeklyFolderPath())...)
backups = append(backups, ListBackupsInPath(options, options.TargetPath(), options.MonthlyFolderPath())...)
if len(backups) > 0 {
// sort.Strings(backups)
// Sort by actual date of the backup folder (its basename)
SortBackupList(&backups, false)
return backups[len(backups)-1]
}
return ""
}